periodic-tasks.rst 7.4 KB

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