
    f8                         d Z ddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddl	mZ ddl	mZ ddlmZ dd	lmZ dd
lmZ  G d d      Zd Zd ZddZd Zedk(  r e        yy)a  supervisord -- run a set of applications as daemons.

Usage: %s [options]

Options:
-c/--configuration FILENAME -- configuration file path (searches if not given)
-n/--nodaemon -- run in the foreground (same as 'nodaemon=true' in config file)
-s/--silent -- no logs to stdout (maps to 'silent=true' in config file)
-h/--help -- print this usage message and exit
-v/--version -- print supervisord version number and exit
-u/--user USER -- run supervisord as this user (or numeric uid)
-m/--umask UMASK -- use this umask for daemon subprocess (default is 022)
-d/--directory DIRECTORY -- directory to chdir to when daemonized
-l/--logfile FILENAME -- use FILENAME as logfile path
-y/--logfile_maxbytes BYTES -- use BYTES to limit the max size of logfile
-z/--logfile_backups NUM -- number of backups to keep when max bytes reached
-e/--loglevel LEVEL -- use LEVEL as log level (debug,info,warn,error,critical)
-j/--pidfile FILENAME -- write a pid file for the daemon process to FILENAME
-i/--identifier STR -- identifier used for this instance of supervisord
-q/--childlogdir DIRECTORY -- the log directory for child process logs
-k/--nocleanup --  prevent the process from performing cleanup (removal of
                   old automatic child log files) at startup.
-a/--minfds NUM -- the minimum number of file descriptors for start success
-t/--strip_ansi -- strip ansi escape codes from process output
--minprocs NUM  -- the minimum number of processes available for start success
--profile_options OPTIONS -- run supervisord under profiler and output
                             results based on OPTIONS, which  is a comma-sep'd
                             list of 'cumulative', 'calls', and/or 'callers',
                             e.g. 'cumulative,callers')
    N)asyncore_25)	as_string)ServerOptions)decode_wait_status)signame)events)SupervisorStates)getProcessStateDescriptionc                   z    e Zd ZdZdZdZdZd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd ZddZddZd Zd Zy)
SupervisorFr   Nc                 .    || _         i | _        i | _        y N)optionsprocess_groupsticks)selfr   s     8/usr/lib/python3/dist-packages/supervisor/supervisord.py__init__zSupervisor.__init__6   s     
    c                    | j                   j                  s| j                   j                          | j                   j                          | j                   j                  r| j                   j	                          | j                   j                          | j                   j                  s| j                   j                          | j                          y r   )	r   firstcleanup_fdsset_uid_or_exitset_rlimits_or_exitmake_logger	nocleanupclear_autochildlogdirrunr   s    r   mainzSupervisor.main;   s    ||!! LL$$&$$&<<LL,,. 	  "||%%LL..0
r   c                 ^   i | _         d | _        t        j                          	 | j                  j
                  D ]  }| j                  |        | j                  j                  |        | j                  j                          | j                  j                  s0| j                  j                  r| j                  j                          | j                  j                          | j                          | j                  j                          y # | j                  j                          w xY wr   )r   stop_groupsr   clearr   process_group_configsadd_process_groupopenhttpservers
setsignalsnodaemonr   	daemonizewrite_pidfile
runforevercleanup)r   configs     r   r   zSupervisor.runP   s     	#,,<< /&&v./LL((.LL##%LL))t||/A/A&&( LL&&(OOLL  "DLL  "s   CD D,c                 ^   | j                   j                  }| j                  j                         D cg c]  }|j                   }}t        t        |D cg c]  }|j                   c}|            }t        t        |D cg c]  }|j                   c}|            }|D cg c]  }|j                  |vs| }}|D cg c]  }|j                  |vs| }	}|D cg c]#  }||j                  |j                  |      k7  r|% }
}||
|	fS c c}w c c}w c c}w c c}w c c}w c c}w r   )	r   r$   r   valuesr-   dictzipnameget)r   newgroupcurcfgcurdictnewdictcandaddedremovedchangeds              r   diff_to_activezSupervisor.diff_to_activeb   s   ll00)-)<)<)C)C)EFu||FFs4CHH4c:;s4CHH4c:;$'DD499G+C4DD$'DD499G+C4DD$' <Dgkk$))T::  < < gw&& G44DD<s/   DDD&D :D D%D%"(D*c                     |j                   }|| j                  vrV|j                          |j                         | j                  |<   t	        j
                  t	        j                  |             yy)NTF)r2   r   after_setuid
