diff --git a/README.md b/README.md index f2062a2..6044e1a 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,13 @@ DRAMATIQ_RESULT_BACKEND = { } ``` +You can specify which actors to write to the database with "black" and "white" lists: + +``` python +DJANGO_DRAMATIQ_TASKS_NOT_WRITES = ['actor_name_that_excluded'] +DJANGO_DRAMATIQ_TASKS_WRITES_ONLY = ['actor_name_that_writes_only1', 'actor_name_that_writes_only2'] +``` + ## Usage diff --git a/django_dramatiq/models.py b/django_dramatiq/models.py index 5a2f19b..e65aea9 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,19 @@ #: The database label to use when storing task metadata. DATABASE_LABEL = DjangoDramatiqConfig.tasks_database() +DJANGO_DRAMATIQ_TASKS_NOT_WRITES = getattr(settings, "DJANGO_DRAMATIQ_TASKS_NOT_WRITES", []) +DJANGO_DRAMATIQ_TASKS_WRITES_ONLY = getattr(settings, "DJANGO_DRAMATIQ_TASKS_WRITES_ONLY", []) + class TaskManager(models.Manager): - def create_or_update_from_message(self, message, **extra_fields): + def create_or_update_from_message(self, message, **extra_fields) -> Optional['Task']: + + # black and write lists + if DJANGO_DRAMATIQ_TASKS_WRITES_ONLY and message.actor_name not in DJANGO_DRAMATIQ_TASKS_WRITES_ONLY: + return None + if DJANGO_DRAMATIQ_TASKS_NOT_WRITES and message.actor_name in DJANGO_DRAMATIQ_TASKS_NOT_WRITES: + return None + task, _ = self.using(DATABASE_LABEL).update_or_create( id=message.message_id, defaults={