o
    r¨Êh®8  ã                   @   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   úH/var/www/html/env/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 r°z|| }| jjjd|d� | ¡  | ¡ s›| jj |¡ W qx tjy¥   ‚    ||   ¡  Y qxqx|D ]8}||v rëz|| }| jjjd|d� | !¡  | ¡ sÖ| jj "|¡ W q³ tjyà   ‚    ||   ¡  Y q³q³|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 )Néd   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,     

ÿ