
    <#(iK=                     z   d dl Z d dlmZ ddlmZ  e j        e          Z G d de          Z G d de          Z	 G d	 d
e          Z
 G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          ZdS )    N)web   )BaseApiHandlerc                       e Zd Zd Zd ZdS )ControlHandlerc                 "    |o|| j         j        v S )N)applicationworkersself
workernames     R/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/flower/api/control.py	is_workerzControlHandler.is_worker   s    DjD,<,DDD    c                     |D ]0}	 ||                              dd          c S # t          $ r Y -w xY wt                              d|           dS )z$extracts error message from responseerrorzUnknown reasonz(Failed to extract error reason from '%s')getKeyErrorloggerr   )r   r   responseress       r   error_reasonzControlHandler.error_reason   sr     	 	C:**74DEEEEE   ?JJJs   $
11N)__name__
__module____qualname__r   r    r   r   r   r   
   s5        E E E         r   r   c                   .    e Zd Zej        d             ZdS )WorkerShutDownc                 "   |                      |          st          j        dd| d          t                              d|           | j        j                            d|g           |                     t          d                     d	S )
a  
Shut down a worker

**Example request**:

.. sourcecode:: http

  POST /api/worker/shutdown/celery@worker2 HTTP/1.1
  Content-Length: 0
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 29
  Content-Type: application/json; charset=UTF-8

  {
      "message": "Shutting down!"
  }

:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown worker
          Unknown worker ''zShutting down '%s' workershutdown)destinationzShutting down!messageN)
r   r   	HTTPErrorr   infocappcontrol	broadcastwritedictr   s     r   postzWorkerShutDown.post   s    < ~~j)) 	G-%E
%E%E%EFFF/<<<	##JZL#III

4 011122222r   Nr   r   r   r   authenticatedr.   r   r   r   r   r      s1        "3 "3 "3 "3 "3r   r   c                   .    e Zd Zej        d             ZdS )WorkerPoolRestartc                    |                      |          st          j        dd| d          t                              d|           | j        j                            dddi|gd	          }|r9d
|d         |         v r)|                     t          d| d                     dS t          
                    |           |                     d           |                     ||          }|                     d| d|            dS )ac  
Restart worker's pool

**Example request**:

.. sourcecode:: http

  POST /api/worker/pool/restart/celery@worker2 HTTP/1.1
  Content-Length: 0
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 56
  Content-Type: application/json; charset=UTF-8

  {
      "message": "Restarting 'celery@worker2' worker's pool"
  }

:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 403: pool restart is not enabled (see CELERYD_POOL_RESTARTS)
:statuscode 404: unknown worker
        r    r!   r"   zRestarting '%s' worker's poolpool_restartreloadFT	argumentsr$   replyokr   zRestarting 'z' worker's poolr%     zFailed to restart the 'z' pool: N)r   r   r'   r   r(   r)   r*   r+   r,   r-   r   
set_statusr   )r   r   r   reasons       r   r.   zWorkerPoolRestart.postA   s+   > ~~j)) 	G-%E
%E%E%EFFF3Z@@@9$..x&7#D / 2 2  	OJ 777JJt$N:$N$N$NOOOPPPPPLL"""OOC   &&z8<<FJJMMMVMMNNNNNr   Nr/   r   r   r   r2   r2   @   s6        +O +O +O +O +Or   r2   c                   .    e Zd Zej        d             ZdS )WorkerPoolGrowc                 T   |                      |          st          j        dd| d          |                     ddt                    }t
                              d||           | j        j        	                    |d|g	          }|r;d
|d         |         v r+| 
                    t          d| d|                      dS t
                              |           |                     d           |                     ||          }| 
                    d| d|            dS )as  
Grow worker's pool

**Example request**:

.. sourcecode:: http

  POST /api/worker/pool/grow/celery@worker2?n=3 HTTP/1.1
  Content-Length: 0
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 58
  Content-Type: application/json; charset=UTF-8

  {
      "message": "Growing 'celery@worker2' worker's pool by 3"
  }

