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)NTas_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. Nrrrrrrrrr Csr c@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`. rtzw+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})prognameusageN) ospathbasenamejoin action_funcskeysformat emit_messagesysexit)rargvr2Zusage_exit_code 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. Nr0r)r<r>lenrAr actionr8)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 AlreadyLockedr r:r4getpid start_messager;r%run)rerrorrDr@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})rDexcN) rread_pidr4killsignalSIGTERMOSErrorr"r:)rrDrMrKrrr_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"r:rErFrS)rrKrrr_stops   zDaemonRunner._stopcCs||dS)z Stop, then start. N)rTrLrrrr_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})rC)r8rCKeyErrorrr:)rfuncrKrrr_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)rZ)rrYrrr do_actions zDaemonRunner.do_action)N)rrrrrIrr'rAr$rLrSrTrUr8rZr[rrrrr#Ks  " r#cCs,|dkrtj}|dj|d|dS)z@ Emit a message to the specified stream (default `sys.stderr`). Nz {message} )r@)r<r/writer:flush)r@streamrrrr;sr;cCsNt|tstdj|d}|tj|s>tdj|d}|t||}|S)z= Make a PIDLockFile instance with the given filesystem path. zNot a filesystem path: {path!r})r5zNot an absolute path: {path!r}) isinstancer ValueErrorr:r4r5isabsrZTimeoutPIDLockFile)r5Zacquire_timeoutrKrGrrrr(&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) rNr4rOrPSIG_DFLProcessLookupErrorrRerrnoZESRCH)rresultZ pidfile_pidrMrrrrE5s  rE)N)#r __future__rrrdr4rPr<warningsrGrdaemonrrr r rc NameErrorNotImplementedtype __metaclass__warnDeprecationWarning Exceptionr r`r RuntimeErrorr r"r#r;r(rErrrr s6   T