Skip to main content

Database-backed Periodic Tasks

Build status coverage BSD License django-celery-beat can be installed via wheel Supported Python versions. Support Python implementations.

Version:

1.1.1

Web:

http://django-celery-beat.readthedocs.io/

Download:

http://pypi.python.org/pypi/django-celery-beat

Source:

http://github.com/celery/django-celery-beat

Keywords:

django, celery, beat, periodic task, cron, scheduling

About

This extension enables you to store the periodic task schedule in the database.

The periodic tasks can be managed from the Django Admin interface, where you can create, edit and delete periodic tasks and how often they should run.

Using the Extension

Usage and installation instructions for this extension are available from the Celery documentation:

http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#using-custom-scheduler-classes

Important Warning about Time Zones

Models

  • django_celery_beat.models.PeriodicTask

This model defines a single periodic task to be run.

It must be associated with a schedule, which defines how often the task should run.

  • django_celery_beat.models.IntervalSchedule

A schedule that runs at a specific interval (e.g. every 5 seconds).

  • django_celery_beat.models.CrontabSchedule

A schedule with fields like entries in cron: minute hour day-of-week day_of_month month_of_year.

  • django_celery_beat.models.PeriodicTasks

This model is only used as an index to keep track of when the schedule has changed.

Whenever you update a PeriodicTask a counter in this table is also incremented, which tells the celery beat service to reload the schedule from the database.

If you update periodic tasks in bulk, you will need to update the counter manually:

>>> from django_celery_beat.models import PeriodicTasks
>>> PeriodicTasks.changed()

Example creating interval-based periodic task

To create a periodic task executing at an interval you must first create the interval object:

>>> from django_celery_beat.models import PeriodicTask, IntervalSchedule

# executes every 10 seconds.
>>> schedule, created = IntervalSchedule.objects.get_or_create(
...     every=10,
...     period=IntervalSchedule.SECONDS,
... )

That’s all the fields you need: a period type and the frequency.

You can choose between a specific set of periods:

  • IntervalSchedule.DAYS

  • IntervalSchedule.HOURS

  • IntervalSchedule.MINUTES

  • IntervalSchedule.SECONDS

  • IntervalSchedule.MICROSECONDS

There’s also a “choices tuple” available should you need to present this to the user:

>>> IntervalSchedule.PERIOD_CHOICES

Now that we have defined the schedule object, we can create the periodic task entry:

>>> PeriodicTask.objects.create(
...     interval=schedule,                  # we created this above.
...     name='Importing contacts',          # simply describes this periodic task.
...     task='proj.tasks.import_contacts',  # name of task.
... )

Note that this is a very basic example, you can also specify the arguments and keyword arguments used to execute the task, the queue to send it to[*], and set an expiry time.

Here’s an example specifying the arguments, note how JSON serialization is required:

>>> import json
>>> from datetime import datetime, timedelta

>>> PeriodicTask.objects.create(
...     interval=schedule,                  # we created this above.
...     name='Importing contacts',          # simply describes this periodic task.
...     task='proj.tasks.import_contacts',  # name of task.
...     args=json.dumps(['arg1', 'arg2']),
...     kwargs=json.dumps({
...        'be_careful': True,
...     }),
...     expires=datetime.utcnow() + timedelta(seconds=30)
... )

Example creating crontab-based periodic task

A crontab schedule has the fields: minute, hour, day_of_week, day_of_month and month_of_year`, so if you want the equivalent of a ``30 * * * * (execute every 30 minutes) crontab entry you specify:

>>> from django_celery_beat.models import CrontabSchedule, PeriodicTask
>>> schedule, _ = CrontabSchedule.objects.get_or_create(
...     minute='30',
...     hour='*',
...     day_of_week='*',
...     day_of_month='*',
...     month_of_year='*',
... )

Then to create a periodic task using this schedule, use the same approach as the interval-based periodic task earlier in this document, but instead of interval=schedule, specify crontab=schedule:

>>> PeriodicTask.objects.create(
...     crontab=schedule,
...     name='Importing contacts',
...     task='proj.tasks.import_contacts',
... )

Temporarily disable a periodic task

You can use the enabled flag to temporarily disable a periodic task:

>>> periodic_task.enabled = False
>>> periodic_task.save()

Installation

You can install django-celery-beat either via the Python Package Index (PyPI) or from source.

To install using pip,:

$ pip install -U django-celery-beat

Downloading and installing from source

Download the latest version of django-celery-beat from http://pypi.python.org/pypi/django-celery-beat

You can install it by doing the following,:

$ tar xvfz django-celery-beat-0.0.0.tar.gz
$ cd django-celery-beat-0.0.0
$ python setup.py build
# python setup.py install

The last command must be executed as a privileged user if you are not currently using a virtualenv.

Using the development version

With pip

You can install the latest snapshot of django-celery-beat using the following pip command:

$ pip install https://github.com/celery/django-celery-beat/zipball/master#egg=django-celery-beat

Metadata

Release files for django-celery-beat 1.1.1

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-celery-beat 1.1.1
File Size Uploaded
django-celery-beat-1.1.1.tar.gz 65.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-celery-beat 1.1.1
File Interpreter ABI Platform
django_celery_beat-1.1.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 89.3 kB

Release files / django-celery-beat-1.1.1.tar.gz

Download URL django-celery-beat-1.1.1.tar.gz
Size 65.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d40e2c48aeed7043fff6064f53bc157f6a1f55b45d984eb2b52bf1b88ae96026
BLAKE2b-256 checksum
How to use checksums
340ff73273e993f347712d6796df3259e3efb0939ef7bb7cb95ba2a060f3ced4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_celery_beat-1.1.1-py2.py3-none-any.whl

Download URL django_celery_beat-1.1.1-py2.py3-none-any.whl
Size 23.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1f770a936f070fd6b2ceac123ef4951fbe9e941e39b52ae9eea5ac0efe57d51d
BLAKE2b-256 checksum
How to use checksums
454b4d9dfd63ebd119e7340526d023248896626e9454edba981b331858b1b5bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

2.9.0

2 release files

2.8.1

2 release files

2.8.0

2 release files

2.7.0

2 release files

2.6.0

1 release file

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

1 release file

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