
sVc           @  s   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 m Z m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z m Z d e f d	     YZ d S(
   i(   t   print_functionN(   t
   HaltServert   AppImportError(   t   Pidfile(   t   create_sockets(   t   util(   t   __version__t   SERVER_SOFTWAREt   Arbiterc           B  s  e  Z d  Z d Z d Z i  Z g  Z i  Z g  Z g  Z	 g  d j
   D] Z e e d e  ^ qC Z e d   e e  D  Z d   Z d   Z d   Z e e 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 d  Z' d   Z( e) d  Z* d   Z+ d   Z, d   Z- d    Z. d!   Z/ d"   Z0 d#   Z1 d$   Z2 d%   Z3 RS('   s   
    Arbiter maintain the workers processes alive. It launches or
    kills them if needed. It also manages application reloading
    via SIGHUP/USR2.
    i   i   s+   HUP QUIT INT TERM TTIN TTOU USR1 USR2 WINCHs   SIG%sc         c  sN   |  ]D } | d   d k r | d  d k r t  t |  | d  j   f Vq d S(   i   t   SIGt   _N(   t   getattrt   signalt   lower(   t   .0t   name(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pys	   <genexpr>2   s    c         C  s   t  t j d <d  |  _ d  |  _ d  |  _ |  j |  d  |  _ d |  _	 d |  _
 d |  _ t j   } t j } | j d t j  i | d 6| d 6t j d 6|  _ d  S(   NR   i    t   Mastert   argst   cwd(   R   t   ost   environt   Nonet   _num_workerst    _last_logged_active_worker_countt   logt   setupt   pidfilet
   worker_aget
   reexec_pidt   master_nameR   t   getcwdt   syst   argvt   insertt
   executablet	   START_CTX(   t   selft   appR   R   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   __init__6   s     							
c         C  s   |  j  S(   N(   R   (   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   _get_num_workersP   s    c         C  s,   |  j  } | |  _  |  j j |  | |  d  S(   N(   R   t   cfgt   nworkers_changed(   R$   t   valuet	   old_value(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   _set_num_workersS   s    		c         C  sO  | |  _  | j |  _ |  j d  k r? |  j j | j  |  _ n  d t j k r^ |  j j   n  |  j j |  _ |  j j	 |  _	 |  j j
 |  _ |  j j |  _ |  j j |  _ |  j j d j d j d   t |  j j j   d d   D    |  j j r/x0 |  j j j   D] \ } } | t j | <qWn  |  j j rK|  j  j   n  d  S(   Nt   GUNICORN_FDs   Current configuration:
{0}s   
c         s  s*   |  ]  \ } } d  j  | | j  Vq d S(   s
     {0}: {1}N(   t   formatR*   (   R   t   configR*   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pys	   <genexpr>l   s   t   keyc         S  s   |  d S(   Ni   (    (   t   setting(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   <lambda>o   s    (   R%   R(   R   R   t   logger_classR   R   t   reopen_filest   worker_classt   addresst   workerst   num_workerst   timeoutt	   proc_namet   debugR.   t   joint   sortedt   settingst   itemst   envt   preload_appt   wsgi(   R$   R%   t   kt   v(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR   Y   s*    	c         C  sL  |  j  j d t  t j   |  _ |  j j d k	 r_ t	 |  j j  |  _ |  j j
 |  j  n  |  j j |   |  j   |  j s t |  j |  j   |  _ n  d j g  |  j D] } t |  ^ q  } |  j  j d  |  j  j d | |  j  |  j  j d |  j j  t |  j d  r8|  j j |  j |  j   n  |  j j |   d S(   sS           Initialize the arbiter. Start listening and set pidfile if needed.
        s   Starting gunicorn %st   ,s   Arbiter booteds   Listening at: %s (%s)s   Using worker: %st   check_configN(   R   t   infoR   R   t   getpidt   pidR(   R   R   R   t   createt   on_startingt   init_signalst	   LISTENERSR   R<   t   strR;   t   worker_class_strt   hasattrR5   RF   t
   when_ready(   R$   t   lt   listeners_str(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   starty   s     
	+c         C  s   |  j  r/ g  |  j  D] } t j |  ^ q n  t j   |  _  } x( | D]  } t j |  t j |  qI W|  j j   g  |  j D] } t	 j	 | |  j	  ^ q t	 j	 t	 j
 |  j  d S(   s           Initialize master signal handling. Most of the signals
        are queued. Child signals only wake up the master.
        N(   t   PIPER   t   closet   pipeR   t   set_non_blockingt   close_on_execR   t   SIGNALSR   t   SIGCHLDt   handle_chld(   R$   t   pt   pairt   s(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyRL      s    	&)c         C  s6   t  |  j  d k  r2 |  j j |  |  j   n  d  S(   Ni   (   t   lent	   SIG_QUEUEt   appendt   wakeup(   R$   t   sigt   frame(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR      s    c         C  s  |  j    t j d |  j  y|  j   x t r!t |  j  rU |  j j d  n d } | d k r |  j
   |  j   |  j   q. n  | |  j k r |  j j d |  q. n  |  j j |  } t |  d | d  } | s |  j j d |  q. n  |  j j d |  |   |  j   q. WWn t k
 r@|  j   n t k
 rZ|  j   n t k
 r} |  j d | j d | j  nv t k
 r  nc t k
 r|  j j d	 t j    |  j t  |  j d k	 r|  j j    n  t! j" d
  n Xd S(   s   Main master loop.s   master [%s]i    s   Ignoring unknown signal: %ss	   handle_%ss   Unhandled signal: %ss   Handling signal: %st   reasont   exit_statuss$   Unhandled exception in main loop:
%siN(#   RT   R   t   _setproctitleR:   t   manage_workerst   TrueR`   Ra   t   popR   t   sleept   murder_workerst	   SIG_NAMESR   RG   t   getR   t   errorRc   t   StopIterationt   haltt   KeyboardInterruptR   Rf   Rg   t
   SystemExitt	   Exceptiont	   tracebackt
   format_exct   stopt   FalseR   t   unlinkR   t   exit(   R$   Rd   t   signamet   handlert   inst(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   run   sJ    

	'


c         C  s   |  j    |  j   d S(   s   SIGCHLD handlingN(   t   reap_workersRc   (   R$   Rd   Re   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR\      s    
c         C  s$   |  j  j d |  j  |  j   d S(   s           HUP handling.
        - Reload configuration
        - Start the new worker processes with a new configuration
        - Gracefully shutdown the old worker processes
        s   Hang up: %sN(   R   RG   R   t   reload(   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt
   handle_hup   s    c         C  s
   t   d S(   s   SIGTERM handlingN(   Rq   (   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   handle_term   s    c         C  s   |  j  t  t  d S(   s   SIGINT handlingN(   Rx   Ry   Rq   (   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt
   handle_int   s    c         C  s   |  j  t  t  d S(   s   SIGQUIT handlingN(   Rx   Ry   Rq   (   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   handle_quit   s    c         C  s   |  j  d 7_  |  j   d S(   sR           SIGTTIN handling.
        Increases the number of workers by one.
        i   N(   R8   Ri   (   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   handle_ttin   s    c         C  s0   |  j  d k r d S|  j  d 8_  |  j   d S(   sR           SIGTTOU handling.
        Decreases the number of workers by one.
        i   N(   R8   Ri   (   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   handle_ttou   s    c         C  s!   |  j  j   |  j t j  d S(   sU           SIGUSR1 handling.
        Kill all workers by sending them a SIGUSR1
        N(   R   R4   t   kill_workersR   t   SIGUSR1(   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   handle_usr1  s    c         C  s   |  j    d S(   s           SIGUSR2 handling.
        Creates a new master/worker set as a slave of the current
        master without affecting old workers. Use this to do live
        deployment with the ability to backout a change.
        N(   t   reexec(   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   handle_usr2  s    c         C  sL   |  j  j r8 |  j j d  d |  _ |  j t j  n |  j j d  d S(   s   SIGWINCH handlings   graceful stop of workersi    s    SIGWINCH ignored. Not daemonizedN(	   R(   t   daemonR   RG   R8   R   R   t   SIGTERMR;   (   R$   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   handle_winch  s
    	c         C  sV   y t  j |  j d d  Wn4 t k
 rQ } | j t j t j g k rR   qR n Xd S(   s;           Wake up the arbiter by writing to the PIPE
        i   t   .N(   R   t   writeRU   t   IOErrort   errnot   EAGAINt   EINTR(   R$   t   e(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyRc   "  s
    i    c         C  s   |  j    |  j j d |  j  | d k	 rB |  j j d |  n  |  j d k	 ra |  j j   n  |  j j |   t	 j
 |  d S(   s    halt arbiter s   Shutting down: %ss
   Reason: %sN(   Rx   R   RG   R   R   R   Rz   R(   t   on_exitR   R{   (   R$   Rf   Rg   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyRr   ,  s    
c         C  s   yT t  j  |  j d g g  g  d  } | d s3 d Sx t j |  j d d  rR q6 WWn t  j k
 r } | j d t j t j g k r   q nN t	 k
 r } | j t j t j g k r   q n t
 k
 r t j   n Xd S(   sm           Sleep until PIPE is readable or we timeout.
        A readable PIPE means a signal occurred.
        i    g      ?Ni   (   t   selectRU   R   t   readRp   R   R   R   R   t   OSErrorRs   R   R{   (   R$   t   readyR   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyRl   7  s    "
		c         C  s   g  |  _  t j } | s$ t j } n  t j   |  j j } |  j |  x, |  j ru t j   | k  ru t j	 d  qJ W|  j t j
  d S(   s           Stop workers

        :attr graceful: boolean, If True (the default) workers will be
        killed gracefully  (ie. trying to wait for the current connection)
        g?N(   RM   R   R   t   SIGQUITt   timeR(   t   graceful_timeoutR   t   WORKERSRl   t   SIGKILL(   R$   t   gracefulRd   t   limit(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyRx   K  s    		c         C  s   |  j  d k	 r, |  j  j d |  j  j  n  t j   |  _ |  j d k rW d |  _ d S|  j j	 j
   } g  |  j D] } | j   ^ qs } d j g  | D] } t |  ^ q  | d <t j |  j d  |  j j |   t j |  j d |  j d |  d S(	   s1           Relaunch the master and workers.
        s	   %s.oldbini    s
   Old MasterNRE   R-   R   R   (   R   R   t   renamet   fnameR   t   forkR   R   R(   t   env_origt   copyRM   t   filenoR<   RN   t   chdirR#   t   pre_exect   execvpe(   R$   R   RR   t   fdst   fd(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR   _  s    	",c         C  s  |  j  j } xb |  j  j D]T } | |  j  j k rK |  j  j | t j | <q y t j | =Wq t k
 rl q Xq W|  j j   |  j	 |  j  |  j
 j   | |  j  j k rg  |  j D] } | j   ^ q t |  j  |  j
  |  _ |  j
 j d d j t |  j    n  |  j  j |   |  j d  k	 r<|  j j   n  |  j  j d  k	 ryt |  j  j  |  _ |  j j |  j  n  t j d |  j  x$ t |  j  j  D] } |  j   qW|  j   d  S(   Ns   Listening at: %sRE   s   master [%s](    R(   R6   R@   R   R   R   t   KeyErrorR%   R   R   R   R4   RM   RV   R   RG   R<   RN   t	   on_reloadR   R   Rz   R   RJ   RI   R   Rh   R:   t   rangeR7   t   spawn_workerRi   (   R$   t   old_addressRC   RR   t   i(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR   u  s2     (c         C  s   |  j  s d St |  j j    } x | D] \ } } y, t j   | j j   |  j  k r` w) n  Wn t t f k
 r} q) n X| j	 s |  j
 j d |  t | _	 |  j | t j  q) |  j | t j  q) Wd S(   s)           Kill unused/idle workers
        Ns   WORKER TIMEOUT (pid:%s)(   R9   t   listR   R?   R   t   tmpt   last_updateR   t
   ValueErrort   abortedR   t   criticalRj   t   kill_workerR   t   SIGABRTR   (   R$   R7   RI   t   worker(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyRm     s    	"
		c         C  s  y x t  r t j d t j  \ } } | s1 Pn  |  j | k rL d |  _ q | d ?} | |  j k r d } t | |  j   n  | |  j k r d } t | |  j   n  |  j j	 | d  } | s q n  | j j   q WWn+ t k
 r
} | j t j k r  qn Xd S(   s7           Reap workers to avoid zombie processes
        ii    i   s   Worker failed to boot.s   App failed to load.N(   Rj   R   t   waitpidt   WNOHANGR   t   WORKER_BOOT_ERRORR   t   APP_LOAD_ERRORR   Rk   R   R   RV   R   R   t   ECHILD(   R$   t   wpidt   statust   exitcodeRf   R   R   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR     s*    	
c         C  s   t  |  j j    |  j k  r+ |  j   n  |  j j   } t | d d   } xA t  |  |  j k r | j d  \ } } |  j | t	 j
  qR Wt  |  } |  j | k r | |  _ |  j j d j |  d i d d 6| d 6d	 d
 6n  d S(   s[           Maintain the number of workers by spawning or killing
        as required.
        R0   c         S  s   |  d j  S(   Ni   (   t   age(   t   w(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR2     s    i    s   {0} workerst   extras   gunicorn.workerst   metricR*   t   gauget   mtypeN(   R`   R   t   keysR8   t   spawn_workersR?   R=   Rk   R   R   R   R   R   R;   R.   (   R$   R7   RI   R
   t   active_worker_count(    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyRi     s    	
c         C  s  |  j  d 7_  |  j |  j  |  j |  j |  j |  j d |  j |  j  } |  j j |  |  t	 j
   } | d k r | |  j | <| St	 j   } zyU t j d |  j  |  j j d |  |  j j |  |  | j   t j d  Wn t k
 r   n t k
 r^} |  j j d t j    t d | d t j t j j   t j |  j  nI |  j j d	 t j    | j st j |  j   n  t j d
  n XWd  |  j j d |  y$ | j! j"   |  j j# |  |  Wn n XXd  S(   Ni   g       @i    s   worker [%s]s   Booting worker with pid: %ss,   Exception while loading the application: 
%ss   %st   files   Exception in worker process:
%sis   Worker exiting (pid: %s)($   R   R5   RI   RM   R%   R9   R(   R   t   pre_forkR   R   R   RH   R   Rh   R:   RG   t	   post_forkt   init_processR   R{   Rt   R   R;   Rv   Rw   t   printt   stderrt   flushR   t	   exceptiont   bootedR   R   RV   t   worker_exit(   R$   R   RI   t
   worker_pidR   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR     sJ     
	c         C  sR   xK t  |  j t |  j j     D]' } |  j   t j d t j    q# Wd S(   s           Spawn new workers as needed.

        This is where a worker process leaves the main loop
        of the master process.
        g?N(	   R   R8   R`   R   R   R   R   Rl   t   random(   R$   R   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR     s    )
c         C  s:   t  |  j j    } x | D] } |  j | |  q Wd S(   s^           Kill all workers with the signal `sig`
        :attr sig: `signal.SIG*` value
        N(   R   R   R   R   (   R$   Rd   t   worker_pidsRI   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR   '  s    c         C  s   y t  j | |  Wn t k
 r } | j t j k r y: |  j j |  } | j j   |  j	 j
 |  |  d SWq t t f k
 r d SXn    n Xd S(   sj           Kill a worker

        :attr pid: int, worker pid
        :attr sig: `signal.SIG*` value
         N(   R   t   killR   R   t   ESRCHR   Rk   R   RV   R(   R   R   (   R$   RI   Rd   R   R   (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR   0  s    N(4   t   __name__t
   __module__t   __doc__R   R   R#   RM   R   RU   Ra   t   splitt   xR   R   RZ   t   dictt   dirRn   R&   R'   R,   t   propertyR8   R   RT   RL   R   R\   R   R   R   R   R   R   R   R   R   Rc   R   Rr   Rl   Rj   Rx   R   R   Rm   R   Ri   R   R   R   R   (    (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyR      sX   )				 				+		
					
						
			5				)			(   t
   __future__R    R   R   R   R   R   R   R   Rv   t   gunicorn.errorsR   R   t   gunicorn.pidfileR   t   gunicorn.sockR   t   gunicornR   R   R   t   objectR   (    (    (    s`   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/gunicorn-19.4.5-py2.7.egg/gunicorn/arbiter.pyt   <module>   s   