make_groupr   notifyProcessGroupAddedEvent)r   r-   r2   s      r   r%   zSupervisor.add_process_groupq   s[    {{t***!(.(9(9(;D%MM&77=>r   c                     | j                   |   j                         ry| j                   |   j                          | j                   |= t        j                  t        j
                  |             y)NFT)r   get_unstopped_processesbefore_remover   rB   ProcessGroupRemovedEvent)r   r2   s     r   remove_process_groupzSupervisor.remove_process_groupz   s[    t$<<>D!//1%f55d;<r   c                     i }| j                   j                         D ]!  }|j                  |j                                # |S r   )r   r/   updateget_dispatchers)r   process_mapr5   s      r   get_process_mapzSupervisor.get_process_map   sB    ((//1 	8Eu4467	8r   c                 t   g }| j                   j                         D ]!  }|j                  |j                                # |rt	        j                         }|| j
                  dz   kD  r|D cg c]!  }t        |j                  j                        # }}dj                  |      }| j                  j                  j                  d|z         || _        |D ]Y  }t        |j                               }| j                  j                  j                  |j                  j                  d|       [ |S c c}w )N   z, zwaiting for %s to diez state: )r   r/   extendrE   timelastshutdownreportr   r-   r2   joinr   loggerinfor
   	get_stateblather)	r   	unstoppedr5   nowpnamesnamestrprocstates	            r   shutdown_reportzSupervisor.shutdown_report   s   	((//1 	>EU::<=	> ))+Cd--12<EGq)AHHMM2GG))E*##(()@7)JK*-'% DD6t~~7GHELL''//*.++*:*:EBDD  Hs   -&D5c                 X    | j                   r| j                   d   j                          y y )N)r"   stop_allr   s    r   ordered_stop_groups_phase_1z&Supervisor.ordered_stop_groups_phase_1   s'    R ))+ r   c                     | j                   rG| j                   j                         }|j                         r| j                   j                  |       y y y r   )r"   poprE   append)r   r5   s     r   ordered_stop_groups_phase_2z&Supervisor.ordered_stop_groups_phase_2   sM     $$((*E,,.
   ''. / r   c                    t        j                  t        j                                d}| j                  j	                         }	 i }|j                  |       |j                  | j                                t        | j                  j                               }|j                          | j                  j                  t        j                  k  rt| j                  s8d| _        |d d  | _        t        j                  t        j                                 | j#                          | j%                         st&        j(                  |j+                         D ]p  \  }}|j-                         r%| j                  j.                  j1                  |       |j3                         sL| j                  j.                  j5                  |       r | j                  j.                  j7                  |      \  }}|D ]y  }||v s	 ||   }| j                  j8                  j;                  d|       |j=                          |j-                         s%| j                  j.                  j?                  |       { |D ]y  }||v s	 ||   }| j                  j8                  j;                  d|       |jC                          |j3                         s%| j                  j.                  jE                  |       { |D ]  }	|	jG                           | jI                          | jK                          | jM                          | j                  j                  t        j                  k  r| jO                          | j                  jP                  ry ># t&        j(                  $ r   ||   jA                          Y xY w# t&        j(                  $ r   ||   jA                          Y lxY w)N   Tz#read event caused by %(dispatcher)r)
dispatcherz$write event caused by %(dispatcher)r))r   rB   SupervisorRunningEventr   get_socket_maprJ   rM   listr   r/   sortmoodr	   RUNNINGstoppingr"   SupervisorStoppingEventrc   r_   asyncoreExitNowitemsreadablepollerregister_readablewritableregister_writablepollrT   rW   handle_read_eventunregister_readablehandle_errorhandle_write_eventunregister_writable
transitionreaphandle_signaltickrg   test)
r   timeout
socket_mapcombined_mappgroupsfdrj   rwr5   s
             r   r+   zSupervisor.runforever   s9   f3356\\002
L
+ 4 4 674..5578GLLN||  #3#;#;;}} %)DM'.qzD$MM&"@"@"BC002++- #***"."4"4"6 >J&&(LL''99"=&&(LL''99"=	> <<&&++G4DAq 8%8%1"%5
++33A'1 4 3 #446)224 LL//CCBG8  8%8%1"%5
++33B'1 4 3 #557)224 LL//CCBG8 ! #  "# IIK IIK||  #3#;#;;002||  Q R $++ 8$R(557 $++ 8$R(557s   ;A1N9A1N1(N.1(Oc                 \   |t        j                          }t        j                  D ]  }|j                  }| j                  j                  |      }|t        ||      x}| j                  |<   t        ||      }||k7  sY|| j                  |<   t        j                   |||               y)zo Send one or more 'tick' events when the timeslice related to
        the period for the event type rolls over N)rQ   r   TICK_EVENTSperiodr   r3   	timeslicerB   )r   rY   eventr   	last_tick	this_ticks         r   r   zSupervisor.tick   s     ;))+C'' 		6E\\F

