Ϫf4RdZddlmZmZmZddlZddlZddlmZddl m Z m Z m Z ddlmZddlmZddlmZej(d d Gd d ZGd dZeZGddej0ZGdde j4ZGddej8Zy)zA Support for starting, monitoring, and restarting child process. )DictListOptionalN)service)errorprotocolreactor)Logger)basic) deprecateT)frozen auto_attribsceZdZUdZeeed<dZee ed<dZ ee ed<e je jeZeeefed<dZeeed<ej&ej*d d d d d Zy)_Processa" The parameters of a process to be restarted. @ivar args: command-line arguments (including name of command as first one) @type args: C{list} @ivar uid: user-id to run process as, or None (which means inherit uid) @type uid: C{int} @ivar gid: group-id to run process as, or None (which means inherit gid) @type gid: C{int} @ivar env: environment for process @type env: C{dict} @ivar cwd: initial working directory for process or None (which means inherit cwd) @type cwd: C{str} argsNuidgid)defaultenvcwdTwistedrc^|j|j|j|jfS)a Convert process to tuple. Convert process to tuple that looks like the legacy structure of processes, for potential users who inspected processes directly. This was only an accidental feature, and will be removed. If you need to remember what processes were added to a process monitor, keep track of that when they are added. The process list inside the process monitor is no longer a public API. This allows changing the internal structure of the process list, when warranted by bug fixes or additional features. @return: tuple representation of process )rrrrselfs 8/usr/lib/python3/dist-packages/twisted/runner/procmon.pytoTuplez_Process.toTuple0s#& 488TXXtxx88)__name__ __module__ __qualname____doc__rstr__annotations__rrintrattribFactorydictrrrr deprecated incrementalVersionrrrrrs( s)OC#C#!$'',$,,t*<=Cc3h=C#Y-+--iQBC9D9rrceZdZdZy)DummyTransportrN)r r!r" disconnectingr.rrr0r0FsMrr0c"eZdZdZdZdZdZdZy) LineLoggerN c |jd}|jjj d|j ||jy#t$rt|}YTwxYw)Nzutf-8z[{tag}] {line})taglinestream)decodeUnicodeDecodeErrorreprrloginfor6r8)rr7s r lineReceivedzLineLogger.lineReceivedSs_ ;;w'D  $((dkk  " :D sAA('A()r r!r"r6r8 delimiterrr>r.rrr3r3Ms C FIG rr3cLeZdZdZdZdZdZdZdZe dZ e dZ y)LoggingProtocolNct|_|j|j_d|j_|j |j_d|_t|_|j|j_d|j_|j |j_d|_|jjt|jjty)NstdoutTstderr) r3_outputnamer6r8r _outputEmpty_error _errorEmptymakeConnection transportrs rconnectionMadezLoggingProtocol.connectionMadebs!| 99 & #||   l )) % "ll  ##I. ""9-rcT|jj||ddk(|_yNr4)rE dataReceivedrGrdatas r outReceivedzLoggingProtocol.outReceivedrs% !!$' H-rcT|jj||ddk(|_yrN)rHrPrIrQs r errReceivedzLoggingProtocol.errReceivedvs%   &8u,rc|js|jjd|js|jjd|j j |jy)Nr4)rGrErPrIrHrconnectionLostrF)rreasons r processEndedzLoggingProtocol.processEndedzsN  LL % %e , KK $ $U + ##DII.rc|jSN)rErs routputzLoggingProtocol.outputs ||rc|jSr[)rGrs remptyzLoggingProtocol.emptys   r) r r!r"rrFrLrSrUrYpropertyr\r^r.rrrArA^sHG D. .-/!!rrAc0eZdZdZdZdZdZdZeZ e fdZ e jejdddd d Ze j"ejdddd d Zd d id fd ZdZdZdZdZdZdZdZdZdefdZy )ProcessMonitora  ProcessMonitor runs processes, monitors their progress, and restarts them when they die. The ProcessMonitor will not attempt to restart a process that appears to die instantly -- with each "instant" death (less than 1 second, by default), it will delay approximately twice as long before restarting it. A successful run will reset the counter. The primary interface is L{addProcess} and L{removeProcess}. When the service is running (that is, when the application it is attached to is running), adding a process automatically starts it. Each process has a name. This name string must uniquely identify the process. In particular, attempting to add two processes with the same name will result in a C{KeyError}. @type threshold: C{float} @ivar threshold: How long a process has to live before the death is considered instant, in seconds. The default value is 1 second. @type killTime: C{float} @ivar killTime: How long a process being killed has to get its affairs in order before it gets killed with an unmaskable signal. The default value is 5 seconds. @type minRestartDelay: C{float} @ivar minRestartDelay: The minimum time (in seconds) to wait before attempting to restart a process. Default 1s. @type maxRestartDelay: C{float} @ivar maxRestartDelay: The maximum time (in seconds) to wait before attempting to restart a process. Default 3600s (1h). @type _reactor: L{IReactorProcess} provider @ivar _reactor: A provider of L{IReactorProcess} and L{IReactorTime} which will be used to spawn processes and register delayed calls. @type log: L{Logger} @ivar log: The logger used to propagate log messages from spawned processes. icf||_i|_i|_i|_i|_i|_i|_yr[)_reactor _processes protocolsdelay timeStartedmurderrestart)rr s r__init__zProcessMonitor.__init__s4    rrrrrc|jjDcic]\}}||jc}}Scc}}w)zu Processes as dict of tuples @return: Dict of process name to monitored processes as tuples )rfitemsr)rrFprocesss r processeszProcessMonitor.processess6>B__=R=R=TUMD'goo''UUUs=ctjj|}|d=i|d<i|d<i|d<i|d<i|d<|d=|j|d<|S) Nrergrhrirjrkrfrp)rService __getstate__rp)rdcts rrszProcessMonitor.__getstate__sgoo**40  OKG MH I  >>K rNc||jvrtd|dt||||||j|<|j|j|<|j r|j |yy)a Add a new monitored process and start it immediately if the L{ProcessMonitor} service is running. Note that args are passed to the system call, not to the shell. If running the shell is desired, the common idiom is to use C{ProcessMonitor.addProcess("name", ['/bin/sh', '-c', shell_script])} @param name: A name for this process. This value must be unique across all processes added to this monitor. @type name: C{str} @param args: The argv sequence for the process to launch. @param uid: The user ID to use to run the process. If L{None}, the current UID is used. @type uid: C{int} @param gid: The group ID to use to run the process. If L{None}, the current GID is used. @type uid: C{int} @param env: The environment to give to the launched process. See L{IReactorProcess.spawnProcess}'s C{env} parameter. @type env: C{dict} @param cwd: The initial working directory of the launched process. The default of C{None} means inheriting the laucnhing process's working directory. @type env: C{dict} @raise KeyError: If a process with the given name already exists. zremove z firstN)rfKeyErrorrminRestartDelayrhrunning startProcess)rrFrrrrrs r addProcesszProcessMonitor.addProcesssm8 4?? "WTF&12 2 (sCc B// 4 <<   d # rc@|j||j|=y)z Stop the named process and remove it from the list of monitored processes. @type name: C{str} @param name: A string that uniquely identifies the process. N) stopProcessrfrrFs r removeProcesszProcessMonitor.removeProcesss  OOD !rctjj|t|jD]}|j |y)z0 Start all monitored processes. N)rrr startServicelistrfryr}s rrzProcessMonitor.startService s< $$T*) $D   d # $rc0tjj|t|jj D]&\}}|j s|j(t|jD]}|j|y)zY Stop all monitored processes and cancel all scheduled process restarts. N) rrr stopServicerrkrnactivecancelrfr|)rrF delayedCalls rrzProcessMonitor.stopServices~ ##D)"&dll&8&8&:!; % D+!!#""$ %) #D   T " #rc||jvrG|j|jr|j|j|j|=|j|=|jj |j |z |jkrC|j|}t|j|dz|j|j|<nd}|j|j|<|jrD||jvr5|jj||j||j |<yyy)a Called when a monitored processes exits. If L{service.IService.running} is L{True} (ie the service is started), the process will be restarted. If the process had been running for more than L{ProcessMonitor.threshold} seconds it will be restarted immediately. If the process had been running for less than L{ProcessMonitor.threshold} seconds, the restart will be delayed and each time the process dies before the configured threshold, the restart delay will be doubled - up to a maximum delay of maxRestartDelay sec. @type name: C{str} @param name: A string that uniquely identifies the process which exited. rN)rjrrrgresecondsri thresholdrhminmaxRestartDelayrwrxrf callLaterryrk)rrF nextDelays rrWzProcessMonitor.connectionLost!s$ 4;; {{4 '') D!((* D! NN4 == "T%5%5d%; ;dnn L 4(I"4::d#3a#79M9MNDJJt  I#33DJJt  <> ! //$'!  $t!%!6!6!8 ""  LLO LL    # rcZ |jdy#tj$rYywxYw)z? @param proc: An L{IProcessTransport} provider KILLN) signalProcessrProcessExitedAlready)rprocs r_forceStopProcessz ProcessMonitor._forceStopProcesses-    v &))   s **cd||jvrtd||jj|d}|]|j} |j d|j j|j|j||j|<yy#tj$rYywxYw)zD @param name: The name of the process to be stopped zUnrecognized process name: NTERM) rfrvrggetrKrrerkillTimerrjrr)rrFrrs rr|zProcessMonitor.stopProcessns t &8?@ @""4.  ??D ""6*%)MM$;$;MM4#9#94% D! --  sBB/.B/cH|jD]}|j|y)z Restart all processes. This is useful for third party management services to allow a user to restart servers because of an outside change in circumstances -- for example, a new version of a library is installed. N)rfr|r}s r restartAllzProcessMonitor.restartAlls%OO #D   T " #rreturncg}|jjD]{\}}d}|jt|j}|j|dt|jzz }|rd|zdz}|j ||d|j }d|jjzdzdj|zdzS) N:()z: < >) rfrnrr$rappendr __class__r join)rlstrFruidgids r__repr__zProcessMonitor.__repr__s////1 ;JD$Fxx#TXXxx##DHH --v+ JJ$&DII=9 : ;T^^,,,s2SXXc]BSHHr)r r!r"r#rrrwrr r<rerlr deprecatedPropertyr,r-rpr+rsrzr~rrrWryrr|rr$rr.rrraras*XIHOO (C'"Y!!"5+"5"5iQ"JKVLVY-+--iQBC D *.4RT!$F "$ #(T 4&# I# Irra)r#typingrrrr'r,twisted.applicationrtwisted.internetrrr retwisted.loggerr twisted.protocolsr twisted.pythonr srr0rK LineReceiverr3ProcessProtocolrArrrar.rrrs (' 'AA!#$t$'.9.9(.9b    ## ")!h..)!XMIW__MIr