Skip to content

Commit 0ae9cdd

Browse files
Notifications fix (#8360) (#8361)
* Fix for app loading - Allow collection for background worker too * Improved logging * Refactor MethodStorageClass - Cache methods more intelligently - Re-collect if null (cherry picked from commit 331692b) Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
1 parent 7babef0 commit 0ae9cdd

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

src/backend/InvenTree/InvenTree/apps.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ def ready(self):
4040
- Adding users set in the current environment
4141
"""
4242
# skip loading if plugin registry is not loaded or we run in a background thread
43+
44+
if not InvenTree.ready.isPluginRegistryLoaded():
45+
return
46+
47+
# Skip if not in worker or main thread
4348
if (
44-
not InvenTree.ready.isPluginRegistryLoaded()
45-
or not InvenTree.ready.isInMainThread()
49+
not InvenTree.ready.isInMainThread()
50+
and not InvenTree.ready.isInWorkerThread()
4651
):
4752
return
4853

@@ -52,7 +57,6 @@ def ready(self):
5257

5358
if InvenTree.ready.canAppAccessDatabase() or settings.TESTING_ENV:
5459
self.remove_obsolete_tasks()
55-
5660
self.collect_tasks()
5761
self.start_background_tasks()
5862

src/backend/InvenTree/common/notifications.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import common.models
1212
import InvenTree.helpers
13-
from InvenTree.ready import isImportingData
13+
from InvenTree.ready import isImportingData, isRebuildingData
1414
from plugin import registry
1515
from plugin.models import NotificationUserSetting, PluginConfig
1616
from users.models import Owner
@@ -185,9 +185,20 @@ class MethodStorageClass:
185185
Is initialized on startup as one instance named `storage` in this file.
186186
"""
187187

188-
liste = None
188+
methods_list = None
189189
user_settings = {}
190190

191+
@property
192+
def methods(self):
193+
"""Return all available methods.
194+
195+
This is cached, and stored internally.
196+
"""
197+
if self.methods_list is None:
198+
self.collect()
199+
200+
return self.methods_list
201+
191202
def collect(self, selected_classes=None):
192203
"""Collect all classes in the environment that are notification methods.
193204
@@ -196,7 +207,8 @@ def collect(self, selected_classes=None):
196207
Args:
197208
selected_classes (class, optional): References to the classes that should be registered. Defaults to None.
198209
"""
199-
logger.debug('Collecting notification methods')
210+
logger.debug('Collecting notification methods...')
211+
200212
current_method = (
201213
InvenTree.helpers.inheritors(NotificationMethod) - IGNORED_NOTIFICATION_CLS
202214
)
@@ -219,8 +231,12 @@ def collect(self, selected_classes=None):
219231
item.plugin = plugin() if plugin else None
220232
filtered_list[ref] = item
221233

222-
storage.liste = list(filtered_list.values())
223-
logger.info('Found %s notification methods', len(storage.liste))
234+
storage.methods_list = list(filtered_list.values())
235+
236+
logger.info('Found %s notification methods', len(storage.methods_list))
237+
238+
for item in storage.methods_list:
239+
logger.debug(' - %s', str(item))
224240

225241
def get_usersettings(self, user) -> list:
226242
"""Returns all user settings for a specific user.
@@ -234,7 +250,8 @@ def get_usersettings(self, user) -> list:
234250
list: All applicablae notification settings.
235251
"""
236252
methods = []
237-
for item in storage.liste:
253+
254+
for item in storage.methods:
238255
if item.USER_SETTING:
239256
new_key = f'NOTIFICATION_METHOD_{item.METHOD_NAME.upper()}'
240257

@@ -250,6 +267,7 @@ def get_usersettings(self, user) -> list:
250267
'icon': getattr(item, 'METHOD_ICON', ''),
251268
'method': item.METHOD_NAME,
252269
})
270+
253271
return methods
254272

255273

@@ -352,7 +370,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs):
352370
delivery_methods = kwargs.get('delivery_methods', None)
353371

354372
# Check if data is importing currently
355-
if isImportingData():
373+
if isImportingData() or isRebuildingData():
356374
return
357375

358376
# Resolve object reference
@@ -422,7 +440,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs):
422440

423441
# Collect possible methods
424442
if delivery_methods is None:
425-
delivery_methods = storage.liste or []
443+
delivery_methods = storage.methods or []
426444
else:
427445
delivery_methods = delivery_methods - IGNORED_NOTIFICATION_CLS
428446

@@ -439,7 +457,7 @@ def trigger_notification(obj, category=None, obj_ref='pk', **kwargs):
439457
# Set delivery flag
440458
common.models.NotificationEntry.notify(category, obj_ref_value)
441459
else:
442-
logger.debug("No possible users for notification '%s'", category)
460+
logger.info("No possible users for notification '%s'", category)
443461

444462

445463
def trigger_superuser_notification(plugin: PluginConfig, msg: str):

src/backend/InvenTree/plugin/templatetags/plugin_extras.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def notification_list(context, *args, **kwargs):
9595
'description': a.__doc__,
9696
'name': a.__name__,
9797
}
98-
for a in storage.liste
98+
for a in storage.methods
9999
]
100100

101101

0 commit comments

Comments
 (0)