worker.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.apps.worker
  4. ~~~~~~~~~~~~~~~~~~
  5. This module is the 'program-version' of :mod:`celery.worker`.
  6. It does everything necessary to run that module
  7. as an actual application, like installing signal handlers,
  8. platform tweaks, and so on.
  9. """
  10. from __future__ import absolute_import, print_function, unicode_literals
  11. import logging
  12. import os
  13. import platform as _platform
  14. import sys
  15. import warnings
  16. from functools import partial
  17. from billiard import current_process
  18. from kombu.utils.encoding import safe_str
  19. from kombu.utils.url import maybe_sanitize_url
  20. from celery import VERSION_BANNER, platforms, signals
  21. from celery.app import trace
  22. from celery.exceptions import (
  23. CDeprecationWarning, WorkerShutdown, WorkerTerminate,
  24. )
  25. from celery.five import string, string_t
  26. from celery.loaders.app import AppLoader
  27. from celery.platforms import check_privileges
  28. from celery.utils import cry, isatty
  29. from celery.utils.imports import qualname
  30. from celery.utils.log import get_logger, in_sighandler, set_in_sighandler
  31. from celery.utils.text import pluralize
  32. from celery.worker import WorkController
  33. __all__ = ['Worker']
  34. logger = get_logger(__name__)
  35. is_jython = sys.platform.startswith('java')
  36. is_pypy = hasattr(sys, 'pypy_version_info')
  37. W_PICKLE_DEPRECATED = """
  38. Starting from version 3.2 Celery will refuse to accept pickle by default.
  39. The pickle serializer is a security concern as it may give attackers
  40. the ability to execute any command. It's important to secure
  41. your broker from unauthorized access when using pickle, so we think
  42. that enabling pickle should require a deliberate action and not be
  43. the default choice.
  44. If you depend on pickle then you should set a setting to disable this
  45. warning and to be sure that everything will continue working
  46. when you upgrade to Celery 3.2::
  47. CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']
  48. You must only enable the serializers that you will actually use.
  49. """
  50. def active_thread_count():
  51. from threading import enumerate
  52. return sum(1 for t in enumerate()
  53. if not t.name.startswith('Dummy-'))
  54. def safe_say(msg):
  55. print('\n{0}'.format(msg), file=sys.__stderr__)
  56. ARTLINES = [
  57. ' --------------',
  58. '---- **** -----',
  59. '--- * *** * --',
  60. '-- * - **** ---',
  61. '- ** ----------',
  62. '- ** ----------',
  63. '- ** ----------',
  64. '- ** ----------',
  65. '- *** --- * ---',
  66. '-- ******* ----',
  67. '--- ***** -----',
  68. ' --------------',
  69. ]
  70. BANNER = """\
  71. {hostname} v{version}
  72. {platform}
  73. [config]
  74. .> app: {app}
  75. .> transport: {conninfo}
  76. .> results: {results}
  77. .> concurrency: {concurrency}
  78. [queues]
  79. {queues}
  80. """
  81. EXTRA_INFO_FMT = """
  82. [tasks]
  83. {tasks}
  84. """
  85. class Worker(WorkController):
  86. def on_before_init(self, **kwargs):
  87. trace.setup_worker_optimizations(self.app)
  88. # this signal can be used to set up configuration for
  89. # workers by name.
  90. signals.celeryd_init.send(
  91. sender=self.hostname, instance=self,
  92. conf=self.app.conf, options=kwargs,
  93. )
  94. check_privileges(self.app.conf.CELERY_ACCEPT_CONTENT)
  95. def on_after_init(self, purge=False, no_color=None,
  96. redirect_stdouts=None, redirect_stdouts_level=None,
  97. **kwargs):
  98. self.redirect_stdouts = self._getopt(
  99. 'redirect_stdouts', redirect_stdouts,
  100. )
  101. self.redirect_stdouts_level = self._getopt(
  102. 'redirect_stdouts_level', redirect_stdouts_level,
  103. )
  104. super(Worker, self).setup_defaults(**kwargs)
  105. self.purge = purge
  106. self.no_color = no_color
  107. self._isatty = isatty(sys.stdout)
  108. self.colored = self.app.log.colored(
  109. self.logfile,
  110. enabled=not no_color if no_color is not None else no_color
  111. )
  112. def on_init_blueprint(self):
  113. self._custom_logging = self.setup_logging()
  114. # apply task execution optimizations
  115. # -- This will finalize the app!
  116. trace.setup_worker_optimizations(self.app)
  117. def on_start(self):
  118. if not self._custom_logging and self.redirect_stdouts:
  119. self.app.log.redirect_stdouts(self.redirect_stdouts_level)
  120. WorkController.on_start(self)
  121. # this signal can be used to e.g. change queues after
  122. # the -Q option has been applied.
  123. signals.celeryd_after_setup.send(
  124. sender=self.hostname, instance=self, conf=self.app.conf,
  125. )
  126. if not self.app.conf.value_set_for('CELERY_ACCEPT_CONTENT'):
  127. warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED))
  128. if self.purge:
  129. self.purge_messages()
  130. # Dump configuration to screen so we have some basic information
  131. # for when users sends bug reports.
  132. print(safe_str(''.join([
  133. string(self.colored.cyan(' \n', self.startup_info())),
  134. string(self.colored.reset(self.extra_info() or '')),
  135. ])), file=sys.__stdout__)
  136. self.set_process_status('-active-')
  137. self.install_platform_tweaks(self)
  138. def on_consumer_ready(self, consumer):
  139. signals.worker_ready.send(sender=consumer)
  140. print('{0} ready.'.format(safe_str(self.hostname), ))
  141. def setup_logging(self, colorize=None):
  142. if colorize is None and self.no_color is not None:
  143. colorize = not self.no_color
  144. return self.app.log.setup(
  145. self.loglevel, self.logfile,
  146. redirect_stdouts=False, colorize=colorize, hostname=self.hostname,
  147. )
  148. def purge_messages(self):
  149. count = self.app.control.purge()
  150. if count:
  151. print('purge: Erased {0} {1} from the queue.\n'.format(
  152. count, pluralize(count, 'message')))
  153. def tasklist(self, include_builtins=True, sep='\n', int_='celery.'):
  154. return sep.join(
  155. ' . {0}'.format(task) for task in sorted(self.app.tasks)
  156. if (not task.startswith(int_) if not include_builtins else task)
  157. )
  158. def extra_info(self):
  159. if self.loglevel <= logging.INFO:
  160. include_builtins = self.loglevel <= logging.DEBUG
  161. tasklist = self.tasklist(include_builtins=include_builtins)
  162. return EXTRA_INFO_FMT.format(tasks=tasklist)
  163. def startup_info(self):
  164. app = self.app
  165. concurrency = string(self.concurrency)
  166. appr = '{0}:0x{1:x}'.format(app.main or '__main__', id(app))
  167. if not isinstance(app.loader, AppLoader):
  168. loader = qualname(app.loader)
  169. if loader.startswith('celery.loaders'):
  170. loader = loader[14:]
  171. appr += ' ({0})'.format(loader)
  172. if self.autoscale:
  173. max, min = self.autoscale
  174. concurrency = '{{min={0}, max={1}}}'.format(min, max)
  175. pool = self.pool_cls
  176. if not isinstance(pool, string_t):
  177. pool = pool.__module__
  178. concurrency += ' ({0})'.format(pool.split('.')[-1])
  179. events = 'ON'
  180. if not self.send_events:
  181. events = 'OFF (enable -E to monitor this worker)'
  182. banner = BANNER.format(
  183. app=appr,
  184. hostname=safe_str(self.hostname),
  185. version=VERSION_BANNER,
  186. conninfo=self.app.connection().as_uri(),
  187. results=maybe_sanitize_url(
  188. self.app.conf.CELERY_RESULT_BACKEND or 'disabled',
  189. ),
  190. concurrency=concurrency,
  191. platform=safe_str(_platform.platform()),
  192. events=events,
  193. queues=app.amqp.queues.format(indent=0, indent_first=False),
  194. ).splitlines()
  195. # integrate the ASCII art.
  196. for i, x in enumerate(banner):
  197. try:
  198. banner[i] = ' '.join([ARTLINES[i], banner[i]])
  199. except IndexError:
  200. banner[i] = ' ' * 16 + banner[i]
  201. return '\n'.join(banner) + '\n'
  202. def install_platform_tweaks(self, worker):
  203. """Install platform specific tweaks and workarounds."""
  204. if self.app.IS_OSX:
  205. self.osx_proxy_detection_workaround()
  206. # Install signal handler so SIGHUP restarts the worker.
  207. if not self._isatty:
  208. # only install HUP handler if detached from terminal,
  209. # so closing the terminal window doesn't restart the worker
  210. # into the background.
  211. if self.app.IS_OSX:
  212. # OS X can't exec from a process using threads.
  213. # See http://github.com/celery/celery/issues#issue/152
  214. install_HUP_not_supported_handler(worker)
  215. else:
  216. install_worker_restart_handler(worker)
  217. install_worker_term_handler(worker)
  218. install_worker_term_hard_handler(worker)
  219. install_worker_int_handler(worker)
  220. install_cry_handler()
  221. install_rdb_handler()
  222. def osx_proxy_detection_workaround(self):
  223. """See http://github.com/celery/celery/issues#issue/161"""
  224. os.environ.setdefault('celery_dummy_proxy', 'set_by_celeryd')
  225. def set_process_status(self, info):
  226. return platforms.set_mp_process_title(
  227. 'celeryd',
  228. info='{0} ({1})'.format(info, platforms.strargv(sys.argv)),
  229. hostname=self.hostname,
  230. )
  231. def _shutdown_handler(worker, sig='TERM', how='Warm',
  232. exc=WorkerShutdown, callback=None):
  233. def _handle_request(*args):
  234. with in_sighandler():
  235. from celery.worker import state
  236. if current_process()._name == 'MainProcess':
  237. if callback:
  238. callback(worker)
  239. safe_say('worker: {0} shutdown (MainProcess)'.format(how))
  240. if active_thread_count() > 1:
  241. setattr(state, {'Warm': 'should_stop',
  242. 'Cold': 'should_terminate'}[how], True)
  243. else:
  244. raise exc()
  245. _handle_request.__name__ = str('worker_{0}'.format(how))
  246. platforms.signals[sig] = _handle_request
  247. install_worker_term_handler = partial(
  248. _shutdown_handler, sig='SIGTERM', how='Warm', exc=WorkerShutdown,
  249. )
  250. if not is_jython: # pragma: no cover
  251. install_worker_term_hard_handler = partial(
  252. _shutdown_handler, sig='SIGQUIT', how='Cold', exc=WorkerTerminate,
  253. )
  254. else: # pragma: no cover
  255. install_worker_term_handler = \
  256. install_worker_term_hard_handler = lambda *a, **kw: None
  257. def on_SIGINT(worker):
  258. safe_say('worker: Hitting Ctrl+C again will terminate all running tasks!')
  259. install_worker_term_hard_handler(worker, sig='SIGINT')
  260. if not is_jython: # pragma: no cover
  261. install_worker_int_handler = partial(
  262. _shutdown_handler, sig='SIGINT', callback=on_SIGINT
  263. )
  264. else: # pragma: no cover
  265. def install_worker_int_handler(*a, **kw):
  266. pass
  267. def _reload_current_worker():
  268. platforms.close_open_fds([
  269. sys.__stdin__, sys.__stdout__, sys.__stderr__,
  270. ])
  271. os.execv(sys.executable, [sys.executable] + sys.argv)
  272. def install_worker_restart_handler(worker, sig='SIGHUP'):
  273. def restart_worker_sig_handler(*args):
  274. """Signal handler restarting the current python program."""
  275. set_in_sighandler(True)
  276. safe_say('Restarting celery worker ({0})'.format(' '.join(sys.argv)))
  277. import atexit
  278. atexit.register(_reload_current_worker)
  279. from celery.worker import state
  280. state.should_stop = True
  281. platforms.signals[sig] = restart_worker_sig_handler
  282. def install_cry_handler(sig='SIGUSR1'):
  283. # Jython/PyPy does not have sys._current_frames
  284. if is_jython or is_pypy: # pragma: no cover
  285. return
  286. def cry_handler(*args):
  287. """Signal handler logging the stacktrace of all active threads."""
  288. with in_sighandler():
  289. safe_say(cry())
  290. platforms.signals[sig] = cry_handler
  291. def install_rdb_handler(envvar='CELERY_RDBSIG',
  292. sig='SIGUSR2'): # pragma: no cover
  293. def rdb_handler(*args):
  294. """Signal handler setting a rdb breakpoint at the current frame."""
  295. with in_sighandler():
  296. from celery.contrib.rdb import set_trace, _frame
  297. # gevent does not pass standard signal handler args
  298. frame = args[1] if args else _frame().f_back
  299. set_trace(frame)
  300. if os.environ.get(envvar):
  301. platforms.signals[sig] = rdb_handler
  302. def install_HUP_not_supported_handler(worker, sig='SIGHUP'):
  303. def warn_on_HUP_handler(signum, frame):
  304. with in_sighandler():
  305. safe_say('{sig} not supported: Restarting with {sig} is '
  306. 'unstable on this platform!'.format(sig=sig))
  307. platforms.signals[sig] = warn_on_HUP_handler