periodic-tasks.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. .. _guide-beat:
  2. ================
  3. Periodic Tasks
  4. ================
  5. .. contents::
  6. :local:
  7. Introduction
  8. ============
  9. :program:`celery beat` is a scheduler. It kicks off tasks at regular intervals,
  10. which are then executed by the 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-timezones:
  19. Time Zones
  20. ==========
  21. The periodic task schedules uses the UTC time zone by default,
  22. but you can change the time zone used using the :setting:`CELERY_TIMEZONE`
  23. setting.
  24. An example time zone could be `Europe/London`:
  25. .. code-block:: python
  26. CELERY_TIMEZONE = 'Europe/London'
  27. This setting must be added to your app, either by configuration it directly
  28. using (``app.conf.CELERY_TIMEZONE = 'Europe/London'``), or by adding
  29. it to your configuration module if you have set one up using
  30. ``app.config_from_object``. See :ref:`celerytut-configuration` for
  31. more information about configuration options.
  32. The default scheduler (storing the schedule in the :file:`celerybeat-schedule`
  33. file) will automatically detect that the time zone has changed, and so will
  34. reset the schedule itself, but other schedulers may not be so smart (e.g. the
  35. Django database scheduler, see below) and in that case you will have to reset the
  36. schedule manually.
  37. .. admonition:: Django Users
  38. Celery recommends and is compatible with the new ``USE_TZ`` setting introduced
  39. in Django 1.4.
  40. For Django users the time zone specified in the ``TIME_ZONE`` setting
  41. will be used, or you can specify a custom time zone for Celery alone
  42. by using the :setting:`CELERY_TIMEZONE` setting.
  43. The database scheduler will not reset when timezone related settings
  44. change, so you must do this manually:
  45. .. code-block:: bash
  46. $ python manage.py shell
  47. >>> from djcelery.models import PeriodicTask
  48. >>> PeriodicTask.objects.update(last_run_at=None)
  49. .. _beat-entries:
  50. Entries
  51. =======
  52. To call a task periodically you have to add an entry to the
  53. beat schedule list.
  54. .. code-block:: python
  55. from celery import Celery
  56. from celery.schedules import crontab
  57. app = Celery()
  58. @app.on_after_configure.connect
  59. def setup_periodic_tasks(sender, **kwargs):
  60. # Calls test('hello') every 10 seconds.
  61. sender.add_periodic_task(10.0, test.s('hello'), name='add every 10')
  62. # Calls test('world') every 30 seconds
  63. sender.add_periodic_task(30.0, test.s('world'), expires=10)
  64. # Executes every Monday morning at 7:30 A.M
  65. sender.add_periodic_task(
  66. crontab(hour=7, minute=30, day_of_week=1),
  67. test.s('Happy Mondays!'),
  68. )
  69. @app.task
  70. def test(arg):
  71. print(arg)
  72. Setting these up from within the ``on_after_configure`` handler means
  73. that we will not evaluate the app at module level when using ``test.s()``.
  74. The `@add_periodic_task` function will add the entry to the
  75. :setting:`CELERYBEAT_SCHEDULE` setting behind the scenes, which also
  76. can be used to set up periodic tasks manually:
  77. Example: Run the `tasks.add` task every 30 seconds.
  78. .. code-block:: python
  79. CELERYBEAT_SCHEDULE = {
  80. 'add-every-30-seconds': {
  81. 'task': 'tasks.add',
  82. 'schedule': 30.0,
  83. 'args': (16, 16)
  84. },
  85. }
  86. CELERY_TIMEZONE = 'UTC'
  87. .. note::
  88. If you are wondering where these settings should go then
  89. please see :ref:`celerytut-configuration`. You can either
  90. set these options on your app directly or you can keep
  91. a separate module for configuration.
  92. Using a :class:`~datetime.timedelta` for the schedule means the task will
  93. be sent in 30 second intervals (the first task will be sent 30 seconds
  94. after `celery beat` starts, and then every 30 seconds
  95. after the last run).
  96. A crontab like schedule also exists, see the section on `Crontab schedules`_.
  97. Like with ``cron``, the tasks may overlap if the first task does not complete
  98. before the next. If that is a concern you should use a locking
  99. strategy to ensure only one instance can run at a time (see for example
  100. :ref:`cookbook-task-serial`).
  101. .. _beat-entry-fields:
  102. Available Fields
  103. ----------------
  104. * `task`
  105. The name of the task to execute.
  106. * `schedule`
  107. The frequency of execution.
  108. This can be the number of seconds as an integer, a
  109. :class:`~datetime.timedelta`, or a :class:`~celery.schedules.crontab`.
  110. You can also define your own custom schedule types, by extending the
  111. interface of :class:`~celery.schedules.schedule`.
  112. * `args`
  113. Positional arguments (:class:`list` or :class:`tuple`).
  114. * `kwargs`
  115. Keyword arguments (:class:`dict`).
  116. * `options`
  117. Execution options (:class:`dict`).
  118. This can be any argument supported by
  119. :meth:`~celery.task.base.Task.apply_async`,
  120. e.g. `exchange`, `routing_key`, `expires`, and so on.
  121. * `relative`
  122. By default :class:`~datetime.timedelta` schedules are scheduled
  123. "by the clock". This means the frequency is rounded to the nearest
  124. second, minute, hour or day depending on the period of the timedelta.
  125. If `relative` is true the frequency is not rounded and will be
  126. relative to the time when :program:`celery beat` was started.
  127. .. _beat-crontab:
  128. Crontab schedules
  129. =================
  130. If you want more control over when the task is executed, for
  131. example, a particular time of day or day of the week, you can use
  132. the :class:`~celery.schedules.crontab` schedule type:
  133. .. code-block:: python
  134. from celery.schedules import crontab
  135. CELERYBEAT_SCHEDULE = {
  136. # Executes every Monday morning at 7:30 A.M
  137. 'add-every-monday-morning': {
  138. 'task': 'tasks.add',
  139. 'schedule': crontab(hour=7, minute=30, day_of_week=1),
  140. 'args': (16, 16),
  141. },
  142. }
  143. The syntax of these crontab expressions are very flexible. Some examples:
  144. +-----------------------------------------+--------------------------------------------+
  145. | **Example** | **Meaning** |
  146. +-----------------------------------------+--------------------------------------------+
  147. | ``crontab()`` | Execute every minute. |
  148. +-----------------------------------------+--------------------------------------------+
  149. | ``crontab(minute=0, hour=0)`` | Execute daily at midnight. |
  150. +-----------------------------------------+--------------------------------------------+
  151. | ``crontab(minute=0, hour='*/3')`` | Execute every three hours: |
  152. | | midnight, 3am, 6am, 9am, |
  153. | | noon, 3pm, 6pm, 9pm. |
  154. +-----------------------------------------+--------------------------------------------+
  155. | ``crontab(minute=0,`` | Same as previous. |
  156. | ``hour='0,3,6,9,12,15,18,21')`` | |
  157. +-----------------------------------------+--------------------------------------------+
  158. | ``crontab(minute='*/15')`` | Execute every 15 minutes. |
  159. +-----------------------------------------+--------------------------------------------+
  160. | ``crontab(day_of_week='sunday')`` | Execute every minute (!) at Sundays. |
  161. +-----------------------------------------+--------------------------------------------+
  162. | ``crontab(minute='*',`` | Same as previous. |
  163. | ``hour='*',`` | |
  164. | ``day_of_week='sun')`` | |
  165. +-----------------------------------------+--------------------------------------------+
  166. | ``crontab(minute='*/10',`` | Execute every ten minutes, but only |
  167. | ``hour='3,17,22',`` | between 3-4 am, 5-6 pm and 10-11 pm on |
  168. | ``day_of_week='thu,fri')`` | Thursdays or Fridays. |
  169. +-----------------------------------------+--------------------------------------------+
  170. | ``crontab(minute=0, hour='*/2,*/3')`` | Execute every even hour, and every hour |
  171. | | divisible by three. This means: |
  172. | | at every hour *except*: 1am, |
  173. | | 5am, 7am, 11am, 1pm, 5pm, 7pm, |
  174. | | 11pm |
  175. +-----------------------------------------+--------------------------------------------+
  176. | ``crontab(minute=0, hour='*/5')`` | Execute hour divisible by 5. This means |
  177. | | that it is triggered at 3pm, not 5pm |
  178. | | (since 3pm equals the 24-hour clock |
  179. | | value of "15", which is divisible by 5). |
  180. +-----------------------------------------+--------------------------------------------+
  181. | ``crontab(minute=0, hour='*/3,8-17')`` | Execute every hour divisible by 3, and |
  182. | | every hour during office hours (8am-5pm). |
  183. +-----------------------------------------+--------------------------------------------+
  184. | ``crontab(0, 0, day_of_month='2')`` | Execute on the second day of every month. |
  185. | | |
  186. +-----------------------------------------+--------------------------------------------+
  187. | ``crontab(0, 0,`` | Execute on every even numbered day. |
  188. | ``day_of_month='2-30/3')`` | |
  189. +-----------------------------------------+--------------------------------------------+
  190. | ``crontab(0, 0,`` | Execute on the first and third weeks of |
  191. | ``day_of_month='1-7,15-21')`` | the month. |
  192. +-----------------------------------------+--------------------------------------------+
  193. | ``crontab(0, 0, day_of_month='11',`` | Execute on 11th of May every year. |
  194. | ``month_of_year='5')`` | |
  195. +-----------------------------------------+--------------------------------------------+
  196. | ``crontab(0, 0,`` | Execute on the first month of every |
  197. | ``month_of_year='*/3')`` | quarter. |
  198. +-----------------------------------------+--------------------------------------------+
  199. See :class:`celery.schedules.crontab` for more documentation.
  200. .. _beat-solar:
  201. Solar schedules
  202. =================
  203. If you have a task that should be executed according to sunrise,
  204. sunset, dawn or dusk, you can use the
  205. :class:`~celery.schedules.solar` schedule type:
  206. .. code-block:: python
  207. from celery.schedules import solar
  208. CELERYBEAT_SCHEDULE = {
  209. # Executes at sunset in Melbourne
  210. 'add-at-melbourne-sunset': {
  211. 'task': 'tasks.add',
  212. 'schedule': solar('sunset', -37.81753, 144.96715),
  213. 'args': (16, 16),
  214. },
  215. }
  216. The arguments are simply: ``solar(event, latitude, longitude)``
  217. Be sure to use the correct sign for latitude and longitude:
  218. +---------------+-------------------+----------------------+
  219. | **Sign** | **Argument** | **Meaning** |
  220. +---------------+-------------------+----------------------+
  221. | ``+`` | ``latitude`` | North |
  222. +---------------+-------------------+----------------------+
  223. | ``-`` | ``latitude`` | South |
  224. +---------------+-------------------+----------------------+
  225. | ``+`` | ``longitude`` | East |
  226. +---------------+-------------------+----------------------+
  227. | ``-`` | ``longitude`` | West |
  228. +---------------+-------------------+----------------------+
  229. Possible event types are:
  230. +-----------------------------------------+--------------------------------------------+
  231. | **Event** | **Meaning** |
  232. +-----------------------------------------+--------------------------------------------+
  233. | ``dawn_astronomical`` | Execute at the moment after which the sky |
  234. | | is no longer completely dark. This is when |
  235. | | the sun is 18 degrees below the horizon. |
  236. +-----------------------------------------+--------------------------------------------+
  237. | ``dawn_nautical`` | Execute when there is enough sunlight for |
  238. | | the horizon and some objects to be |
  239. | | distinguishable; formally, when the sun is |
  240. | | 12 degrees below the horizon. |
  241. +-----------------------------------------+--------------------------------------------+
  242. | ``dawn_civil`` | Execute when there is enough light for |
  243. | | objects to be distinguishable so that |
  244. | | outdoor activities can commence; |
  245. | | formally, when the Sun is 6 degrees below |
  246. | | the horizon. |
  247. +-----------------------------------------+--------------------------------------------+
  248. | ``sunrise`` | Execute when the upper edge of the sun |
  249. | | appears over the eastern horizon in the |
  250. | | morning. |
  251. +-----------------------------------------+--------------------------------------------+
  252. | ``solar_noon`` | Execute when the sun is highest above the |
  253. | | horizon on that day. |
  254. +-----------------------------------------+--------------------------------------------+
  255. | ``sunset`` | Execute when the trailing edge of the sun |
  256. | | disappears over the western horizon in the |
  257. | | evening. |
  258. +-----------------------------------------+--------------------------------------------+
  259. | ``dusk_civil`` | Execute at the end of civil twilight, when |
  260. | | objects are still distinguishable and some |
  261. | | stars and planets are visible. Formally, |
  262. | | when the sun is 6 degrees below the |
  263. | | horizon. |
  264. +-----------------------------------------+--------------------------------------------+
  265. | ``dusk_nautical`` | Execute when the sun is 12 degrees below |
  266. | | the horizon. Objects are no longer |
  267. | | distinguishable, and the horizon is no |
  268. | | longer visible to the naked eye. |
  269. +-----------------------------------------+--------------------------------------------+
  270. | ``dusk_astronomical`` | Execute at the moment after which the sky |
  271. | | becomes completely dark; formally, when |
  272. | | the sun is 18 degrees below the horizon. |
  273. +-----------------------------------------+--------------------------------------------+
  274. All solar events are calculated using UTC, and are therefore
  275. unaffected by your timezone setting.
  276. In polar regions, the sun may not rise or set every day. The scheduler
  277. is able to handle these cases, i.e. a ``sunrise`` event won't run on a day
  278. when the sun doesn't rise. The one exception is ``solar_noon``, which is
  279. formally defined as the moment the sun transits the celestial meridian,
  280. and will occur every day even if the sun is below the horizon.
  281. Twilight is defined as the period between dawn and sunrise, and between
  282. sunset and dusk. You can schedule an event according to "twilight"
  283. depending on your definition of twilight (civil, nautical or astronomical),
  284. and whether you want the event to take place at the beginning or end
  285. of twilight, using the appropriate event from the list above.
  286. See :class:`celery.schedules.solar` for more documentation.
  287. .. _beat-starting:
  288. Starting the Scheduler
  289. ======================
  290. To start the :program:`celery beat` service:
  291. .. code-block:: bash
  292. $ celery -A proj beat
  293. You can also start embed `beat` inside the worker by enabling
  294. workers `-B` option, this is convenient if you will never run
  295. more than one worker node, but it's not commonly used and for that
  296. reason is not recommended for production use:
  297. .. code-block:: bash
  298. $ celery -A proj worker -B
  299. Beat needs to store the last run times of the tasks in a local database
  300. file (named `celerybeat-schedule` by default), so it needs access to
  301. write in the current directory, or alternatively you can specify a custom
  302. location for this file:
  303. .. code-block:: bash
  304. $ celery -A proj beat -s /home/celery/var/run/celerybeat-schedule
  305. .. note::
  306. To daemonize beat see :ref:`daemonizing`.
  307. .. _beat-custom-schedulers:
  308. Using custom scheduler classes
  309. ------------------------------
  310. Custom scheduler classes can be specified on the command-line (the `-S`
  311. argument). The default scheduler is :class:`celery.beat.PersistentScheduler`,
  312. which is simply keeping track of the last run times in a local database file
  313. (a :mod:`shelve`).
  314. `django-celery` also ships with a scheduler that stores the schedule in the
  315. Django database:
  316. .. code-block:: bash
  317. $ celery -A proj beat -S djcelery.schedulers.DatabaseScheduler
  318. Using `django-celery`'s scheduler you can add, modify and remove periodic
  319. tasks from the Django Admin.