U {1`)@s dZddlmZmZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z mZmZmZzeWnek reZYnXeZedeGdd d eZGd d d eeZGd d d eeZGdddeeZGdddZdddZ ddZ!ddZ"dS)z Daemon runner library. )absolute_importunicode_literalsNpidfile)0_chain_exception_from_existing_exception_context DaemonContext basestringunicodeu@The ‘runner’ module is not a supported API for this library.cs(eZdZdZfddZddZZS)DaemonRunnerErrorz3 Abstract base class for errors from DaemonRunner. cs|tj||dS)N)_chain_from_contextsuper__init__)selfargskwargs __class__>/opt/alt/python38/lib/python3.8/site-packages/daemon/runner.pyr2szDaemonRunnerError.__init__cCst|dddS)NTZas_causerrrrrr 7sz%DaemonRunnerError._chain_from_context)__name__ __module__ __qualname____doc__rr __classcell__rrrrr /s r c@seZdZdZddZdS)DaemonRunnerInvalidActionErrorz; Raised when specified action for DaemonRunner is invalid. cCst|dddS)NFrrrrrrr >sz2DaemonRunnerInvalidActionError._chain_from_contextN)rrrrr rrrrr;src@seZdZdZdS)DaemonRunnerStartFailureErrorz, Raised when failure starting DaemonRunner. NrrrrrrrrrCsrc@seZdZdZdS)DaemonRunnerStopFailureErrorz, Raised when failure stopping DaemonRunner. Nr rrrrr!Gsr!c@sreZdZdZdZddZddZddZdd d Zd d Z ddZ ddZ ddZ e e e dZ ddZddZd S) DaemonRunnera4 Controller for a callable running in a separate background process. The first command-line argument is the action to take: * 'start': Become a daemon and call `app.run()`. * 'stop': Exit the daemon process specified in the PID file. * 'restart': Stop, then start. zstarted with pid {pid:d}cCsN|||_t|_||d|_|jdk r@t|j|j|_|j|j_dS)a# Set up the parameters of a new runner. :param app: The application instance; see below. :return: ``None``. The `app` argument must have the following attributes: * `stdin_path`, `stdout_path`, `stderr_path`: Filesystem paths to open and replace the existing `sys.stdin`, `sys.stdout`, `sys.stderr`. * `pidfile_path`: Absolute filesystem path to a file that will be used as the PID file for the daemon. If ``None``, no PID file will be used. * `pidfile_timeout`: Used as the default acquisition timeout value supplied to the runner's PID lock file. * `run`: Callable that will be invoked when the daemon is started. N) parse_argsapprdaemon_context#_open_streams_from_app_stream_pathsrZ pidfile_pathmake_pidlockfileZpidfile_timeoutrr$rrrrXs  zDaemonRunner.__init__cCs8t|jd|j_t|jd|j_t|jddd|j_dS)ab Open the `daemon_context` streams from the paths specified. :param app: The application instance. Open the `daemon_context` standard streams (`stdin`, `stdout`, `stderr`) as stream objects of the appropriate types, from each of the corresponding filesystem paths from the `app`. Zrtzw+tr) bufferingN)openZ stdin_pathr%stdinZ stdout_pathstdoutZ stderr_pathstderrr(rrrr&zs z0DaemonRunner._open_streams_from_app_stream_pathscCsHtj|d}d}d|j}dj||d}t|t |dS)z Emit a usage message, then exit. :param argv: The command-line arguments used to invoke the program, as a sequence of strings. :return: ``None``. r|zusage: {progname} {usage})prognameZusageN) ospathbasenamejoin action_funcskeysformat emit_messagesysexit)rargvr0Zusage_exit_codeZ action_usagemessagerrr _usage_exitszDaemonRunner._usage_exitNcCsP|dkrtj}d}t||kr(||t|d|_|j|jkrL||dS)a Parse command-line arguments. :param argv: The command-line arguments used to invoke the program, as a sequence of strings. :return: ``None``. The parser expects the first argument as the program name, the second argument as the action to perform. If the parser fails to parse the arguments, emit a usage message and exit the program. Nr.r)r9r;lenr=r actionr5)rr;Zmin_argsrrrr#s   zDaemonRunner.parse_argscCs|t|jr|jz|jWn,tjk rNtdj|jd}|YnXt }|j j|d}t ||j dS)z Open the daemon context and run the application. :return: ``None``. :raises DaemonRunnerStartFailureError: If the PID file cannot be locked by this process. z(PID file {pidfile.path!r} already lockedr)pidN)is_pidfile_staler break_lockr%r*lockfileZ AlreadyLockedrr7r1getpid start_messager8r$run)rerrorr@r<rrr_starts   zDaemonRunner._startc CsZ|j}zt|tjWn8tk rT}ztdj||d}|W5d}~XYnXdS)z Terminate the daemon process specified in the current PID file. :return: ``None``. :raises DaemonRunnerStopFailureError: If terminating the daemon fails with an OS error. z"Failed to terminate {pid:d}: {exc})r@excN) rread_pidr1killsignalSIGTERMOSErrorr!r7)rr@rIrGrrr_terminate_daemon_processs z&DaemonRunner._terminate_daemon_processcCsB|js tdj|jd}|t|jr6|jn|dS)z Exit the daemon process specified in the current PID file. :return: ``None``. :raises DaemonRunnerStopFailureError: If the PID file is not already locked. z$PID file {pidfile.path!r} not lockedrN)rZ is_lockedr!r7rArBrO)rrGrrr_stops   zDaemonRunner._stopcCs||dS)z Stop, then start. N)rPrHrrrr_restartszDaemonRunner._restart)startstopZrestartcCs@z|j|j}Wn*tk r:tdj|jd}|YnX|S)ae Get the function for the specified action. :return: The function object corresponding to the specified action. :raises DaemonRunnerInvalidActionError: if the action is unknown. The action is specified by the `action` attribute, which is set during `parse_args`. zUnknown action: {action!r})r?)r5r?KeyErrorrr7)rfuncrGrrr_get_action_funcs  zDaemonRunner._get_action_funccCs|}||dS)z Perform the requested action. :return: ``None``. The action is specified by the `action` attribute, which is set during `parse_args`. N)rV)rrUrrr do_actions zDaemonRunner.do_action)N)rrrrrErr&r=r#rHrOrPrQr5rVrWrrrrr"Ks  " r"cCs,|dkrtj}|dj|d|dS)z@ Emit a message to the specified stream (default `sys.stderr`). Nz {message} )r<)r9r-writer7flush)r<streamrrrr8sr8cCsNt|tstdj|d}|tj|s>tdj|d}|t||}|S)z= Make a PIDLockFile instance with the given filesystem path. zNot a filesystem path: {path!r})r2zNot an absolute path: {path!r}) isinstancer ValueErrorr7r1r2isabsrZTimeoutPIDLockFile)r2Zacquire_timeoutrGrCrrrr'&s   r'c Cstd}|}|dk rpzt|tjWnHtk r>d}Yn2tk rn}z|jtjkr^d}W5d}~XYnX|S)u Determine whether a PID file is stale. :return: ``True`` iff the PID file is stale; otherwise ``False``. The PID file is “stale” if its contents are valid but do not match the PID of a currently-running process. FNT) rJr1rKrLSIG_DFLProcessLookupErrorrNerrnoZESRCH)rresultZ pidfile_pidrIrrrrA5s  rA)N)#rZ __future__rrr`r1rLr9warningsrCrZdaemonrrr r r_ NameErrorNotImplementedtypeZ __metaclass__warnDeprecationWarning Exceptionr r\r RuntimeErrorrr!r"r8r'rArrrr s6   T