- Notifications
You must be signed in to change notification settings - Fork475
Celery Periodic Tasks backed by the Django ORM
License
celery/django-celery-beat
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
| Version: | 2.8.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 |
| DeepWiki: | ![]() |
| Keywords: | django, celery, beat, periodic task, cron, scheduling |
This extension enables you to store the periodic task schedule in thedatabase.
The periodic tasks can be managed from the Django Admin interface, where youcan create, edit and delete periodic tasks and how often they should run.
Usage and installation instructions for this extension are availablefrom theCelery documentation.
Warning
If you change the DjangoTIME_ZONE setting your periodic task schedulewill still be based on the old timezone.
To fix that you would have to reset the "last run time" for each periodic task:
>>>fromdjango_celery_beat.modelsimportPeriodicTask,PeriodicTasks>>>PeriodicTask.objects.all().update(last_run_at=None)>>>PeriodicTasks.update_changed()
Note
This will reset the state as if the periodic tasks have never run before.
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 shouldrun.
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 haschanged.
Whenever you update aPeriodicTask a counter in this table is alsoincremented, which tells thecelery beat service to reload the schedulefrom the database.
If you update periodic tasks in bulk, you will need to update the countermanually:
>>>fromdjango_celery_beat.modelsimportPeriodicTasks>>>PeriodicTasks.update_changed()
To create a periodic task executing at an interval you must firstcreate the interval object:
>>>fromdjango_celery_beat.modelsimportPeriodicTask,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.DAYSIntervalSchedule.HOURSIntervalSchedule.MINUTESIntervalSchedule.SECONDSIntervalSchedule.MICROSECONDS
Note
If you have multiple periodic tasks executing every 10 seconds,then they should all point to the same schedule object.
There's also a "choices tuple" available should you need to present thisto the user:
>>>IntervalSchedule.PERIOD_CHOICES
Now that we have defined the schedule object, we can create the periodic taskentry:
>>>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 argumentsand keyword arguments used to execute the task, thequeue to send itto[*], and set an expiry time.
Here's an example specifying the arguments, note how JSON serialization isrequired:
>>>importjson>>>fromdatetimeimportdatetime,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)... )
| [*] | you can also use low-level AMQP routing using theexchange androuting_key fields. |
A crontab schedule has the fields:minute,hour,day_of_week,day_of_month andmonth_of_year, so if you want the equivalentof a30 * * * * (execute 30 minutes past every hour) crontab entry you specify:
>>>fromdjango_celery_beat.modelsimportCrontabSchedule,PeriodicTask>>>schedule,_=CrontabSchedule.objects.get_or_create(...minute='30',...hour='*',...day_of_week='*',...day_of_month='*',...month_of_year='*',...timezone=zoneinfo.ZoneInfo('Canada/Pacific')... )
The crontab schedule is linked to a specific timezone using the 'timezone' input parameter.
Then to create a periodic task using this schedule, use the same approach asthe interval-based periodic task earlier in this document, but insteadofinterval=schedule, specifycrontab=schedule:
>>>PeriodicTask.objects.create(...crontab=schedule,...name='Importing contacts',...task='proj.tasks.import_contacts',... )
You can use theenabled flag to temporarily disable a periodic task:
>>>periodic_task.enabled=False>>>periodic_task.save()
The periodic tasks still need 'workers' to execute them.So make sure the defaultCelery package is installed.(If not installed, please follow the installation instructionshere:https://github.com/celery/celery)
Both the worker and beat services need to be running at the same time.
Start a Celery worker service (specify your Django project name):
$ celery -A [project-name] worker --loglevel=info
As a separate process, start the beat service (specify the Django scheduler):
$ celery -A [project-name] beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
OR you can use the -S (scheduler flag), for more options see
celery beat --help):$ celery -A [project-name] beat -l info -S django
Also, as an alternative, you can run the two steps above (worker and beat services)with only one command (recommended fordevelopment environment only):
$ celery -A [project-name] worker --beat --scheduler django --loglevel=info
Now you can add and manage your periodic tasks from the Django Admin interface.
You can install django-celery-beat either via the Python Package Index (PyPI)or from source.
To install usingpip:
$ pip install --upgrade django-celery-beat
Download the latest version of django-celery-beat fromhttp://pypi.python.org/pypi/django-celery-beat
You can install it by doing the following :
$ python3 -m venv .venv$source .venv/bin/activate$ pip install --upgrade build pip$ tar xvfz django-celery-beat-0.0.0.tar.gz$cd django-celery-beat-0.0.0$ python -m build$ pip install --upgrade.
After installation, adddjango_celery_beat to Django's settings module:
INSTALLED_APPS= [ ...,'django_celery_beat',]
Run thedjango_celery_beat migrations using:
$ python manage.py migrate django_celery_beat
You can install the latest main version of django-celery-beat using the followingpip command:
$ pip install git+https://github.com/celery/django-celery-beat#egg=django-celery-beat
To spin up a local development copy of django-celery-beat with Django admin athttp://127.0.0.1:58000/admin/ run:
$ docker-compose up --build
Log-in as useradmin with passwordadmin.
If you have a project that is time zone naive, you can setDJANGO_CELERY_BEAT_TZ_AWARE=False in your settings file.
The maintainers of django-celery-beat and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use.Learn more.
About
Celery Periodic Tasks backed by the Django ORM
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
