periodic-tasks.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ================
  2. Periodic Tasks
  3. ================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. Celerybeat is a scheduler. It kicks off tasks at regular intervals,
  9. which are then executed by worker nodes available in the cluster.
  10. By default the entries are taken from the ``CELERYBEAT_SCHEDULE`` setting,
  11. but custom stores can also be used, like storing the entries
  12. in an SQL database.
  13. You have to ensure only a single scheduler is running for a schedule
  14. at a time, otherwise you would end up with duplicate tasks. Using
  15. a centralized approach means the schedule does not have to be synchronized,
  16. and the service can operate without using locks.
  17. Entries
  18. =======
  19. To schedule a task periodically you have to add an entry to the
  20. ``CELERYBEAT_SCHEDULE`` setting:
  21. .. code-block:: python
  22. from datetime import timedelta
  23. CELERYBEAT_SCHEDULE = {
  24. "runs-every-30-seconds": {
  25. "task": "tasks.add",
  26. "schedule": timedelta(seconds=30),
  27. "args": (16, 16)
  28. },
  29. }
  30. Here we run the ``tasks.add`` task every 30 seconds.
  31. Using a :class:`~datetime.timedelta` means the task will be executed
  32. 30 seconds after ``celerybeat`` starts, and then every 30 seconds
  33. after the last run. A crontab like schedule also exists, see the section
  34. on `Crontab schedules`_.
  35. Available Fields
  36. ----------------
  37. * ``task``
  38. The name of the task to execute.
  39. * ``schedule``
  40. The frequency of execution.
  41. This can be the number of seconds as an integer, a
  42. :class:`~datetime.timedelta`, or a :class:`~celery.schedules.crontab`.
  43. You can also define your own custom schedule types, just make sure
  44. it supports the :class:`~celery.schedules.schedule` interface.
  45. * ``args``
  46. Positional arguments (:class:`list` or :class:`tuple`).
  47. * ``kwargs``
  48. Keyword arguments (:class:`dict`).
  49. * ``options``
  50. Execution options (:class:`dict`).
  51. This can be any argument supported by :meth:`~celery.execute.apply_async`,
  52. e.g. ``exchange``, ``routing_key``, ``expires``, and so on.
  53. * ``relative``
  54. By default :class:`~datetime.timedelta` schedules are scheduled
  55. "by the clock". This means the frequency is rounded to the nearest
  56. second, minute, hour or day depending on the period of the timedelta.
  57. If ``relative`` is true the frequency is not rounded and will be
  58. relative to the time ``celerybeat`` was started.
  59. Crontab schedules
  60. =================
  61. If you want more control over when the task is executed, for
  62. example, a particular time of day or day of the week, you can use
  63. the ``crontab`` schedule type:
  64. .. code-block:: python
  65. from celery.schedules import crontab
  66. CELERYBEAT_SCHEDULE = {
  67. # Executes every monday morning at 7:30 A.M
  68. "every-monday-morning": {
  69. "task": "tasks.add",
  70. "schedule": crontab(hour=7, minute=30, day_of_week=1),
  71. "args": (16, 16),
  72. },
  73. }
  74. The syntax of these crontab expressions are very flexible. Some examples:
  75. +-------------------------------------+--------------------------------------------+
  76. | **Example** | **Meaning** |
  77. +-------------------------------------+--------------------------------------------+
  78. | crontab() | Execute every minute. |
  79. +-------------------------------------+--------------------------------------------+
  80. | crontab(minute=0, hour=0) | Execute daily at midnight. |
  81. +-------------------------------------+--------------------------------------------+
  82. | crontab(minute=0, | Execute every three hours---at midnight, |
  83. | | 3am, 6am, 9am, noon, 3pm, 6pm, 9pm. |
  84. +-------------------------------------+--------------------------------------------+
  85. | crontab(minute=0, | Same as previous. |
  86. | hour=[0,3,6,9,12,15,18,21]) | |
  87. +-------------------------------------+--------------------------------------------+
  88. | crontab(minute="\*/15") | Execute every 15 minutes. |
  89. +-------------------------------------+--------------------------------------------+
  90. | crontab(day_of_week="sunday") | Execute every minute (!) at sundays. |
  91. +-------------------------------------+--------------------------------------------+
  92. | crontab(minute="*", | Same as previous. |
  93. | hour="*", | |
  94. | day_of_week="sun") | |
  95. +-------------------------------------+--------------------------------------------+
  96. | crontab(minute="\*/10", | Execute every ten minutes, but only |
  97. | hour="3,17,22", | between 3-4 am, 5-6 pm and 10-11 pm on |
  98. | day_of_week="thu,fri") | thursdays or fridays. |
  99. +-------------------------------------+--------------------------------------------+
  100. | crontab(minute=0, hour="\*/2,\*/3") | Execute every even hour, and every hour |
  101. | | divisable by three. This means: |
  102. | | at every hour *except*: 1am, |
  103. | | 5am, 7am, 11am, 1pm, 5pm, 7pm, |
  104. | | 11pm |
  105. +-------------------------------------+--------------------------------------------+
  106. | crontab(minute=0, hour="\*/5") | Execute hour divisable by 5. This means |
  107. | | that it is triggered at 3pm, not 5pm |
  108. | | (since 3pm equals the 24-hour clock |
  109. | | value of "15", which is divisable by 5). |
  110. +-------------------------------------+--------------------------------------------+
  111. | crontab(minute=0, hour="\*/3,8-17") | Execute every hour divisable by 3, and |
  112. | | every hour during office hours (8am-5pm). |
  113. +-------------------------------------+--------------------------------------------+
  114. Starting celerybeat
  115. ===================
  116. To start the ``celerybeat`` service::
  117. $ celerybeat
  118. You can also start ``celerybeat`` with ``celeryd`` by using the ``-B`` option,
  119. this is convenient if you only intend to use one worker node::
  120. $ celeryd -B
  121. Celerybeat needs to store the last run times of the tasks in a local database
  122. file (named ``celerybeat-schedule`` by default), so you need access to
  123. write to the current directory, or alternatively you can specify a custom
  124. location for this file::
  125. $ celerybeat -s /home/celery/var/run/celerybeat-schedule
  126. Using custom scheduler classes
  127. ------------------------------
  128. Custom scheduler classes can be specified on the command line (the ``-S``
  129. argument). The default scheduler is :class:`celery.beat.PersistentScheduler`,
  130. which is simply keeping track of the last run times in a local database file
  131. (a :mod:`shelve`).
  132. ``django-celery`` also ships with a scheduler that stores the schedule in a
  133. database::
  134. $ celerybeat -S djcelery.schedulers.DatabaseScheduler
  135. Using ``django-celery``'s scheduler you can add, modify and remove periodic
  136. tasks from the Django Admin.