ķ
P'^c           @@  s1  d  Z  d d l m 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 d d g Z e e  Z e j e j e j Z Z Z e e j j d d   Z d e
 j f d     YZ  d e f d     YZ! d S(   s;  
    celery.worker.autoscale
    ~~~~~~~~~~~~~~~~~~~~~~~

    This module implements the internal thread responsible
    for growing and shrinking the pool according to the
    current autoscale settings.

    The autoscale thread is only enabled if :option:`--autoscale`
    has been enabled on the command-line.

i    (   t   absolute_importN(   t   sleep(   t	   DummyLock(   t	   bootsteps(   t	   monotonic(   t
   get_logger(   t   bgThreadi   (   t   state(   t   Poolt
   Autoscalert   WorkerComponentt   AUTOSCALE_KEEPALIVEi   c           B@  s8   e  Z d  Z e Z e f Z d   Z d   Z d   Z	 RS(   R	   c         K@  s   | j  |  _ d  | _ d  S(   N(   t	   autoscalet   enabledt   Nonet
   autoscaler(   t   selft   wt   kwargs(    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   __init__,   s    c      	   C@  sZ   |  j  | j | j | j | j d | d | j r9 t   n d  } | _ | j sV | Sd  S(   Nt   workert   mutex(	   t   instantiatet   autoscaler_clst   poolt   max_concurrencyt   min_concurrencyt   use_eventloopR   R   R   (   R   R   t   scaler(    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   create0   s
    (c         C@  s9   | j  j j | j j  | j | j j | j j  d  S(   N(   t   consumert   on_task_messaget   addR   t   maybe_scalet   call_repeatedlyt	   keepalive(   R   R   t   hub(    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   register_with_event_loop8   s    (
   t   __name__t
   __module__t   labelt   Truet   conditionalR   t   requiresR   R   R%   (    (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR
   '   s   			c           B@  sĒ   e  Z d  d e d d  Z d   Z d d  Z d d  Z d d d  Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z e d    Z e d    Z RS(   i    c         C@  sw   t  t |   j   | |  _ | p+ t j   |  _ | |  _ | |  _ | |  _	 d  |  _ | |  _ |  j	 ss t d   d  S(   Ns   cannot scale down too fast.(   t   superR	   R   R   t	   threadingt   LockR   R   R   R#   R   t   _last_actionR   t   AssertionError(   R   R   R   R   R   R#   R   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR   A   s    						c         C@  s(   |  j   |  j   Wd  QXt d  d  S(   Ng      đ?(   R   R!   R   (   R   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   bodyO   s    
c         C@  sk   |  j  } t |  j |  j  } | | k r? |  j | |  t S| | k  rg |  j | | |  j  t Sd  S(   N(   t	   processest   mint   qtyR   t   scale_upR)   t
   scale_downR   (   R   t   reqt   procst   cur(    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   _maybe_scaleT   s    	c         C@  s#   |  j  |  r |  j j   n  d  S(   N(   R:   R   t   maintain_pool(   R   R7   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR!   ^   s    c         C@  s    |  j   | d  k	 rH | |  j k  r< |  j |  j |  n  | |  _ n  | d  k	 r | |  j k rz |  j | |  j  n  | |  _ n  |  j |  j f SWd  QXd  S(   N(   R   R   R   t   _shrinkR2   R   t   _grow(   R   t   maxR3   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   updateb   s    
c         C@  sX   |  j  I |  j | } | |  j k r2 | |  _ n  |  j d 7_ |  j |  Wd  QXd  S(   Ni   (   R   R2   R   R   R=   (   R   t   nt   new(    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   force_scale_upn   s    
c         C@  s^   |  j  O |  j | } | |  j k  r; t | d  |  _ n  |  j t | |  j   Wd  QXd  S(   Ni    (   R   R2   R   R>   R<   R3   (   R   R@   RA   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   force_scale_downv   s
    
c         C@  s   t    |  _ |  j |  S(   N(   R   R/   R=   (   R   R@   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR5   }   s    c         C@  sE   | rA |  j  rA t   |  j  |  j k rA t   |  _  |  j |  Sd  S(   N(   R/   R   R#   R<   (   R   R@   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR6      s    c         C@  s4   t  d |  |  j j |  |  j j j |  d  S(   Ns   Scaling up %s processes.(   t   infoR   t   growR   R   t   _update_prefetch_count(   R   R@   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR=      s    c         C@  s|   t  d |  y |  j j |  Wn@ t k
 r> t d  n& t k
 rc } t d | d t n X|  j j	 j
 |  d  S(   Ns   Scaling down %s processes.s0   Autoscaler won't scale down: all processes busy.s   Autoscaler: scale_down: %rt   exc_info(   RD   R   t   shrinkt
   ValueErrort   debugt	   Exceptiont   errorR)   R   R   RF   (   R   R@   t   exc(    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR<      s    c         C@  s,   i |  j  d 6|  j d 6|  j d 6|  j d 6S(   NR>   R3   t   currentR4   (   R   R   R2   R4   (   R   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyRD      s    

c         C@  s   t  t j  S(   N(   t   lenR   t   reserved_requests(   R   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR4      s    c         C@  s
   |  j  j S(   N(   R   t   num_processes(   R   (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR2       s    N(   R&   R'   R   R   R   R1   R:   R!   R?   RB   RC   R5   R6   R=   R<   RD   t   propertyR4   R2   (    (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyR	   ?   s   	
						
	("   t   __doc__t
   __future__R    t   osR-   t   timeR   t   kombu.async.semaphoreR   t   celeryR   t   celery.fiveR   t   celery.utils.logR   t   celery.utils.threadsR   t    R   t
   componentsR   t   __all__R&   t   loggerRJ   RD   RL   t   floatt   environt   getR   t   StartStopStepR
   R	   (    (    (    s9   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/autoscale.pyt   <module>   s"   