forked from celery/celery
/
celerybeat.py
173 lines (135 loc) · 5.6 KB
/
celerybeat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
"""celerybeat
.. program:: celerybeat
.. cmdoption:: -f, --logfile
Path to log file. If no logfile is specified, ``stderr`` is used.
.. cmdoption:: -l, --loglevel
Logging level, choose between ``DEBUG``, ``INFO``, ``WARNING``,
``ERROR``, ``CRITICAL``, or ``FATAL``.
.. cmdoption:: -p, --pidfile
Path to pidfile.
.. cmdoption:: -d, --detach, --daemon
Run in the background as a daemon.
.. cmdoption:: -u, --uid
User-id to run ``celerybeat`` as when in daemon mode.
.. cmdoption:: -g, --gid
Group-id to run ``celerybeat`` as when in daemon mode.
.. cmdoption:: --umask
umask of the process when in daemon mode.
.. cmdoption:: --workdir
Directory to change to when in daemon mode.
.. cmdoption:: --chroot
Change root directory to this path when in daemon mode.
"""
import os
import sys
import traceback
import optparse
from celery import conf
from celery import platform
from celery import __version__
from celery.log import emergency_error
from celery.beat import ClockService
from celery.loaders import current_loader, settings
from celery.messaging import get_connection_info
STARTUP_INFO_FMT = """
Configuration ->
. broker -> %(conninfo)s
. exchange -> %(exchange)s (%(exchange_type)s)
. consumer -> queue:%(consumer_queue)s binding:%(consumer_rkey)s
""".strip()
OPTION_LIST = (
optparse.make_option('-f', '--logfile', default=conf.CELERYBEAT_LOG_FILE,
action="store", dest="logfile",
help="Path to log file."),
optparse.make_option('-l', '--loglevel',
default=conf.CELERYBEAT_LOG_LEVEL,
action="store", dest="loglevel",
help="Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL/FATAL."),
optparse.make_option('-p', '--pidfile',
default=conf.CELERYBEAT_PID_FILE,
action="store", dest="pidfile",
help="Path to pidfile."),
optparse.make_option('-d', '--detach', '--daemon', default=False,
action="store_true", dest="detach",
help="Run in the background as a daemon."),
optparse.make_option('-u', '--uid', default=None,
action="store", dest="uid",
help="User-id to run celerybeat as when in daemon mode."),
optparse.make_option('-g', '--gid', default=None,
action="store", dest="gid",
help="Group-id to run celerybeat as when in daemon mode."),
optparse.make_option('--umask', default=0,
action="store", type="int", dest="umask",
help="umask of the process when in daemon mode."),
optparse.make_option('--workdir', default=None,
action="store", dest="working_directory",
help="Directory to change to when in daemon mode."),
optparse.make_option('--chroot', default=None,
action="store", dest="chroot",
help="Change root directory to this path when in daemon mode."),
)
def run_clockservice(detach=False, loglevel=conf.CELERYBEAT_LOG_LEVEL,
logfile=conf.CELERYBEAT_LOG_FILE, pidfile=conf.CELERYBEAT_PID_FILE,
umask=0, uid=None, gid=None, working_directory=None, chroot=None,
**kwargs):
"""Starts the celerybeat clock server."""
print("celerybeat %s is starting." % __version__)
# Setup logging
if not isinstance(loglevel, int):
loglevel = conf.LOG_LEVELS[loglevel.upper()]
if not detach:
logfile = None # log to stderr when not running in the background.
# Dump configuration to screen so we have some basic information
# when users sends e-mails.
print(STARTUP_INFO_FMT % {
"conninfo": get_connection_info(),
"exchange": conf.AMQP_EXCHANGE,
"exchange_type": conf.AMQP_EXCHANGE_TYPE,
"consumer_queue": conf.AMQP_CONSUMER_QUEUE,
"consumer_rkey": conf.AMQP_CONSUMER_ROUTING_KEY,
"publisher_rkey": conf.AMQP_PUBLISHER_ROUTING_KEY,
"loglevel": loglevel,
"pidfile": pidfile,
})
print("celerybeat has started.")
arg_start = "manage" in sys.argv[0] and 2 or 1
platform.set_process_title("celerybeat",
info=" ".join(sys.argv[arg_start:]))
from celery.log import setup_logger, redirect_stdouts_to_logger
if detach:
context = platform.create_daemon_context(logfile, pidfile,
chroot_directory=chroot,
working_directory=working_directory,
umask=umask,
uid=uid,
gid=gid)
context.open()
logger = setup_logger(loglevel, logfile)
redirect_stdouts_to_logger(logger, loglevel)
# Run the worker init handler.
# (Usually imports task modules and such.)
current_loader.on_worker_init()
def _run_clock():
logger = setup_logger(loglevel, logfile)
clockservice = ClockService(logger=logger, is_detached=detach)
try:
clockservice.start()
except Exception, e:
emergency_error(logfile,
"celerybeat raised exception %s: %s\n%s" % (
e.__class__, e, traceback.format_exc()))
try:
_run_clock()
except:
if detach:
context.close()
raise
def parse_options(arguments):
"""Parse the available options to ``celeryd``."""
parser = optparse.OptionParser(option_list=OPTION_LIST)
options, values = parser.parse_args(arguments)
return options
if __name__ == "__main__":
options = parse_options(sys.argv[1:])
run_clockservice(**vars(options))