o
    f8                     @   s   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 dZdd Zdd ZdddZdd Zedkr_e  dS dS )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                   @   s   e Zd ZdZdZdZdZdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd"ddZd#ddZdd Zd d! ZdS )$
SupervisorFr   Nc                 C   s   || _ i | _i | _d S N)optionsprocess_groupsticks)selfr    r   I/var/www/html/venv/lib/python3.10/site-packages/supervisor/supervisord.py__init__6   s   
zSupervisor.__init__c                 C   sV   | j js	| j   | j   | j jr| j   | j   | j js%| j   |   d S r   )	r   firstcleanup_fdsset_uid_or_exitset_rlimits_or_exitmake_logger	nocleanupclear_autochildlogdirrunr   r   r   r   main;   s   




zSupervisor.mainc                 C   s   i | _ d | _t  z5| jjD ]}| | q| j|  | j  | jj	s/| jj
r/| j  | j  |   W | j  d S | j  w r   )r   stop_groupsr   clearr   process_group_configsadd_process_groupopenhttpservers
setsignalsnodaemonr   	daemonizewrite_pidfile
runforevercleanup)r   configr   r   r   r   P   s   



zSupervisor.runc                    s   | j j}dd | j D }ttdd |D | ttdd |D | fdd|D }fdd|D } fdd|D }|||fS )Nc                 S      g | ]}|j qS r   )r(   ).0groupr   r   r   
<listcomp>d       z-Supervisor.diff_to_active.<locals>.<listcomp>c                 S   r)   r   namer*   cfgr   r   r   r,   f   r-   c                 S   r)   r   r.   r0   r   r   r   r,   g   r-   c                       g | ]	}|j  vr|qS r   r.   r*   candcurdictr   r   r,   i       c                    r2   r   r.   r3   )newdictr   r   r,   j   r7   c                    s"   g | ]}|  |j|kr|qS r   )getr/   r3   r5   r   r   r,   l   s    )r   r   r   valuesdictzip)r   newcuraddedremovedchangedr   )r6   r8   r   diff_to_activeb   s   
zSupervisor.diff_to_activec                 C   s>   |j }|| jvr|  | | j|< tt| dS dS )NTF)r/   r   after_setuid
make_groupr   notifyProcessGroupAddedEvent)r   r(   r/   r   r   r   r    q   s   
zSupervisor.add_process_groupc                 C   s<   | j |  r	dS | j |   | j |= tt| dS )NFT)r   get_unstopped_processesbefore_remover   rE   ProcessGroupRemovedEvent)r   r/   r   r   r   remove_process_groupz   s   zSupervisor.remove_process_groupc                 C   s&   i }| j  D ]	}||  q|S r   )r   r:   updateget_dispatchers)r   process_mapr+   r   r   r   get_process_map   s   zSupervisor.get_process_mapc                 C   s   g }| j  D ]	}||  q|rNt }|| jd krNdd |D }d|}| jj	d|  || _|D ]}t
| }| jjd|jj|f  q8|S )N   c                 S   s   g | ]}t |jjqS r   )r   r(   r/   )r*   pr   r   r   r,      s    z.Supervisor.shutdown_report.<locals>.<listcomp>z, zwaiting for %s to diez%s state: %s)r   r:   extendrG   timelastshutdownreportjoinr   loggerinfor	   	get_stateblatherr(   r/   )r   	unstoppedr+   nownamesnamestrprocstater   r   r   shutdown_report   s    
zSupervisor.shutdown_reportc                 C   s   | j r| j d   d S d S )N)r   stop_allr   r   r   r   ordered_stop_groups_phase_1   s   z&Supervisor.ordered_stop_groups_phase_1c                 C   s0   | j r| j  }| r| j | d S d S d S r   )r   poprG   append)r   r+   r   r   r   ordered_stop_groups_phase_2   s   
z&Supervisor.ordered_stop_groups_phase_2c           
   	   C   s*  t t   d}| j }	 i }|| ||   t| j	 }|
  | jjtjk rN| jsCd| _|d d  | _t t   |   |  sNtj| D ]\}}| ra| jj| | rl| jj| qR| jj|\}}|D ]8}||v rz|| }| jjjd|d |  | s| jj| W qx tjy       ||    Y qxqx|D ]8}||v rz|| }| jjjd|d |!  | s| jj"| W q tjy       ||    Y qq|D ]}	|	#  q| $  | %  | &  | jjtjk r| '  | jj(rd S q)N   Tz#read event caused by %(dispatcher)r)
