Skip to main content

Coverage Status CI Status

About

Django Triggers is a light-weight framework for having one part of an application generate a trigger while another part responds to to it. Triggers are persistent and can be scheduled to be processed at a later time.

Usage

Triggers are defined by subclassing the Trigger model. Trigger defines common data structures and logic for all child triggers. The only thing a child should have to do is override the _process method and set typed to a unique slug.

Settings

The following settings are used:

  • DJTRIGGERS_TRIES_BEFORE_WARNING: the number of times a task can be retried before a warning is logged. Defaults to 3.
  • DJTRIGGERS_TRIES_BEFORE_ERROR: the number of times a task can be retried before an error is raised. Defaults to 5.
  • DJTRIGGERS_ASYNC_HANDLING: whether processing should be asynchronous (using Celery) or not. Default to False.
  • DJTRIGGERS_CELERY_TASK_MAX_RETRIES: the number of times the Celery task for a trigger should be retried. Defaults to 0.
  • DJTRIGGERS_TYPE_TO_TABLE: mapping of trigger types to database tables. Used for the cleanup script. Defaults to {}.
  • DJTRIGGERS_REDIS_URL: the URL of the Redis instance used for locks.
  • DJTRIGGERS_LOGGERS: separate logging config for django-triggers. Defaults to ().

Examples

General use

from djtriggers.models import Trigger

class BreakfastTrigger(Trigger):
    class Meta:
        # There is no trigger specific data so make a proxy model.
        # This ensures no additional db table is created.
        proxy = True
    typed = 'breakfast'

    def _process(self, dictionary={}):
        prepare_toast()
        prepare_juice()
        eat()

Trigger specific data

from djtriggers.models import Trigger

class PayBill(Trigger):
    class Meta:
        # We need a regular model as the trigger specific data needs a
        # place to live in the db.
        proxy = False

    amount = models.IntegerField()
    recipient = models.ForeignKey(User)

    def _process(self, dictionary={}):
        amount = self.amount
        recipient = self.recipient
        check_balance()
        pay_bill(amount, recipient)

Trigger processing

from .models import BreakfastTrigger
from .exceptions import ProcessError

trigger = BreakfastTrigger.objects.get(pk=1)
try:
    trigger.process()
except ProcessError as e:
    report_error(e)

Delayed processing

from .models import BreakfastTrigger

trigger = BreakfastTrigger()
# Process 8 hours later (this can be any datetime)
trigger.process_after = now() + timedelta(hour=8)

Metadata

Release files for django-triggers 2.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-triggers 2.1.5
File Size Uploaded
django-triggers-2.1.5.tar.gz 11.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-triggers 2.1.5
File Interpreter ABI Platform
django_triggers-2.1.5-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 33.2 kB

Release files / django-triggers-2.1.5.tar.gz

Download URL django-triggers-2.1.5.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
99c772e8f62b81a4fb9c0f47eac09b83fb506f06dc7ae9a42802c49ebfa22d2b
BLAKE2b-256 checksum
How to use checksums
d6301e187e3669982464ebdc269faf2ef8d307dc8d3ea859d712fc4ca606c3be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.5

Release files / django_triggers-2.1.5-py2.py3-none-any.whl

Download URL django_triggers-2.1.5-py2.py3-none-any.whl
Size 21.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
158af383c56ac55b22e11630570fec399ba166580ee20882493db6ca9d5fb357
BLAKE2b-256 checksum
How to use checksums
82f4baaa21abbfa3dce4e1f81bc08f2cfc9505f159e9123f91f781c2aec0b080
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.20.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

2.1.5 This release

2 release files

2.1.4

1 release file

2.1.1

2 release files

2.1.0

2 release files

2.0.13

2 release files

2.0.8

2 release files

2.0.7

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

3 release files

2.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page