Giter Site home page Giter Site logo

jcbalmeida / django-celery-beat Goto Github PK

View Code? Open in Web Editor NEW

This project forked from celery/django-celery-beat

0.0 2.0 0.0 152 KB

Celery Periodic Tasks backed by the Django ORM

License: Other

Makefile 4.92% Python 88.12% HTML 0.94% PowerShell 3.63% Batchfile 2.40%

django-celery-beat's Introduction

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.0

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

Warning

If you change the Django TIME_ZONE setting your periodic task schedule will still be based on the old timezone.

To fix that you would have to reset the "last run time" for each periodic task:

>>> from django_celery_beat.models import PeriodicTask, PeriodicTasks
>>> PeriodicTask.objects.all().update(last_run_at=None)
>>> for task in PeriodicTask.objects.all():
>>>     PeriodicTasks.changed(task)

Note that this will reset the state as if the periodic tasks have never run before.

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

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 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 a30 * * * *(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 ofinterval=schedule, specifycrontab=schedule:: >>> PeriodicTask.objects.create( ... crontab=schedule, ... name='Importing contacts', ... task='proj.tasks.import_contacts', ... ) Temporarily disable a periodic task ----------------------------------- You can use theenabled`` 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

django-celery-beat's People

Contributors

ask avatar auvipy avatar bigmars86 avatar bradshjg avatar ddevine avatar hmphu avatar mapleflow avatar mbaechtold avatar megwill4268 avatar mheppner avatar millin avatar ransford avatar sobolevn avatar t0neg avatar tarkatronic avatar thedrow avatar vstoykov avatar wardal avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.