Viewing File: /opt/alt/python35/lib/python3.5/site-packages/daemon/__pycache__/runner.cpython-35.pyc



^V&
@s`dZddlmZmZddlZddlZddlZddlZyeWne	k
rne
ZYnXddlZddlm
Z
ddlmZmZddlmZddlmZGd	d
d
eZGdddeeZGd
ddeeZGdddeeZGdddZdddZddZddZdS)z Daemon runner library.
    )absolute_importunicode_literalsN)pidfile)
basestringunicode)
DaemonContext)0_chain_exception_from_existing_exception_contextcs4eZdZdZfddZddZS)DaemonRunnerErrorz3 Abstract base class for errors from DaemonRunner. cs'|jtt|j||dS)N)_chain_from_contextsuperr
__init__)selfargskwargs)	__class__
/runner.pyr
,s
zDaemonRunnerError.__init__cCst|dddS)Nas_causeT)r	)rrrrr1sz%DaemonRunnerError._chain_from_context)__name__
__module____qualname____doc__r
rrr)rrr
)sr
c@s"eZdZdZddZdS)DaemonRunnerInvalidActionErrorz; Raised when specified action for DaemonRunner is invalid. cCst|dddS)NrF)r	)rrrrr8sz2DaemonRunnerInvalidActionError._chain_from_contextN)rrrrrrrrrr5src@seZdZdZdS)DaemonRunnerStartFailureErrorz, Raised when failure starting DaemonRunner. N)rrrrrrrrr=src@seZdZdZdS)DaemonRunnerStopFailureErrorz, Raised when failure stopping DaemonRunner. N)rrrrrrrrrAsrc@seZdZdZeZdZddZddZddd	Z	d
dZ
dd
ZddZddZ
de
dede
iZddZddZdS)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}cCs|j||_t|_t|jd|j_t|jd|j_t|j	ddd|j_
d|_|jdk	rt
|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.

            Zrtzw+t	bufferingrN)
parse_argsapprdaemon_contextopenZ
stdin_pathstdinZstdout_pathstdoutZstderr_pathstderrrZpidfile_pathmake_pidlockfileZpidfile_timeout)rrrrrr
Ts
		zDaemonRunner.__init__cCsgtjj|d}d}dj|jj}djd|d|}t|tj	|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)rargvr(Zusage_exit_codeZaction_usagemessagerrr_usage_exitys	
zDaemonRunner._usage_exitNcCsp|dkrtj}d}t||kr:|j|t|d|_|j|jkrl|j|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)r2r4lenr6ractionr.)rr4Zmin_argsrrrrs	
zDaemonRunner.parse_argscCst|jr|jjy|jjWn6tjk
retdjd|j}|YnXt	j
}|jjd|}t||j
jdS)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 lockedrpidN)is_pidfile_staler
break_lockr r!lockfileZ
AlreadyLockedrr0r*getpid
start_messager1rrun)rerrorr9r5rrr_starts
	
zDaemonRunner._startcCsv|jj}ytj|tjWnItk
rq}z)tdjd|d|}|WYdd}~XnXdS)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}r9excN)	rread_pidr*killsignalSIGTERMOSErrorrr0)rr9rBr@rrr_terminate_daemon_processs	z&DaemonRunner._terminate_daemon_processcCs]|jjs0tdjd|j}|t|jrO|jjn
|jdS)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_lockedrr0r:r;rH)rr@rrr_stops	zDaemonRunner._stopcCs|j|jdS)z Stop, then start.
            N)rIrA)rrrr_restarts
zDaemonRunner._restartstartstopZrestartcCsNy|j|j}Wn3tk
rItdjd|j}|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}r8)r.r8KeyErrorrr0)rfuncr@rrr_get_action_funcs
	zDaemonRunner._get_action_funccCs|j}||dS)z Perform the requested action.

            :return: ``None``.

            The action is specified by the `action` attribute, which is set
            during `parse_args`.

            N)rO)rrNrrr	do_actions	zDaemonRunner.do_action)rrrrtypeZ
__metaclass__r>r
r6rrArHrIrJr.rOrPrrrrrEs	%rcCs<|dkrtj}|jdjd||jdS)z@ Emit a message to the specified stream (default `sys.stderr`). Nz
{message}
r5)r2r$writer0flush)r5streamrrrr1s	r1cCsst|ts-tdjd|}|tjj|s]tdjd|}|tj||}|S)z= Make a PIDLockFile instance with the given filesystem path. zNot a filesystem path: {path!r}r+zNot an absolute path: {path!r})	
isinstancer
ValueErrorr0r*r+isabsrZTimeoutPIDLockFile)r+Zacquire_timeoutr@r<rrrr%sr%cCsd}|j}|dk	rytj|tjWnTtk
rOd}Yn=tk
r}z|jtjkryd}WYdd}~XnX|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)	rCr*rDrESIG_DFLProcessLookupErrorrGerrnoZESRCH)rresultZpidfile_pidrBrrrr:%s	

r:)rZ
__future__rrr2r*rErZrY	NameErrorNotImplementedr<rdaemonrrrr		Exceptionr
rVrRuntimeErrorrrrr1r%r:rrrr<module>s,

Back to Directory File Manager