v.I 1:631GG	DJJv.!&#.II%%.

6"eIt45		6r   c                    |dk(  ry | j                   j                         \  }}|r| j                   j                  j                  |d       }|;t	        |      \  }}| j                   j
                  j                  d|d|d       n)|j                  ||       | j                   j                  |= |s| j                  d|dz          y y y )Nd   zreaped unknown pid z ()Fri   )oncerecursionguard)	r   waitpid
pidhistoryr3   r   rT   rU   finishr   )r   r   r   pidstsprocess_msgs           r   r   zSupervisor.reap  s    S <<'')Sll--11#t<G+C03##((c)RSsC(LL++C0 		u^A5E	F  r   c                    | j                   j                         }|rD|t        j                  t        j                  t        j
                  fv rQ| j                   j                  j                  dt        |      z         t        j                  | j                   _        y |t        j                  k(  r| j                   j                  t        j                  k(  r2| j                   j                  j                  dt        |      z         y | j                   j                  j                  dt        |      z         t        j                  | j                   _        y |t        j                  k(  r2| j                   j                  j                  dt        |      z         y |t        j                   k(  r{| j                   j                  j#                  dt        |      z         | j                   j%                          | j&                  j)                         D ]  }|j%                           y | j                   j                  j+                  dt        |      z         y y )Nz#received %s indicating exit requestz<ignored %s indicating restart request (shutdown in progress)z&received %s indicating restart requestz#received %s indicating a child quitz)received %s indicating log reopen requestzreceived %s indicating nothing)r   
get_signalsignalSIGTERMSIGINTSIGQUITrT   warnr   r	   SHUTDOWNro   SIGHUP
RESTARTINGSIGCHLDdebugSIGUSR2rU   
reopenlogsr   r/   rW   )r   sigr5   s      r   r   zSupervisor.handle_signal   s   ll%%'v~~v}}fnnEE##((9GCLHJ$4$=$=!%<<$$(8(A(AALL'',,VY`adYeeg LL'',,@73<OQ(8(C(CDLL%&##))9GCLHJ&##((?'#,NP'')!00779 'E$$&' ##++4ws|CE/ r   c                 .    | j                   j                  S r   )r   ro   r   s    r   rV   zSupervisor.get_state<  s    ||   r   r   )Fr   )__name__
__module____qualname__rq   rR   r   r"   r   r    r   r>   r%   rH   rM   r_   rc   rg   r+   r   r   r   rV    r   r   r   r   0   sf    HNK
*#$'(,
/N`6"G"E8!r   r   c                 $    t        ||| z  z
        S r   )int)r   whens     r   r   r   ?  s    ttf}%&&r   c                    	 dd l }dd l}dd l}|j                         \  }}		 |j                  | |||	       |j                  |	      }
|
j                           |
j                  |xs d  |r|
j                  d       n|
j                  d       t        j                  |	       y # t        $ r dd l}Y w xY w# t        j                  |	       w xY w)Nr   )
cumulativecallsrQ   g333333?)cProfileImportErrorprofilepstatstempfilemkstemprunctxStats
strip_dirs
sort_statsprint_callersprint_statsosremove)cmdglobalslocals
sort_ordercallersr   r   r   r   fnstatss              r   r   r   C  s    " FBsGVR0R *G(GH#b!
		"!    			"s   B% A-B8 %B54B58Cc                    t         j                  dk(  sJ d       d}	 t               }|j                  | t               ||_        ||_        |j                  r/|j                  \  }}t        dt               t               ||       nt        |       |j                          |j                          d}|s|j                  t        j                   k  ry )Nposixz)This code makes Unix-specific assumptionsT)doczgo(options)F)r   r2   r   realize__doc__r   r   profile_optionsr   r   r   goclose_httpserversclose_loggerro   r	   r   )argsr   r   r   r   r   s         r   r    r    Z  s    77gJJJE
/'*""")"9"9JM79fh
GLwK!!#GLL#3#>#>> r   c                 n    t        |       }	 |j                          y # t        j                  $ r Y y w xY wr   )r   r    rs   rt   )r   ds     r   r   r   n  s2    7A	 s    44__main__)NF)r   r   rQ   r   supervisor.medusar   rs   supervisor.compatr   supervisor.optionsr   r   r   
supervisorr   supervisor.statesr	   r
   r   r   r   r    r   r   r   r   r   <module>r      sb   > 
   5 ' , 1 &  . 8M! M!^'.( zF r   