:query n: number of pool processes to grow, default is 1
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 403: failed to grow
:statuscode 404: unknown worker
        r    r!   r"   nr   defaulttypez"Growing '%s' worker's pool by '%s'Tr@   r8   r$   r9   r   z	Growing '' worker's pool by r%   r:   zFailed to grow '' worker's pool: N)r   r   r'   get_argumentintr   r(   r)   r*   	pool_growr,   r-   r   r;   r   r   r   r@   r   r<   s        r   r.   zWorkerPoolGrow.postq   sC   B ~~j)) 	G-%E
%E%E%EFFFc13778*aHHH9$..t* / 7 7 	QJ 777JJt$R
$R$Rq$R$RSSSTTTTTLL"""OOC   &&z8<<FJJO*OOvOOPPPPPr   Nr/   r   r   r   r>   r>   p   s6        .Q .Q .Q .Q .Qr   r>   c                   .    e Zd Zej        d             ZdS )WorkerPoolShrinkc                 T   |                      |          st          j        dd| d          |                     ddt                    }t
                              d||           | j        j        	                    |d|g	          }|r;d
|d         |         v r+| 
                    t          d| d|                      dS t
                              |           |                     d           |                     ||          }| 
                    d| d|            dS )ay  
Shrink worker's pool

**Example request**:

.. sourcecode:: http

  POST /api/worker/pool/shrink/celery@worker2 HTTP/1.1
  Content-Length: 0
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 60
  Content-Type: application/json; charset=UTF-8

  {
      "message": "Shrinking 'celery@worker2' worker's pool by 1"
  }

:query n: number of pool processes to shrink, default is 1
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 403: failed to shrink
:statuscode 404: unknown worker
        r    r!   r"   r@   r   rA   z$Shrinking '%s' worker's pool by '%s'TrD   r9   r   zShrinking 'rE   r%   r:   zFailed to shrink 'rF   N)r   r   r'   rG   rH   r   r(   r)   r*   pool_shrinkr,   r-   r   r;   r   rJ   s        r   r.   zWorkerPoolShrink.post   sD   B ~~j)) 	G-%E
%E%E%EFFFc1377:JJJJ9$00t* 1 7 7 	SJ 777JJt$T*$T$TQR$T$TUUUVVVVVLL"""OOC   &&z8<<FJJQJQQQQRRRRRr   Nr/   r   r   r   rL   rL      s6        .S .S .S .S .Sr   rL   c                   .    e Zd Zej        d             ZdS )WorkerPoolAutoscalec                    |                      |          st          j        dd| d          |                     dt                    }|                     dt                    }t
                              d|||f           | j        j        	                    d||d	|gd
          }|r9d|d         |         v r)| 
                    t          d| d                     dS t
                              |           |                     d           |                     ||          }| 
                    d| d|            dS )a  
Autoscale worker pool

**Example request**:

.. sourcecode:: http

  POST /api/worker/pool/autoscale/celery@worker2?min=3&max=10 HTTP/1.1
  Content-Length: 0
  Content-Type: application/x-www-form-urlencoded; charset=utf-8
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 66
  Content-Type: application/json; charset=UTF-8

  {
      "message": "Autoscaling 'celery@worker2' worker (min=3, max=10)"
  }

:query min: minimum number of pool processes
:query max: maximum number of pool processes
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 403: autoscaling is not enabled (see CELERYD_AUTOSCALER)
:statuscode 404: unknown worker
        r    r!   r"   min)rC   maxzAutoscaling '%s' worker by '%s'	autoscale)rR   rS   Tr6   r9   r   zAutoscaling 'z' worker (min={min}, max={max})r%   r:   zFailed to autoscale '
' worker: N)r   r   r'   rG   rH   r   r(   r)   r*   r+   r,   r-   r   r;   r   )r   r   rR   rS   r   r<   s         r   r.   zWorkerPoolAutoscale.post   s   F ~~j)) 	G-%E
%E%E%EFFFC00C005c
	, 	, 	,9$..3s#;#;#D / 2 2  	OJ 777JJt %=J %= %= %= > > > ? ? ? ? ? LL"""OOC   &&z8<<FJJMzMMVMMNNNNNr   Nr/   r   r   r   rP   rP      s6        4O 4O 4O 4O 4Or   rP   c                   .    e Zd Zej        d             ZdS )WorkerQueueAddConsumerc                 h   |                      |          st          j        dd| d          |                     d          }t                              d||           | j        j                            dd|i|gd          }|rGd	|d
         |         v r7| 	                    t          |d
         |         d	                              dS t                              |           |                     d           |                     ||          }| 	                    d| d| d|            dS )a  
