beat.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. """The :program:`celery beat` command.
  3. .. program:: celery beat
  4. .. seealso::
  5. See :ref:`preload-options` and :ref:`daemon-options`.
  6. .. cmdoption:: --detach
  7. Detach and run in the background as a daemon.
  8. .. cmdoption:: -s, --schedule
  9. Path to the schedule database. Defaults to `celerybeat-schedule`.
  10. The extension '.db' may be appended to the filename.
  11. Default is {default}.
  12. .. cmdoption:: -S, --scheduler
  13. Scheduler class to use.
  14. Default is :class:`{default}`.
  15. .. cmdoption:: --max-interval
  16. Max seconds to sleep between schedule iterations.
  17. .. cmdoption:: -f, --logfile
  18. Path to log file. If no logfile is specified, `stderr` is used.
  19. .. cmdoption:: -l, --loglevel
  20. Logging level, choose between `DEBUG`, `INFO`, `WARNING`,
  21. `ERROR`, `CRITICAL`, or `FATAL`.
  22. .. cmdoption:: --pidfile
  23. File used to store the process pid. Defaults to `celerybeat.pid`.
  24. The program won't start if this file already exists
  25. and the pid is still alive.
  26. .. cmdoption:: --uid
  27. User id, or user name of the user to run as after detaching.
  28. .. cmdoption:: --gid
  29. Group id, or group name of the main group to change to after
  30. detaching.
  31. .. cmdoption:: --umask
  32. Effective umask (in octal) of the process after detaching. Inherits
  33. the umask of the parent process by default.
  34. .. cmdoption:: --workdir
  35. Optional directory to change to after detaching.
  36. .. cmdoption:: --executable
  37. Executable to use for the detached process.
  38. """
  39. from __future__ import absolute_import, unicode_literals
  40. from functools import partial
  41. from celery.platforms import detached, maybe_drop_privileges
  42. from celery.bin.base import Command, daemon_options
  43. __all__ = ('beat',)
  44. HELP = __doc__
  45. class beat(Command):
  46. """Start the beat periodic task scheduler.
  47. Examples:
  48. .. code-block:: console
  49. $ celery beat -l info
  50. $ celery beat -s /var/run/celery/beat-schedule --detach
  51. $ celery beat -S django
  52. The last example requires the :pypi:`django-celery-beat` extension
  53. package found on PyPI.
  54. """
  55. doc = HELP
  56. enable_config_from_cmdline = True
  57. supports_args = False
  58. def run(self, detach=False, logfile=None, pidfile=None, uid=None,
  59. gid=None, umask=None, workdir=None, **kwargs):
  60. if not detach:
  61. maybe_drop_privileges(uid=uid, gid=gid)
  62. kwargs.pop('app', None)
  63. beat = partial(self.app.Beat,
  64. logfile=logfile, pidfile=pidfile, **kwargs)
  65. if detach:
  66. with detached(logfile, pidfile, uid, gid, umask, workdir):
  67. return beat().run()
  68. else:
  69. return beat().run()
  70. def add_arguments(self, parser):
  71. c = self.app.conf
  72. bopts = parser.add_argument_group('Beat Options')
  73. bopts.add_argument('--detach', action='store_true', default=False)
  74. bopts.add_argument(
  75. '-s', '--schedule', default=c.beat_schedule_filename)
  76. bopts.add_argument('--max-interval', type=float)
  77. bopts.add_argument('-S', '--scheduler', default=c.beat_scheduler)
  78. bopts.add_argument('-l', '--loglevel', default='WARN')
  79. daemon_options(parser, default_pidfile='celerybeat.pid')
  80. user_options = self.app.user_options['beat']
  81. if user_options:
  82. uopts = parser.add_argument_group('User Options')
  83. self.add_compat_options(uopts, user_options)
  84. def main(app=None):
  85. beat(app=app).execute_from_commandline()
  86. if __name__ == '__main__': # pragma: no cover
  87. main()