django-queuebie 0.1.0__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ambient Digital
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ambient Innovation: GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,205 @@
1
+ Metadata-Version: 2.3
2
+ Name: django-queuebie
3
+ Version: 0.1.0
4
+ Summary: Simple message queue for commands and events (CQRS)
5
+ Author-email: Ambient Digital <hello@ambient.digital>
6
+ Description-Content-Type: text/markdown
7
+ Classifier: Development Status :: 4 - Beta
8
+ Classifier: Environment :: Web Environment
9
+ Classifier: Framework :: Django
10
+ Classifier: Framework :: Django :: 4.2
11
+ Classifier: Framework :: Django :: 5.0
12
+ Classifier: Framework :: Django :: 5.1
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Natural Language :: English
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Topic :: Utilities
24
+ Requires-Dist: Django>=4.2
25
+ Requires-Dist: typer~=0.12 ; extra == "dev"
26
+ Requires-Dist: pytest-django~=4.9 ; extra == "dev"
27
+ Requires-Dist: pytest-mock~=3.14 ; extra == "dev"
28
+ Requires-Dist: coverage~=7.6 ; extra == "dev"
29
+ Requires-Dist: pre-commit~=4.0 ; extra == "dev"
30
+ Requires-Dist: ruff~=0.6 ; extra == "dev"
31
+ Requires-Dist: sphinx~=7.1 ; extra == "dev"
32
+ Requires-Dist: sphinx-rtd-theme~=2.0 ; extra == "dev"
33
+ Requires-Dist: m2r2==0.3.3.post2 ; extra == "dev"
34
+ Requires-Dist: mistune<2.0.0 ; extra == "dev"
35
+ Requires-Dist: flit~=3.9 ; extra == "dev"
36
+ Requires-Dist: keyring~=25.4 ; extra == "dev"
37
+ Requires-Dist: ambient-package-update ; extra == "dev"
38
+ Project-URL: Bugtracker, https://github.com/ambient-innovation/django-queuebie/issues
39
+ Project-URL: Changelog, https://django-queuebie.readthedocs.io/en/latest/features/changelog.html
40
+ Project-URL: Documentation, https://django-queuebie.readthedocs.io/en/latest/index.html
41
+ Project-URL: Homepage, https://github.com/ambient-innovation/django-queuebie/
42
+ Project-URL: Maintained by, https://ambient.digital/
43
+ Provides-Extra: dev
44
+
45
+ [![PyPI release](https://img.shields.io/pypi/v/django-queuebie.svg)](https://pypi.org/project/django-queuebie/)
46
+ [![Downloads](https://static.pepy.tech/badge/django-queuebie)](https://pepy.tech/project/django-queuebie)
47
+ [![Coverage](https://img.shields.io/badge/Coverage-100.0%25-success)](https://github.com/ambient-innovation/django-queuebie/actions?workflow=CI)
48
+ [![Linting](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
49
+ [![Coding Style](https://img.shields.io/badge/code%20style-Ruff-000000.svg)](https://github.com/astral-sh/ruff)
50
+ [![Documentation Status](https://readthedocs.org/projects/django-queuebie/badge/?version=latest)](https://django-queuebie.readthedocs.io/en/latest/?badge=latest)
51
+
52
+ A simple message queue for commands and events (CQRS) for Django.
53
+
54
+ * [PyPI](https://pypi.org/project/django-queuebie/)
55
+ * [GitHub](https://github.com/ambient-innovation/django-queuebie)
56
+ * [Full documentation](https://django-queuebie.readthedocs.io/en/latest/index.html)
57
+ * Creator & Maintainer: [Ambient Digital](https://ambient.digital/)
58
+
59
+
60
+ ## Features
61
+
62
+ * Split up your business logic in commands and events
63
+ * Commands are imperatives telling your system what to do, events reflect that something has happened
64
+ * Register light-weight functions via a decorator to listen to your commands and events
65
+ * Message handlers receive the context of the message (command or event), providing an explicit API
66
+ * No magic, no side effects since the queue works synchronously
67
+
68
+ ## Installation
69
+
70
+ - Install the package via pip:
71
+
72
+ `pip install django_queuebie`
73
+
74
+ or via pipenv:
75
+
76
+ `pipenv install django_queuebie`
77
+
78
+ - Add module to `INSTALLED_APPS` within the main django `settings.py`:
79
+
80
+ ```python
81
+ INSTALLED_APPS = (
82
+ # ...
83
+ "queuebie",
84
+ )
85
+ ```
86
+
87
+
88
+
89
+ ## Contribute
90
+
91
+ ### Setup package for development
92
+
93
+ - Create a Python virtualenv and activate it
94
+ - Install "pip-tools" with `pip install -U pip-tools`
95
+ - Compile the requirements with `pip-compile --extra dev, -o requirements.txt pyproject.toml --resolver=backtracking`
96
+ - Sync the dependencies with your virtualenv with `pip-sync`
97
+
98
+ ### Add functionality
99
+
100
+ - Create a new branch for your feature
101
+ - Change the dependency in your requirements.txt to a local (editable) one that points to your local file system:
102
+ `-e /Users/workspace/django-queuebie` or via pip `pip install -e /Users/workspace/django-queuebie`
103
+ - Ensure the code passes the tests
104
+ - Create a pull request
105
+
106
+ ### Run tests
107
+
108
+ - Run tests
109
+ ````
110
+ pytest --ds settings tests
111
+ ````
112
+
113
+ - Check coverage
114
+ ````
115
+ coverage run -m pytest --ds settings tests
116
+ coverage report -m
117
+ ````
118
+
119
+ ### Git hooks (via pre-commit)
120
+
121
+ We use pre-push hooks to ensure that only linted code reaches our remote repository and pipelines aren't triggered in
122
+ vain.
123
+
124
+ To enable the configured pre-push hooks, you need to [install](https://pre-commit.com/) pre-commit and run once:
125
+
126
+ pre-commit install -t pre-push -t pre-commit --install-hooks
127
+
128
+ This will permanently install the git hooks for both, frontend and backend, in your local
129
+ [`.git/hooks`](./.git/hooks) folder.
130
+ The hooks are configured in the [`.pre-commit-config.yaml`](templates/.pre-commit-config.yaml.tpl).
131
+
132
+ You can check whether hooks work as intended using the [run](https://pre-commit.com/#pre-commit-run) command:
133
+
134
+ pre-commit run [hook-id] [options]
135
+
136
+ Example: run single hook
137
+
138
+ pre-commit run ruff --all-files
139
+
140
+ Example: run all hooks of pre-push stage
141
+
142
+ pre-commit run --all-files --hook-stage push
143
+
144
+ ### Update documentation
145
+
146
+ - To build the documentation, run: `sphinx-build docs/ docs/_build/html/`.
147
+ - Open `docs/_build/html/index.html` to see the documentation.
148
+
149
+
150
+ ### Translation files
151
+
152
+ If you have added custom text, make sure to wrap it in `_()` where `_` is
153
+ gettext_lazy (`from django.utils.translation import gettext_lazy as _`).
154
+
155
+ How to create translation file:
156
+
157
+ * Navigate to `django-queuebie`
158
+ * `python manage.py makemessages -l de`
159
+ * Have a look at the new/changed files within `queuebie/locale`
160
+
161
+ How to compile translation files:
162
+
163
+ * Navigate to `django-queuebie`
164
+ * `python manage.py compilemessages`
165
+ * Have a look at the new/changed files within `queuebie/locale`
166
+
167
+
168
+ ### Publish to ReadTheDocs.io
169
+
170
+ - Fetch the latest changes in GitHub mirror and push them
171
+ - Trigger new build at ReadTheDocs.io (follow instructions in admin panel at RTD) if the GitHub webhook is not yet set
172
+ up.
173
+
174
+ ### Publish to PyPi
175
+
176
+ - Update documentation about new/changed functionality
177
+
178
+ - Update the `Changelog`
179
+
180
+ - Increment version in main `__init__.py`
181
+
182
+ - Create pull request / merge to main
183
+
184
+ - This project uses the flit package to publish to PyPI. Thus, publishing should be as easy as running:
185
+ ```
186
+ flit publish
187
+ ```
188
+
189
+ To publish to TestPyPI use the following to ensure that you have set up your .pypirc as
190
+ shown [here](https://flit.readthedocs.io/en/latest/upload.html#using-pypirc) and use the following command:
191
+
192
+ ```
193
+ flit publish --repository testpypi
194
+ ```
195
+
196
+ ### Maintenance
197
+
198
+ Please note that this package supports the [ambient-package-update](https://pypi.org/project/ambient-package-update/).
199
+ So you don't have to worry about the maintenance of this package. This updater is rendering all important
200
+ configuration and setup files. It works similar to well-known updaters like `pyupgrade` or `django-upgrade`.
201
+
202
+ To run an update, refer to the [documentation page](https://pypi.org/project/ambient-package-update/)
203
+ of the "ambient-package-update".
204
+
205
+
@@ -0,0 +1,17 @@
1
+ queuebie/__init__.py,sha256=fEdc8GP2WxdWAotrTBYmqywK_fAZhkIq72ITTcv0Bwo,207
2
+ queuebie/apps.py,sha256=dTNk2xGvasXJncLUbbIjVQZrX7VYQsIdv_7ToFPy5b8,477
3
+ queuebie/exceptions.py,sha256=B_vsZBEMqpWsxUp5RrokabiZOYfNEbV0nGxhhX0o5NY,711
4
+ queuebie/logger.py,sha256=OWO9yiNxfnhUqejoR6RCUueKSWYzsMtfUBCmxAuAksk,244
5
+ queuebie/messages.py,sha256=JcC7QxWzWllxDkwbYGefhaL1YWT_froOXa1NDugbGCM,1022
6
+ queuebie/registry.py,sha256=F9kMwwl09ZEJYGHUKxg7zSPPXRzqOvQFOAFJy4tRY94,6149
7
+ queuebie/runner.py,sha256=VmPCLQfWdvU3HjOscQyulG-emsNGMRkR3GwFuuTn-Jg,2252
8
+ queuebie/settings.py,sha256=CAUldgjCH-coTIAPctaYDRWmoZXb2NcWZlaMBxec0Nw,850
9
+ queuebie/utils.py,sha256=aeHOkILLpPrkA9bnDmsR1YF40KLdSlKqk15ItmLTZW4,997
10
+ queuebie/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ queuebie/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ queuebie/management/commands/clear_queuebie_registry.py,sha256=Vx4JLSo9-i7gK0YeNP5F_50TsyjEP4i4plYK_byHj3I,392
13
+ django_queuebie-0.1.0.dist-info/LICENSE,sha256=VCMRQqZBDEG5HsJdp-Ps6a3-GFnpQZPzitGMRUHdgso,1093
14
+ django_queuebie-0.1.0.dist-info/LICENSE.md,sha256=SiLpAPFRchFM_cDmWQIdItf_xPR6jeYCAaNUgpKWxV8,1102
15
+ django_queuebie-0.1.0.dist-info/WHEEL,sha256=ssQ84EZ5gH1pCOujd3iW7HClo_O_aDaClUbX4B8bjKY,100
16
+ django_queuebie-0.1.0.dist-info/METADATA,sha256=sX7mvm05fJwZO5LqjMvybDjFivZsrcdY5ETQMsRxfaQ,7536
17
+ django_queuebie-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: flit 3.10.1
3
+ Root-Is-Purelib: true
4
+ Tag: py2-none-any
5
+ Tag: py3-none-any
queuebie/__init__.py ADDED
@@ -0,0 +1,8 @@
1
+ """Simple message queue for commands and events (CQRS)"""
2
+
3
+ __version__ = "0.1.0"
4
+
5
+ from queuebie.registry import MessageRegistry
6
+
7
+ # Create global message registry
8
+ message_registry = MessageRegistry()
queuebie/apps.py ADDED
@@ -0,0 +1,15 @@
1
+ from django.apps import AppConfig
2
+
3
+ from queuebie import message_registry
4
+
5
+
6
+ class QueuebieConfig(AppConfig):
7
+ default_auto_field = "django.db.models.BigAutoField"
8
+ name = "queuebie"
9
+
10
+ def ready(self):
11
+ super().ready()
12
+
13
+ # Register all decorated functions before they get imported by something else which will break the
14
+ # registration process since decorators are only executed the first time
15
+ message_registry.autodiscover()
queuebie/exceptions.py ADDED
@@ -0,0 +1,17 @@
1
+ class RegisterWrongMessageTypeError(TypeError):
2
+ def __init__(self, *, message_name: str, decoratee_name: str):
3
+ super().__init__(
4
+ f'Trying to register message function of wrong type: "{message_name}" on handler "{decoratee_name}".'
5
+ )
6
+
7
+
8
+ class RegisterOutOfScopeCommandError(TypeError):
9
+ def __init__(self, *, message_name: str, decoratee_name: str):
10
+ super().__init__(
11
+ f'Trying to register a command from another scope/app: "{message_name}" on handler "{decoratee_name}".'
12
+ )
13
+
14
+
15
+ class InvalidMessageTypeError(TypeError):
16
+ def __init__(self, *, class_name: str):
17
+ super().__init__(f'"{class_name}" is not an Event or Command')
queuebie/logger.py ADDED
@@ -0,0 +1,10 @@
1
+ import logging
2
+
3
+ from queuebie.settings import get_queuebie_logger_name
4
+
5
+
6
+ def get_logger() -> logging.Logger:
7
+ """
8
+ Returns an instance of a queuebie Django logger
9
+ """
10
+ return logging.getLogger(get_queuebie_logger_name())
File without changes
File without changes
@@ -0,0 +1,13 @@
1
+ from django.core.cache import cache
2
+ from django.core.management.base import BaseCommand
3
+
4
+ from queuebie.logger import get_logger
5
+ from queuebie.settings import get_queuebie_cache_key
6
+
7
+
8
+ class Command(BaseCommand):
9
+ def handle(self, *args, **options):
10
+ cache.delete(get_queuebie_cache_key())
11
+
12
+ logger = get_logger()
13
+ logger.info("Queuebie registry cleared.")
queuebie/messages.py ADDED
@@ -0,0 +1,39 @@
1
+ import abc
2
+ import uuid
3
+
4
+
5
+ class Message(abc.ABC):
6
+ """
7
+ Base class for all commands and events.
8
+ """
9
+
10
+ uuid: str
11
+
12
+ def __post_init__(self):
13
+ super().__init__()
14
+ # Hier kannst du zusätzliche Initialisierungslogik hinzufügen, falls nötig
15
+ self.uuid = str(uuid.uuid4())
16
+
17
+ def __str__(self) -> str:
18
+ return f"{self.__class__} ({self.uuid})"
19
+
20
+ @classmethod
21
+ def module_path(cls) -> str:
22
+ return f"{cls.__module__}.{cls.__qualname__}"
23
+
24
+
25
+ class Command(Message):
26
+ """
27
+ Commands are messages which prompt the system to do something.
28
+ Are always written in present tense: "CreateInvoice".
29
+ Every instance has to be decorated as "@dataclass(kw_only=True)".
30
+ # todo: check that messages are dataclasses?
31
+ """
32
+
33
+
34
+ class Event(Message):
35
+ """
36
+ Events are the results of a command.
37
+ Are always written in past tense: "InvoiceCreated".
38
+ Every instance has to be decorated as "@dataclass(kw_only=True)".
39
+ """
queuebie/registry.py ADDED
@@ -0,0 +1,153 @@
1
+ import dataclasses
2
+ import importlib
3
+ import json
4
+ import os
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ from django.apps import apps
9
+ from django.core.cache import cache
10
+
11
+ from queuebie.exceptions import RegisterOutOfScopeCommandError, RegisterWrongMessageTypeError
12
+ from queuebie.logger import get_logger
13
+ from queuebie.messages import Command, Event
14
+ from queuebie.settings import get_queuebie_app_base_path, get_queuebie_cache_key, get_queuebie_strict_mode
15
+ from queuebie.utils import is_part_of_app, unique_append_to_inner_list
16
+
17
+
18
+ @dataclasses.dataclass(kw_only=True)
19
+ class FunctionDefinition:
20
+ module: str
21
+ name: str
22
+
23
+
24
+ class MessageRegistry:
25
+ """
26
+ Singleton for registering messages classes in.
27
+ """
28
+
29
+ # TODO: make message registry generic and put in toolbox
30
+ _instance: "MessageRegistry" = None
31
+
32
+ def __init__(self):
33
+ self.command_dict: dict = {}
34
+ self.event_dict: dict = {}
35
+
36
+ def __new__(cls, *args, **kwargs):
37
+ if not cls._instance:
38
+ cls._instance = super().__new__(cls)
39
+ return cls._instance
40
+
41
+ def register_command(self, *, command: type[Command]):
42
+ def decorator(decoratee):
43
+ # Ensure that registered message is of correct type
44
+ if not (issubclass(command, Command)):
45
+ raise RegisterWrongMessageTypeError(message_name=command.__name__, decoratee_name=decoratee.__name__)
46
+
47
+ if get_queuebie_strict_mode() and not is_part_of_app(function=decoratee, class_type=command):
48
+ raise RegisterOutOfScopeCommandError(message_name=command.__name__, decoratee_name=decoratee.__name__)
49
+
50
+ # Add decoratee to dependency list
51
+ function_definition = dataclasses.asdict(
52
+ FunctionDefinition(module=decoratee.__module__, name=decoratee.__name__)
53
+ )
54
+ self.command_dict = unique_append_to_inner_list(
55
+ data=self.command_dict, key=command.module_path(), value=function_definition
56
+ )
57
+
58
+ logger = get_logger()
59
+ logger.debug("Registered command '%s'", decoratee.__name__)
60
+
61
+ # Return decoratee
62
+ return decoratee
63
+
64
+ return decorator
65
+
66
+ def register_event(self, *, event: type[Event]):
67
+ # TODO: create a generic registry function and "inherit" here from it
68
+ def decorator(decoratee):
69
+ # Ensure that registered message is of correct type
70
+ if not (issubclass(event, Event)):
71
+ raise RegisterWrongMessageTypeError(message_name=event.__name__, decoratee_name=decoratee.__name__)
72
+
73
+ # Add decoratee to dependency list
74
+ function_definition = dataclasses.asdict(
75
+ FunctionDefinition(module=decoratee.__module__, name=decoratee.__name__)
76
+ )
77
+ self.event_dict = unique_append_to_inner_list(
78
+ data=self.event_dict, key=event.module_path(), value=function_definition
79
+ )
80
+
81
+ logger = get_logger()
82
+ logger.debug("Registered event '%s'", decoratee.__name__)
83
+
84
+ # Return decoratee
85
+ return decoratee
86
+
87
+ return decorator
88
+
89
+ def autodiscover(self) -> None:
90
+ """
91
+ Detects message registries which have been registered via the "register_*" decorator.
92
+ """
93
+ # Fetch registered handlers from cache if possible
94
+ self.command_dict, self.event_dict = self._load_handlers_from_cache()
95
+
96
+ # If the handlers were cached, we don't have to go through the file system
97
+ if len(self.command_dict) > 0 and len(self.event_dict) > 0:
98
+ return
99
+
100
+ # Project directory
101
+ project_path = get_queuebie_app_base_path()
102
+ logger = get_logger()
103
+
104
+ for app_config in apps.get_app_configs():
105
+ app_path = Path(app_config.path).resolve()
106
+
107
+ # If it's not a local app, we don't care
108
+ if project_path not in app_path.parents:
109
+ continue
110
+
111
+ for message_type in ("commands", "events"):
112
+ try:
113
+ for module in os.listdir(app_path / "handlers" / message_type):
114
+ if module[-3:] != ".py":
115
+ continue
116
+ module_name = module.replace(".py", "")
117
+ module_path = f"{app_config.name}.handlers.{message_type}.{module_name}"
118
+ sys_module = sys.modules.get(module_path)
119
+ if sys_module:
120
+ importlib.reload(sys_module)
121
+ else:
122
+ importlib.import_module(module_path)
123
+ logger.debug(f'"{module_path}" imported.')
124
+ except FileNotFoundError:
125
+ pass
126
+
127
+ # Log to shell which functions have been detected
128
+ logger.debug("Message autodiscovery running for commands...")
129
+ for command in self.command_dict:
130
+ handler_list = ", ".join(str(x) for x in self.command_dict[command])
131
+ logger.debug(f"* {command}: [{handler_list}]")
132
+ logger.debug("Message autodiscovery running for events...")
133
+ for event in self.event_dict:
134
+ handler_list = ", ".join(str(x) for x in self.event_dict[event])
135
+ logger.debug(f"* {event}: [{handler_list}]")
136
+
137
+ logger.debug(f"{len(self.command_dict) + len(self.event_dict)} message handlers detected.\n")
138
+
139
+ # Update cache
140
+ cache.set(get_queuebie_cache_key(), json.dumps({"commands": self.command_dict, "events": self.event_dict}))
141
+
142
+ def _load_handlers_from_cache(self) -> tuple[dict, dict]:
143
+ """
144
+ Get registered handler definitions from Django cache
145
+ """
146
+ cached_data = cache.get(get_queuebie_cache_key())
147
+ if cached_data is None:
148
+ return {}, {}
149
+ json_data = json.loads(cached_data)
150
+ cached_commands = json_data.get("commands", None)
151
+ cached_events = json_data.get("events", None)
152
+
153
+ return cached_commands, cached_events
queuebie/runner.py ADDED
@@ -0,0 +1,58 @@
1
+ import importlib
2
+
3
+ from django.db import transaction
4
+
5
+ from queuebie import message_registry
6
+ from queuebie.exceptions import InvalidMessageTypeError
7
+ from queuebie.logger import get_logger
8
+ from queuebie.messages import Command, Event, Message
9
+
10
+
11
+ def handle_message(messages: Message | list[Message]) -> None:
12
+ queue: list[Message] = messages if isinstance(messages, list) else [messages]
13
+
14
+ for message in queue:
15
+ if not isinstance(message, (Command, Event)):
16
+ raise InvalidMessageTypeError(class_name=message.__class__.__name__)
17
+
18
+ # Run auto-registry
19
+ message_registry.autodiscover()
20
+
21
+ handler_list = []
22
+ while queue:
23
+ message = queue.pop(0)
24
+ if isinstance(message, Command):
25
+ handler_list = message_registry.command_dict.get(message.module_path(), [])
26
+ else:
27
+ handler_list = message_registry.event_dict.get(message.module_path(), [])
28
+
29
+ new_messages = _process_message(handler_list=handler_list, message=message)
30
+ queue.extend(new_messages)
31
+
32
+
33
+ def _process_message(*, handler_list: list, message: [Command, Event]):
34
+ """
35
+ Handler to process messages of type "Command"
36
+ """
37
+ logger = get_logger()
38
+ messages = []
39
+
40
+ with transaction.atomic():
41
+ for handler in handler_list:
42
+ try:
43
+ logger.debug(
44
+ f"Handling command '{message.module_path()}' ({message.uuid}) with handler '{handler['name']}'."
45
+ )
46
+ module = importlib.import_module(handler["module"])
47
+ handler_function = getattr(module, handler["name"])
48
+ handler_messages = handler_function(context=message) or []
49
+ handler_messages = handler_messages if isinstance(handler_messages, list) else [handler_messages]
50
+ if len(handler_messages) > 0:
51
+ messages.extend(handler_messages)
52
+ uuid_list = [f"{m!s}" for m in handler_messages]
53
+ logger.debug(f"New messages: {uuid_list!s}")
54
+ except Exception as e:
55
+ logger.debug(f"Exception handling command {message.module_path()}: {e!s}")
56
+ raise e from e
57
+
58
+ return messages
queuebie/settings.py ADDED
@@ -0,0 +1,32 @@
1
+ from pathlib import Path
2
+
3
+ from django.conf import settings
4
+
5
+
6
+ def get_queuebie_app_base_path() -> Path | str:
7
+ """
8
+ Base path of the application queuebie should look for registered handlers.
9
+ :return:
10
+ """
11
+ return getattr(settings, "QUEUEBIE_APP_BASE_PATH", getattr(settings, "BASE_PATH", None))
12
+
13
+
14
+ def get_queuebie_cache_key() -> str:
15
+ """
16
+ Cache key to store registered handlers in.
17
+ """
18
+ return getattr(settings, "QUEUEBIE_CACHE_KEY", "queuebie")
19
+
20
+
21
+ def get_queuebie_logger_name() -> str:
22
+ """
23
+ Django logger name
24
+ """
25
+ return getattr(settings, "QUEUEBIE_LOGGER_NAME", "queuebie")
26
+
27
+
28
+ def get_queuebie_strict_mode() -> bool:
29
+ """
30
+ Determines if commands are allowed to be imported across Django app borders.
31
+ """
32
+ return getattr(settings, "QUEUEBIE_STRICT_MODE", True)
queuebie/utils.py ADDED
@@ -0,0 +1,30 @@
1
+ from collections.abc import Callable
2
+
3
+ from django.apps import apps
4
+
5
+
6
+ def is_part_of_app(*, function: Callable, class_type: type) -> bool:
7
+ """
8
+ Checks if a class belongs to the same Django app as the given function.
9
+ """
10
+
11
+ # Get the app configurations for the class and function
12
+ class_app_config = apps.get_containing_app_config(class_type.__module__)
13
+ function_app_config = apps.get_containing_app_config(function.__module__)
14
+
15
+ # Check if both belong to the same app
16
+ return class_app_config == function_app_config
17
+
18
+
19
+ def unique_append_to_inner_list(*, data: dict, key: str | int, value) -> dict:
20
+ """
21
+ Inserts "value" in the dictionary "data" on "key".
22
+ If "key" doesn't exist yet, it will create a new list containing "value".
23
+ If "value" at "key" already exists, it won't be appended.
24
+ """
25
+ if key not in data:
26
+ data[key] = [value]
27
+ elif value not in data[key]:
28
+ data[key].append(value)
29
+
30
+ return data