diff --git a/README.md b/README.md index f2062a2..7311f92 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,15 @@ The wildcard detection will ignore all sub modules from that point on. You will need to ignore the module itself if you don't want the `__init__.py` to be processed. +### Actors to write to the database + +You can specify which actors are allowed or blocked from writing to the database using the following settings: + +``` python +DJANGO_DRAMATIQ_TASKS_BLOCKLIST = ['actor_name_that_excluded'] +DJANGO_DRAMATIQ_TASKS_ALLOWLIST = ['actor_name_that_writes_only1', 'actor_name_that_writes_only2'] +``` + ### Testing You should have a separate settings file for test. In that file, diff --git a/django_dramatiq/models.py b/django_dramatiq/models.py index 5a2f19b..4150abd 100644 --- a/django_dramatiq/models.py +++ b/django_dramatiq/models.py @@ -1,5 +1,7 @@ from datetime import timedelta +from typing import Optional +from django.conf import settings from django.db import models from django.utils.functional import cached_property from django.utils.timezone import now @@ -10,9 +12,18 @@ #: The database label to use when storing task metadata. DATABASE_LABEL = DjangoDramatiqConfig.tasks_database() +DJANGO_DRAMATIQ_TASKS_BLOCKLIST = getattr(settings, "DJANGO_DRAMATIQ_TASKS_BLOCKLIST", []) +DJANGO_DRAMATIQ_TASKS_ALLOWLIST = getattr(settings, "DJANGO_DRAMATIQ_TASKS_ALLOWLIST", []) + class TaskManager(models.Manager): def create_or_update_from_message(self, message, **extra_fields): + + if DJANGO_DRAMATIQ_TASKS_ALLOWLIST and message.actor_name not in DJANGO_DRAMATIQ_TASKS_ALLOWLIST: + return None + if DJANGO_DRAMATIQ_TASKS_BLOCKLIST and message.actor_name in DJANGO_DRAMATIQ_TASKS_BLOCKLIST: + return None + task, _ = self.using(DATABASE_LABEL).update_or_create( id=message.message_id, defaults={