|
@@ -7,33 +7,27 @@ Here's an example of a periodic task:
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
- from celery.task import PeriodicTask
|
|
|
- from celery.registry import tasks
|
|
|
+ from celery.decorators import periodic_task
|
|
|
from datetime import timedelta
|
|
|
|
|
|
- class MyPeriodicTask(PeriodicTask):
|
|
|
- run_every = timedelta(seconds=30)
|
|
|
-
|
|
|
- def run(self, **kwargs):
|
|
|
- logger = self.get_logger(**kwargs)
|
|
|
- logger.info("Running periodic task!")
|
|
|
- >>> tasks.register(MyPeriodicTask)
|
|
|
+ @periodic_task(run_every=timedelta(seconds=30))
|
|
|
+ def every_30_seconds(\*\*kwargs):
|
|
|
+ logger = self.get_logger(\*\*kwargs)
|
|
|
+ logger.info("Running periodic task!")
|
|
|
|
|
|
If you want a little more control over when the task is executed, for example,
|
|
|
-a particular time of day or day of the week, you can use ``crontab`` to set
|
|
|
-the ``run_every`` property:
|
|
|
+a particular time of day or day of the week, you can use the ``crontab`` schedule
|
|
|
+type:
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
- from celery.task import PeriodicTask
|
|
|
from celery.task.schedules import crontab
|
|
|
+ from celery.decorators import periodic_task
|
|
|
|
|
|
- class EveryMondayMorningTask(PeriodicTask):
|
|
|
- run_every = crontab(hour=7, minute=30, day_of_week=1)
|
|
|
-
|
|
|
- def run(self, **kwargs):
|
|
|
- logger = self.get_logger(**kwargs)
|
|
|
- logger.info("Execute every Monday at 7:30AM.")
|
|
|
+ @periodoc_task(run_every=crontab(hour=7, minute=30, day_of_week=1))
|
|
|
+ def every_monday_morning(\*\*kwargs):
|
|
|
+ logger = self.get_logger(\*\*kwargs)
|
|
|
+ logger.info("Execute every Monday at 7:30AM.")
|
|
|
|
|
|
If you want to use periodic tasks you need to start the ``celerybeat``
|
|
|
service. You have to make sure only one instance of this server is running at
|
|
@@ -43,16 +37,7 @@ To start the ``celerybeat`` service::
|
|
|
|
|
|
$ celerybeat
|
|
|
|
|
|
-or if using Django::
|
|
|
-
|
|
|
- $ python manage.py celerybeat
|
|
|
-
|
|
|
-
|
|
|
You can also start ``celerybeat`` with ``celeryd`` by using the ``-B`` option,
|
|
|
this is convenient if you only have one server::
|
|
|
|
|
|
$ celeryd -B
|
|
|
-
|
|
|
-or if using Django::
|
|
|
-
|
|
|
- $ python manage.py celeryd -B
|