"""Worker command-line program.This module is the 'program-version' of :mod:`celery.worker`.It does everything necessary to run that moduleas an actual application, like installing signal handlers,platform tweaks, and so on."""importloggingimportosimportplatformas_platformimportsysfromdatetimeimportdatetimefromfunctoolsimportpartialfrombilliard.commonimportREMAP_SIGTERMfrombilliard.processimportcurrent_processfromkombu.utils.encodingimportsafe_strfromceleryimportVERSION_BANNER,platforms,signalsfromcelery.appimporttracefromcelery.loaders.appimportAppLoaderfromcelery.platformsimportEX_FAILURE,EX_OK,check_privileges,isattyfromcelery.utilsimportstatic,termfromcelery.utils.debugimportcryfromcelery.utils.importsimportqualnamefromcelery.utils.logimportget_logger,in_sighandler,set_in_sighandlerfromcelery.utils.textimportpluralizefromcelery.workerimportWorkController__all__=('Worker',)logger=get_logger(__name__)is_jython=sys.platform.startswith('java')is_pypy=hasattr(sys,'pypy_version_info')ARTLINES=[' --------------','--- ***** -----','-- ******* ----','- *** --- * ---','- ** ----------','- ** ----------','- ** ----------','- ** ----------','- *** --- * ---','-- ******* ----','--- ***** -----',' --------------',]BANNER="""\{hostname} v{version}{platform}{timestamp}[config].> app: {app}.> transport: {conninfo}.> results: {results}.> concurrency: {concurrency}.> task events: {events}[queues]{queues}"""EXTRA_INFO_FMT="""[tasks]{tasks}"""defactive_thread_count():fromthreadingimportenumeratereturnsum(1fortinenumerate()ifnott.name.startswith('Dummy-'))defsafe_say(msg,f=sys.__stderr__):ifhasattr(f,'fileno')andf.fileno()isnotNone:os.write(f.fileno(),f'\n{msg}\n'.encode())
[文档]classWorker(WorkController):"""Worker as a program."""
[文档]defon_before_init(self,quiet=False,**kwargs):self.quiet=quiettrace.setup_worker_optimizations(self.app,self.hostname)# this signal can be used to set up configuration for# workers by name.signals.celeryd_init.send(sender=self.hostname,instance=self,conf=self.app.conf,options=kwargs,)check_privileges(self.app.conf.accept_content)
[文档]defon_init_blueprint(self):self._custom_logging=self.setup_logging()# apply task execution optimizations# -- This will finalize the app!trace.setup_worker_optimizations(self.app,self.hostname)
[文档]defon_start(self):app=self.appsuper().on_start()# this signal can be used to, for example, change queues after# the -Q option has been applied.signals.celeryd_after_setup.send(sender=self.hostname,instance=self,conf=app.conf,)ifself.purge:self.purge_messages()ifnotself.quiet:self.emit_banner()self.set_process_status('-active-')self.install_platform_tweaks(self)ifnotself._custom_loggingandself.redirect_stdouts:app.log.redirect_stdouts(self.redirect_stdouts_level)# TODO: Remove the following code in Celery 6.0# This qualifies as a hack for issue #6366.warn_deprecated=Trueconfig_source=app._config_sourceifisinstance(config_source,str):# Don't raise the warning when the settings originate from# django.conf:settingswarn_deprecated=config_source.lower()notin['django.conf:settings',]ifwarn_deprecated:ifapp.conf.maybe_warn_deprecated_settings():logger.warning("Please run `celery upgrade settings path/to/settings.py` ""to avoid these warnings and to allow a smoother upgrade ""to Celery 6.0.")
[文档]defemit_banner(self):# Dump configuration to screen so we have some basic information# for when users sends bug reports.use_image=term.supports_images()ifuse_image:print(term.imgcat(static.logo()))print(safe_str(''.join([str(self.colored.cyan(' \n',self.startup_info(artlines=notuse_image))),str(self.colored.reset(self.extra_info()or'')),])),file=sys.__stdout__,flush=True)
[文档]defpurge_messages(self):withself.app.connection_for_write()asconnection:count=self.app.control.purge(connection=connection)ifcount:# pragma: no coverprint(f"purge: Erased {count}{pluralize(count,'message')} from the queue.\n",flush=True)
[文档]defstartup_info(self,artlines=True):app=self.appconcurrency=str(self.concurrency)appr='{}:{:#x}'.format(app.mainor'__main__',id(app))ifnotisinstance(app.loader,AppLoader):loader=qualname(app.loader)ifloader.startswith('celery.loaders'):# pragma: no coverloader=loader[14:]appr+=f' ({loader})'ifself.autoscale:max,min=self.autoscaleconcurrency=f'{{min={min}, max={max}}}'pool=self.pool_clsifnotisinstance(pool,str):pool=pool.__module__concurrency+=f" ({pool.split('.')[-1]})"events='ON'ifnotself.task_events:events='OFF (enable -E to monitor tasks in this worker)'banner=BANNER.format(app=appr,hostname=safe_str(self.hostname),timestamp=datetime.now().replace(microsecond=0),version=VERSION_BANNER,conninfo=self.app.connection().as_uri(),results=self.app.backend.as_uri(),concurrency=concurrency,platform=safe_str(_platform.platform()),events=events,queues=app.amqp.queues.format(indent=0,indent_first=False),).splitlines()# integrate the ASCII art.ifartlines:fori,_inenumerate(banner):try:banner[i]=' '.join([ARTLINES[i],banner[i]])exceptIndexError:banner[i]=' '*16+banner[i]return'\n'.join(banner)+'\n'
[文档]definstall_platform_tweaks(self,worker):"""Install platform specific tweaks and workarounds."""ifself.app.IS_macOS:self.macOS_proxy_detection_workaround()# Install signal handler so SIGHUP restarts the worker.ifnotself._isatty:# only install HUP handler if detached from terminal,# so closing the terminal window doesn't restart the worker# into the background.ifself.app.IS_macOS:# macOS can't exec from a process using threads.# See https://github.com/celery/celery/issues#issue/152install_HUP_not_supported_handler(worker)else:install_worker_restart_handler(worker)install_worker_term_handler(worker)install_worker_term_hard_handler(worker)install_worker_int_handler(worker)install_cry_handler()install_rdb_handler()
def_shutdown_handler(worker:Worker,sig='SIGTERM',how='Warm',callback=None,exitcode=EX_OK,verbose=True):"""Install signal handler for warm/cold shutdown. The handler will run from the MainProcess. Args: worker (Worker): The worker that received the signal. sig (str, optional): The signal that was received. Defaults to 'TERM'. how (str, optional): The type of shutdown to perform. Defaults to 'Warm'. callback (Callable, optional): Signal handler. Defaults to None. exitcode (int, optional): The exit code to use. Defaults to EX_OK. verbose (bool, optional): Whether to print the type of shutdown. Defaults to True. """def_handle_request(*args):within_sighandler():fromcelery.workerimportstateifcurrent_process()._name=='MainProcess':ifcallback:callback(worker)ifverbose:safe_say(f'worker: {how} shutdown (MainProcess)',sys.__stdout__)signals.worker_shutting_down.send(sender=worker.hostname,sig=sig,how=how,exitcode=exitcode,)setattr(state,{'Warm':'should_stop','Cold':'should_terminate'}[how],exitcode)_handle_request.__name__=str(f'worker_{how}')platforms.signals[sig]=_handle_requestdefon_hard_shutdown(worker:Worker):"""Signal handler for hard shutdown. The handler will terminate the worker immediately by force using the exit code ``EX_FAILURE``. In practice, you should never get here, as the standard shutdown process should be enough. This handler is only for the worst-case scenario, where the worker is stuck and cannot be terminated gracefully (e.g., spamming the Ctrl+C in the terminal to force the worker to terminate). Args: worker (Worker): The worker that received the signal. Raises: WorkerTerminate: This exception will be raised in the MainProcess to terminate the worker immediately. """fromcelery.exceptionsimportWorkerTerminateraiseWorkerTerminate(EX_FAILURE)defduring_soft_shutdown(worker:Worker):"""This signal handler is called when the worker is in the middle of the soft shutdown process. When the worker is in the soft shutdown process, it is waiting for tasks to finish. If the worker receives a SIGINT (Ctrl+C) or SIGQUIT signal (or possibly SIGTERM if REMAP_SIGTERM is set to "SIGQUIT"), the handler will cancels all unacked requests to allow the worker to terminate gracefully and replace the signal handler for SIGINT and SIGQUIT with the hard shutdown handler ``on_hard_shutdown`` to terminate the worker immediately by force next time the signal is received. It will give the worker once last chance to gracefully terminate (the cold shutdown), after canceling all unacked requests, before using the hard shutdown handler to terminate the worker forcefully. Args: worker (Worker): The worker that received the signal. """# Replace the signal handler for SIGINT (Ctrl+C) and SIGQUIT (and possibly SIGTERM)# with the hard shutdown handler to terminate the worker immediately by forceinstall_worker_term_hard_handler(worker,sig='SIGINT',callback=on_hard_shutdown,verbose=False)install_worker_term_hard_handler(worker,sig='SIGQUIT',callback=on_hard_shutdown)# Cancel all unacked requests and allow the worker to terminate naturallyworker.consumer.cancel_all_unacked_requests()# We get here if the worker was in the middle of the soft (cold) shutdown process,# and the matching signal was received. This can typically happen when the worker is# waiting for tasks to finish, and the user decides to still cancel the running tasks.# We give the worker the last chance to gracefully terminate by letting the soft shutdown# waiting time to finish, which is running in the MainProcess from the previous signal handler call.safe_say('Waiting gracefully for cold shutdown to complete...',sys.__stdout__)defon_cold_shutdown(worker:Worker):"""Signal handler for cold shutdown. Registered for SIGQUIT and SIGINT (Ctrl+C) signals. If REMAP_SIGTERM is set to "SIGQUIT", this handler will also be registered for SIGTERM. This handler will initiate the cold (and soft if enabled) shutdown procesdure for the worker. Worker running with N tasks: - SIGTERM: -The worker will initiate the warm shutdown process until all tasks are finished. Additional. SIGTERM signals will be ignored. SIGQUIT will transition to the cold shutdown process described below. - SIGQUIT: - The worker will initiate the cold shutdown process. - If the soft shutdown is enabled, the worker will wait for the tasks to finish up to the soft shutdown timeout (practically having a limited warm shutdown just before the cold shutdown). - Cancel all tasks (from the MainProcess) and allow the worker to complete the cold shutdown process gracefully. Caveats: - SIGINT (Ctrl+C) signal is defined to replace itself with the cold shutdown (SIGQUIT) after first use, and to emit a message to the user to hit Ctrl+C again to initiate the cold shutdown process. But, most important, it will also be caught in WorkController.start() to initiate the warm shutdown process. - SIGTERM will also be handled in WorkController.start() to initiate the warm shutdown process (the same). - If REMAP_SIGTERM is set to "SIGQUIT", the SIGTERM signal will be remapped to SIGQUIT, and the cold shutdown process will be initiated instead of the warm shutdown process using SIGTERM. - If SIGQUIT is received (also via SIGINT) during the cold/soft shutdown process, the handler will cancel all unacked requests but still wait for the soft shutdown process to finish before terminating the worker gracefully. The next time the signal is received though, the worker will terminate immediately by force. So, the purpose of this handler is to allow waiting for the soft shutdown timeout, then cancel all tasks from the MainProcess and let the WorkController.terminate() to terminate the worker naturally. If the soft shutdown is disabled, it will immediately cancel all tasks let the cold shutdown finish normally. Args: worker (Worker): The worker that received the signal. """safe_say('worker: Hitting Ctrl+C again will terminate all running tasks!',sys.__stdout__)# Replace the signal handler for SIGINT (Ctrl+C) and SIGQUIT (and possibly SIGTERM)install_worker_term_hard_handler(worker,sig='SIGINT',callback=during_soft_shutdown)install_worker_term_hard_handler(worker,sig='SIGQUIT',callback=during_soft_shutdown)ifREMAP_SIGTERM=="SIGQUIT":install_worker_term_hard_handler(worker,sig='SIGTERM',callback=during_soft_shutdown)# else, SIGTERM will print the _shutdown_handler's message and do nothing, every time it is received..# Initiate soft shutdown process (if enabled and tasks are running)worker.wait_for_soft_shutdown()# Cancel all unacked requests and allow the worker to terminate naturallyworker.consumer.cancel_all_unacked_requests()# Stop the pool to allow successful tasks call on_success()worker.consumer.pool.stop()# Allow SIGTERM to be remapped to SIGQUIT to initiate cold shutdown instead of warm shutdown using SIGTERMifREMAP_SIGTERM=="SIGQUIT":install_worker_term_handler=partial(_shutdown_handler,sig='SIGTERM',how='Cold',callback=on_cold_shutdown,exitcode=EX_FAILURE,)else:install_worker_term_handler=partial(_shutdown_handler,sig='SIGTERM',how='Warm',)ifnotis_jython:# pragma: no coverinstall_worker_term_hard_handler=partial(_shutdown_handler,sig='SIGQUIT',how='Cold',callback=on_cold_shutdown,exitcode=EX_FAILURE,)else:# pragma: no coverinstall_worker_term_handler= \
install_worker_term_hard_handler=lambda*a,**kw:Nonedefon_SIGINT(worker):safe_say('worker: Hitting Ctrl+C again will initiate cold shutdown, terminating all running tasks!',sys.__stdout__)install_worker_term_hard_handler(worker,sig='SIGINT',verbose=False)ifnotis_jython:# pragma: no coverinstall_worker_int_handler=partial(_shutdown_handler,sig='SIGINT',callback=on_SIGINT,exitcode=EX_FAILURE,)else:# pragma: no coverdefinstall_worker_int_handler(*args,**kwargs):passdef_reload_current_worker():platforms.close_open_fds([sys.__stdin__,sys.__stdout__,sys.__stderr__,])os.execv(sys.executable,[sys.executable]+sys.argv)definstall_worker_restart_handler(worker,sig='SIGHUP'):defrestart_worker_sig_handler(*args):"""Signal handler restarting the current python program."""set_in_sighandler(True)safe_say(f"Restarting celery worker ({' '.join(sys.argv)})",sys.__stdout__)importatexitatexit.register(_reload_current_worker)fromcelery.workerimportstatestate.should_stop=EX_OKplatforms.signals[sig]=restart_worker_sig_handlerdefinstall_cry_handler(sig='SIGUSR1'):# PyPy does not have sys._current_framesifis_pypy:# pragma: no coverreturndefcry_handler(*args):"""Signal handler logging the stack-trace of all active threads."""within_sighandler():safe_say(cry())platforms.signals[sig]=cry_handlerdefinstall_rdb_handler(envvar='CELERY_RDBSIG',sig='SIGUSR2'):# pragma: no coverdefrdb_handler(*args):"""Signal handler setting a rdb breakpoint at the current frame."""within_sighandler():fromcelery.contrib.rdbimport_frame,set_trace# gevent does not pass standard signal handler argsframe=args[1]ifargselse_frame().f_backset_trace(frame)ifos.environ.get(envvar):platforms.signals[sig]=rdb_handlerdefinstall_HUP_not_supported_handler(worker,sig='SIGHUP'):defwarn_on_HUP_handler(signum,frame):within_sighandler():safe_say('{sig} not supported: Restarting with {sig} is ''unstable on this platform!'.format(sig=sig))platforms.signals[sig]=warn_on_HUP_handler