Skip to content

Commit

Permalink
Notifications fix (#8360) (#8361)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
github-actions[bot] and SchrodingersGat authored Oct 25, 2024
1 parent 7babef0 commit 0ae9cdd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
10 changes: 7 additions & 3 deletions src/backend/InvenTree/InvenTree/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ def ready(self):
- Adding users set in the current environment
"""
# skip loading if plugin registry is not loaded or we run in a background thread

if not InvenTree.ready.isPluginRegistryLoaded():
return

# Skip if not in worker or main thread
if (
not InvenTree.ready.isPluginRegistryLoaded()
or not InvenTree.ready.isInMainThread()
not InvenTree.ready.isInMainThread()
and not InvenTree.ready.isInWorkerThread()
):
return

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

if InvenTree.ready.canAppAccessDatabase() or settings.TESTING_ENV:
self.remove_obsolete_tasks()

self.collect_tasks()
self.start_background_tasks()

Expand Down
36 changes: 27 additions & 9 deletions src/backend/InvenTree/common/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import common.models
import InvenTree.helpers
from InvenTree.ready import isImportingData
from InvenTree.ready import isImportingData, isRebuildingData
from plugin import registry
from plugin.models import NotificationUserSetting, PluginConfig
from users.models import Owner
Expand Down Expand Up @@ -185,9 +185,20 @@ class MethodStorageClass:
Is initialized on startup as one instance named `storage` in this file.
"""

liste = None
methods_list = None
user_settings = {}

@property
def methods(self):
"""Return all available methods.
This is cached, and stored internally.
"""
if self.methods_list is None:
self.collect()

return self.methods_list

def collect(self, selected_classes=None):
"""Collect all classes in the environment that are notification methods.
Expand All @@ -196,7 +207,8 @@ def collect(self, selected_classes=None):
Args:
selected_classes (class, optional): References to the classes that should be registered. Defaults to None.
"""
logger.debug('Collecting notification methods')
logger.debug('Collecting notification methods...')

current_method = (
InvenTree.helpers.inheritors(NotificationMethod) - IGNORED_NOTIFICATION_CLS
)
Expand All @@ -219,8 +231,12 @@ def collect(self, selected_classes=None):
item.plugin = plugin() if plugin else None
filtered_list[ref] = item

storage.liste = list(filtered_list.values())
logger.info('Found %s notification methods', len(storage.liste))
storage.methods_list = list(filtered_list.values())

logger.info('Found %s notification methods', len(storage.methods_list))

for item in storage.methods_list:
logger.debug(' - %s', str(item))

def get_usersettings(self, user) -> list:
"""Returns all user settings for a specific user.
Expand All @@ -234,7 +250,8 @@ def get_usersettings(self, user) -> list:
list: All applicablae notification settings.
"""
methods = []
for item in storage.liste:

for item in storage.methods:
if item.USER_SETTING:
new_key = f'NOTIFICATION_METHOD_{item.METHOD_NAME.upper()}'

Expand All @@ -250,6 +267,7 @@ def get_usersettings(self, user) -> list:
'icon': getattr(item, 'METHOD_ICON', ''),
'method': item.METHOD_NAME,
})

return methods


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

# Check if data is importing currently
if isImportingData():
if isImportingData() or isRebuildingData():
return

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

# Collect possible methods
if delivery_methods is None:
delivery_methods = storage.liste or []
delivery_methods = storage.methods or []
else:
delivery_methods = delivery_methods - IGNORED_NOTIFICATION_CLS

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


def trigger_superuser_notification(plugin: PluginConfig, msg: str):
Expand Down
2 changes: 1 addition & 1 deletion src/backend/InvenTree/plugin/templatetags/plugin_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def notification_list(context, *args, **kwargs):
'description': a.__doc__,
'name': a.__name__,
}
for a in storage.liste
for a in storage.methods
]


Expand Down

0 comments on commit 0ae9cdd

Please sign in to comment.