Start consuming from a queue

**Example request**:

.. sourcecode:: http

  POST /api/worker/queue/add-consumer/celery@worker2?queue=sample-queue
  Content-Length: 0
  Content-Type: application/x-www-form-urlencoded; charset=utf-8
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 40
  Content-Type: application/json; charset=UTF-8

  {
      "message": "add consumer sample-queue"
  }

:query queue: the name of a new queue
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 403: failed to add consumer
:statuscode 404: unknown worker
        r    r!   r"   queuez#Adding consumer '%s' to worker '%s'add_consumerTr6   r9   r   r%   r:   zFailed to add 'z' consumer to 'rU   Nr   r   r'   rG   r   r(   r)   r*   r+   r,   r-   r   r;   r   r   r   rY   r   r<   s        r   r.   zWorkerQueueAddConsumer.post  sS   B ~~j)) 	G-%E
%E%E%EFFF!!'**9:	' 	' 	'9$..w&6#D / 2 2  	_J 777JJtHQK
$;D$ABBBCCCCCLL"""OOC   &&z8<<FJJ]]]z]]U[]]^^^^^r   Nr/   r   r   r   rW   rW     s6        0_ 0_ 0_ 0_ 0_r   rW   c                   .    e Zd Zej        d             ZdS )WorkerQueueCancelConsumerc                 h   |                      |          st          j        dd| d          |                     d          }t                              d||           | j        j                            dd|i|gd          }|rGd	|d
         |         v r7| 	                    t          |d
         |         d	                              dS t                              |           |                     d           |                     ||          }| 	                    d| d| d|            dS )a  
Stop consuming from a queue

**Example request**:

.. sourcecode:: http

  POST /api/worker/queue/cancel-consumer/celery@worker2?queue=sample-queue
  Content-Length: 0
  Content-Type: application/x-www-form-urlencoded; charset=utf-8
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 52
  Content-Type: application/json; charset=UTF-8

  {
      "message": "no longer consuming from sample-queue"
  }

:query queue: the name of queue
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 403: failed to cancel consumer
:statuscode 404: unknown worker
        r    r!   r"   rY   z(Canceling consumer '%s' from worker '%s'cancel_consumerTr6   r9   r   r%   r:   zFailed to cancel 'z' consumer from 'rU   Nr[   r\   s        r   r.   zWorkerQueueCancelConsumer.postE  sS   B ~~j)) 	G-%E
%E%E%EFFF!!'**>:	' 	' 	'9$..'5)9#D / 2 2  	dJ 777JJtHQK
$;D$ABBBCCCCCLL"""OOC   &&z8<<FJJbEbbJbbZ`bbcccccr   Nr/   r   r   r   r^   r^   D  s6        0d 0d 0d 0d 0dr   r^   c                   .    e Zd Zej        d             ZdS )
TaskRevokec                 B   t                               d|           |                     ddt                    }|                     ddt                    }| j        j                            |||           |                     t          d| d	
                     dS )a  
Revoke a task

**Example request**:

.. sourcecode:: http

  POST /api/task/revoke/1480b55c-b8b2-462c-985e-24af3e9158f9?terminate=true
  Content-Length: 0
  Content-Type: application/x-www-form-urlencoded; charset=utf-8
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 61
  Content-Type: application/json; charset=UTF-8

  {
      "message": "Revoked '1480b55c-b8b2-462c-985e-24af3e9158f9'"
  }

:query terminate: terminate the task if it is running
:query signal: name of signal to send to process if terminate (default: 'SIGTERM')
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
        zRevoking task '%s'	terminateFrA   signalSIGTERM)rd   re   z	Revoked 'r"   r%   N)
r   r(   rG   boolstrr)   r*   revoker,   r-   )r   taskidrd   re   s       r   r.   zTaskRevoke.postz  s    @ 	(&111%%k5t%LL	""8YS"II	  9V LLL

4 5F 5 5 566677777r   Nr/   r   r   r   rb   rb   y  s1        #8 #8 #8 #8 #8r   rb   c                   .    e Zd Zej        d             ZdS )
TaskTimoutc                 0   |                      d          }|                      ddt                    }|                      ddt                    }|| j        j        vrt	          j        dd| d          |.|                     |          st	          j        dd	| d          t                              d
|||           ||gnd}| j        j	        
                    |d|||          }|rGd|d         |         v r7|                     t          |d         |         d                              dS t                              |           |                     d           |                     ||          }|                     d| d           dS )a  
Change soft and hard time limits for a task

**Example request**:

.. sourcecode:: http

    POST /api/task/timeout/tasks.sleep HTTP/1.1
    Content-Length: 44
    Content-Type: application/x-www-form-urlencoded; charset=utf-8
    Host: localhost:5555

    soft=30&hard=100&workername=celery%40worker1

**Example response**:

.. sourcecode:: http

    HTTP/1.1 200 OK
    Content-Length: 46
    Content-Type: application/json; charset=UTF-8

    {
        "message": "time limits set successfully"
    }

:query workername: worker name
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown task/worker
        r   hardNrA   softr    Unknown task 'r"   r!   z'Setting timeouts for '%s' task (%s, %s)T)r8   rn   ro   r$   r9   r   r%   r:   zFailed to set timeouts: ')rG   floatr)   tasksr   r'   r   r   r(   r*   
time_limitr,   r-   r   r;   r   )r   tasknamer   rn   ro   r$   r   r<   s           r   r.   zTaskTimout.post  s   D &&|44
  E BB  E BB49?**-%Ah%A%A%ABBB!$..*D*D!-%E
%E%E%EFFF=dD	* 	* 	*&0&<zll$9$//Dt$# 0 % %  	>J 777JJtHQK
$;D$ABBBCCCCCLL"""OOC   &&x::FJJ<6<<<=====r   Nr/   r   r   r   rl   rl     s1        7> 7> 7> 7> 7>r   rl   c                   .    e Zd Zej        d             ZdS )TaskRateLimitc                    |                      d          }|                      d          }|| j        j        vrt          j        dd| d          |.|                     |          st          j        dd| d          t                              d||           ||gnd}| j        j        	                    ||d	|
          }|rGd|d         |         v r7| 
                    t          |d         |         d                              dS t                              |           |                     d           |                     ||          }| 
                    d| d           dS )a  
Change rate limit for a task

**Example request**:

.. sourcecode:: http

    POST /api/task/rate-limit/tasks.sleep HTTP/1.1
    Content-Length: 41
    Content-Type: application/x-www-form-urlencoded; charset=utf-8
    Host: localhost:5555

    ratelimit=200&workername=celery%40worker1

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 61
  Content-Type: application/json; charset=UTF-8

  {
      "message": "new rate limit set successfully"
  }

:query workername: worker name
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown task/worker
        r   	ratelimitr    rp   r"   Nr!   z%Setting '%s' rate limit for '%s' taskT)r8   r$   r9   r   r%   r:   zFailed to set rate limit: ')rG   r)   rr   r   r'   r   r   r(   r*   
rate_limitr,   r-   r   r;   r   )r   rt   r   rx   r$   r   r<   s          r   r.   zTaskRateLimit.post  s   D &&|44
%%k22	49?**-%Ah%A%A%ABBB!$..*D*D!-%E
%E%E%EFFF;x	) 	) 	)&0&<zll$9$//it 0 F F 	@J 777JJtHQK
$;D$ABBBCCCCCLL"""OOC   &&x::FJJ>V>>>?????r   Nr/   r   r   r   rv   rv     s6        4@ 4@ 4@ 4@ 4@r   rv   )loggingtornador    r   	getLoggerr   r   r   r   r2   r>   rL   rP   rW   r^   rb   rl   rv   r   r   r   <module>r~      s_               		8	$	$         ^      $3 $3 $3 $3 $3^ $3 $3 $3N-O -O -O -O -O -O -O -O`0Q 0Q 0Q 0Q 0Q^ 0Q 0Q 0Qf0S 0S 0S 0S 0S~ 0S 0S 0Sf6O 6O 6O 6O 6O. 6O 6O 6Or2_ 2_ 2_ 2_ 2_^ 2_ 2_ 2_j2d 2d 2d 2d 2d 2d 2d 2dj%8 %8 %8 %8 %8 %8 %8 %8P9> 9> 9> 9> 9> 9> 9> 9>x6@ 6@ 6@ 6@ 6@N 6@ 6@ 6@ 6@ 6@r   