dispatcherz$write event caused by %(dispatcher)r))r   rE   SupervisorRunningEventr   get_socket_maprK   rN   listr   r:   sortmoodr   RUNNINGstoppingr   SupervisorStoppingEventrb   r_   asyncoreExitNowitemsreadablepollerregister_readablewritableregister_writablepollrU   rX   handle_read_eventunregister_readablehandle_errorhandle_write_eventunregister_writable
transitionreaphandle_signaltickre   test)
r   timeout
socket_mapcombined_mappgroupsfdrg   rwr+   r   r   r   r&      s   



zSupervisor.runforeverc                 C   sz   |du rt   }tjD ]/}|j}| j|}|du r$t|| }| j|< t||}||kr:|| j|< t|||  qdS )zo Send one or more 'tick' events when the timeslice related to
        the period for the event type rolls over N)rR   r   TICK_EVENTSperiodr   r9   	timeslicerE   )r   rZ   eventr   	last_tick	this_tickr   r   r   r      s   


zSupervisor.tickc                 C   s   |dkrd S | j  \}}|rE| j j|d }|d u r-t|\}}| j jd||f  n||| | j j|= |sG| jd|d d d S d S d S )Nd   zreaped unknown pid %s (%s)Frf   )oncerecursionguard)	r   waitpid
pidhistoryr9   r   rU   rV   finishr   )r   r   r   pidstsprocess_msgr   r   r   r     s   
zSupervisor.reapc                 C   s$  | j  }|r|tjtjtjfv r#| j jdt|  t	j
| j _d S |tjkrN| j jt	j
kr<| j jdt|  d S | j jdt|  t	j| j _d S |tjkr`| j jdt|  d S |tjkr| j jdt|  | j   | j D ]}|  qzd S | j jdt|  d S d S )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SIGQUITrU   warnr   r   SHUTDOWNrl   SIGHUP
RESTARTINGSIGCHLDdebugSIGUSR2rV   
reopenlogsr   r:   rX   )r   sigr+   r   r   r   r      s@   











zSupervisor.handle_signalc                 C   s   | j jS r   )r   rl   r   r   r   r   rW   <  s   zSupervisor.get_stater   )Fr   )__name__
__module____qualname__rn   rS   r   r   r   r   r   rB   r    rJ   rN   r_   rb   re   r&   r   r   r   rW   r   r   r   r   r
   0   s(    	
P
r
   c                 C   s   t |||   S r   )int)r   whenr   r   r   r   ?  s   r   c              	   C   s   zdd l }W n ty   dd l}Y nw dd l}dd l}| \}}	z5|| |||	 ||	}
|
  |
j	|p9d  |rC|

d n|
d W t|	 d S W t|	 d S t|	 w )Nr   )
cumulativecallsrR   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statsr   r   r   r   C  s&   
r   Fc                 C   s   t jdks	J dd}	 t }|j| td ||_||_|jr/|j\}}tdt	 t
 || nt| |  |  d}|sE|jtjk rGd S q)Nposixz)This code makes Unix-specific assumptionsTrf   )doczgo(options)F)r   r/   r   realize__doc__r   r   profile_optionsr   r   r   goclose_httpserversclose_loggerrl   r   r   )argsr   r   r   r   r   r   r   r   r   Z  s"   
r   c                 C   s.   t | }z|  W d S  tjy   Y d S w r   )r
   r   rp   rq   )r   dr   r   r   r   n  s   r   __main__)NF)r   r   rR   r   supervisor.medusar   rp   supervisor.compatr   supervisor.optionsr   r   r   
supervisorr   supervisor.statesr   r	   r
   r   r   r   r   r   r   r   r   r   <module>   s,     

