Skip to main content

Framework to create and process triggers.

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-triggers-2.1.5.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

django_triggers-2.1.5-py2.py3-none-any.whl (21.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-triggers-2.1.5.tar.gz.

File metadata

  • Download URL: django-triggers-2.1.5.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-triggers-2.1.5.tar.gz
Algorithm Hash digest
SHA256 99c772e8f62b81a4fb9c0f47eac09b83fb506f06dc7ae9a42802c49ebfa22d2b
MD5 7291388ab0fb13abc2fb1d1d7bc572cc
BLAKE2b-256 d6301e187e3669982464ebdc269faf2ef8d307dc8d3ea859d712fc4ca606c3be

See more details on using hashes here.

File details

Details for the file django_triggers-2.1.5-py2.py3-none-any.whl.

File metadata

  • Download URL: django_triggers-2.1.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for django_triggers-2.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 158af383c56ac55b22e11630570fec399ba166580ee20882493db6ca9d5fb357
MD5 505d9e1daf79d5a4d8bf66f9c4fd079f
BLAKE2b-256 82f4baaa21abbfa3dce4e1f81bc08f2cfc9505f159e9123f91f781c2aec0b080

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page