
    <#(iC                        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
lmZ d dlmZ ddlm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'dS )%    N)OrderedDict)datetime)states)DisabledBackend)AbortableAsyncResult)AsyncResult)web)json_decode)IOLoop)	HTTPError   )tasks)Broker   )BaseApiHandlerc                   F    e Zd ZdZd Zed             Zd Zd Zd Z	d Z
dS )	BaseTaskHandlerz%Y-%m-%d %H:%M:%S.%fc                    	 | j         j        }|rt          |          ni }n0# t          $ r#}t	          dt          |                    |d }~ww xY wt          |t                    st	          dd          |                    dg           }|                    di           }t          |t          t          f          st	          dd          |||fS )N  zinvalid optionsargskwargszargs must be an array)requestbodyr
   
ValueErrorr   str
isinstancedictpoplisttuple)selfr   optionser   r   s         P/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/flower/api/tasks.pyget_task_argszBaseTaskHandler.get_task_args   s    	0<$D+/7k$'''RGG 	0 	0 	0CQ((a/	0 '4(( 	4C!2333{{62&&Xr**$u.. 	:C!8999VW$$s   " 
AA

Ac                 8    t          | j        t                     S N)r   backendr   )results    r$   backend_configuredz"BaseTaskHandler.backend_configured+   s    fno>>>>    c                 0    |                      |           d S r'   )
set_status)r!   status_coder   s      r$   write_errorzBaseTaskHandler.write_error/   s    $$$$$r+   c                     |j         t          j        k    r7|                    |                     |j                  |j        d           d S |                    d|                     |j                  i           d S )N)r)   	tracebackr)   )stater   FAILUREupdatesafe_resultr)   r1   )r!   responser)   s      r$   update_response_resultz&BaseTaskHandler.update_response_result2   s    <6>))OOt'7'7'F'F*0*:< < = = = = = OOXt'7'7'F'FGHHHHHr+   c                 (   d|v r#t          j        |d         | j                  |d<   d|v rt          |d                   |d<   d|v rJ|d         }	 t          |          }n*# t          $ r t          j        || j                  }Y nw xY w||d<   d S d S )Neta	countdownexpires)r   strptimeDATE_FORMATfloatr   )r!   r"   r;   s      r$   normalize_optionsz!BaseTaskHandler.normalize_options9   s    G%.wu~/3/?A AGEN'!!#()=#>#>GK i(GG.. G G G"+GT5EFFG!(GI  s   A! !$BBc                 p    	 t          j        |           n# t          $ r t          |          cY S w xY w|S )zreturns json encodable result)jsondumps	TypeErrorrepr)r!   r)   s     r$   r5   zBaseTaskHandler.safe_resultG   sL    	 Jv 	  	  	 <<	 s    33N)__name__
__module____qualname__r=   r%   staticmethodr*   r/   r7   r?   r5    r+   r$   r   r      s        (K% % %$ ? ? \?% % %I I I) ) )    r+   r   c                   4    e Zd Zej        d             Zd ZdS )	TaskApplyc                 "  K   |                                  \  }}}t                              d|||           	 | j        j        |         }n'# t
          $ r}t          dd| d          |d}~ww xY w	 |                     |           n## t          $ r}t          dd          |d}~ww xY w |j	        d
||d|}d	|j
        i}t          j                                        d| j        ||           d{V }|                     |           dS )a#  
Execute a task by name and wait results

**Example request**:

.. sourcecode:: http

  POST /api/task/apply/tasks.add HTTP/1.1
  Accept: application/json
  Accept-Encoding: gzip, deflate, compress
  Content-Length: 16
  Content-Type: application/json; charset=utf-8
  Host: localhost:5555

  {
      "args": [1, 2]
  }

**Example response**:

.. sourcecode:: http

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

  {
      "state": "SUCCESS",
      "task-id": "c60be250-fe52-48df-befb-ac66174076e6",
      "result": 3
  }

:query args: a list of arguments
:query kwargs: a dictionary of arguments
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown task
        'Invoking a task '%s' with '%s' and '%s'  Unknown task ''Nr   Invalid optionr   r   task-idrI   )r%   loggerdebugcappr   KeyErrorr   r?   r   apply_asynctask_idr   currentrun_in_executorwait_resultswrite	r!   tasknamer   r   r"   taskexcr)   r6   s	            r$   postzTaskApply.postQ   sr     R !% 2 2 4 4fg>tV	- 	- 	-	H9?8,DD 	H 	H 	HC!=(!=!=!=>>CG	H	<""7++++ 	< 	< 	<C!122;	< "!FtFFFgFFv~.))99$#VX7 7 7 7 7 7 7 7

8s/   A 
A0A++A04B
 

B*B%%B*c                     |                     d           |                     ||           |                     |          r|                    |j                   |S )NF)	propagater2   )getr7   r*   r4   r2   )r!   r)   r6   s      r$   r\   zTaskApply.wait_results   s]    

U
#####Hf555""6** 	0OO&,O///r+   N)rE   rF   rG   r	   authenticatedrb   r\   rI   r+   r$   rK   rK   P   sA        ; ; ;z    r+   rK   c                   .    e Zd Zej        d             ZdS )TaskAsyncApplyc                    |                                  \  }}}t                              d|||           	 | j        j        |         }n'# t
          $ r}t          dd| d          |d}~ww xY w	 |                     |           n## t          $ r}t          dd          |d}~ww xY w |j	        d||d|}d	|j
        i}|                     |          r|                    |j        
           |                     |           dS )ac  
Execute a task

**Example request**:

.. sourcecode:: http

  POST /api/task/async-apply/tasks.add HTTP/1.1
  Accept: application/json
  Accept-Encoding: gzip, deflate, compress
  Content-Length: 16
  Content-Type: application/json; charset=utf-8
  Host: localhost:5555

  {
      "args": [1, 2]
  }

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 71
  Content-Type: application/json; charset=UTF-8
  Date: Sun, 13 Apr 2014 15:55:00 GMT

  {
      "state": "PENDING",
      "task-id": "abc300c7-2922-4069-97b6-a635cc2ac47c"
  }

:query args: a list of arguments
:query kwargs: a dictionary of arguments
:query options: a dictionary of `apply_async` keyword arguments
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown task
        rM   rN   rO   rP   Nr   rQ   rR   rS   re   rI   )r%   rT   rU   rV   r   rW   r   r?   r   rX   rY   r*   r4   r2   r]   r^   s	            r$   rb   zTaskAsyncApply.post   sS   T !% 2 2 4 4fg>tV	- 	- 	-	H9?8,DD 	H 	H 	HC!=(!=!=!=>>CG	H	<""7++++ 	< 	< 	<C!122;	< "!FtFFFgFFv~.""6** 	0OO&,O///

8s/   A
 

A.A))A.2B 
B(B##B(NrE   rF   rG   r	   rg   rb   rI   r+   r$   ri   ri      s1        ; ; ; ; ;r+   ri   c                   .    e Zd Zej        d             ZdS )TaskSendc                 8   |                                  \  }}}t                              d|||            | j        j        |f||d|}d|j        i}|                     |          r|                    |j                   | 	                    |           dS )a"  
Execute a task by name (doesn't require task sources)

**Example request**:

.. sourcecode:: http

  POST /api/task/send-task/tasks.add HTTP/1.1
  Accept: application/json
  Accept-Encoding: gzip, deflate, compress
  Content-Length: 16
  Content-Type: application/json; charset=utf-8
  Host: localhost:5555

  {
      "args": [1, 2]
  }

**Example response**:

.. sourcecode:: http

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

  {
      "state": "SUCCESS",
      "task-id": "c60be250-fe52-48df-befb-ac66174076e6"
  }

:query args: a list of arguments
:query kwargs: a dictionary of arguments
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown task
        z%Invoking task '%s' with '%s' and '%s'rR   rS   re   N)
r%   rT   rU   rV   	send_taskrY   r*   r4   r2   r]   )r!   r_   r   r   r"   r)   r6   s          r$   rb   zTaskSend.post   s    P !% 2 2 4 4fg<tV	- 	- 	-$$;; ;29; ;v~.""6** 	0OO&,O///

8r+   Nrk   rI   r+   r$   rm   rm      s1        / / / / /r+   rm   c                   .    e Zd Zej        d             ZdS )
TaskResultc                    |                      dd          }|t          |          nd}t          |          }|                     |          st	          d          ||j        d}|r.|                    |d           |                     ||           n*|                                r|                     ||           | 	                    |           dS )a  
Get a task result

**Example request**:

.. sourcecode:: http

  GET /api/task/result/c60be250-fe52-48df-befb-ac66174076e6 HTTP/1.1
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

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

  {
      "result": 3,
      "state": "SUCCESS",
      "task-id": "c60be250-fe52-48df-befb-ac66174076e6"
  }

:query timeout: how long to wait, in seconds, before the operation times out
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 503: result backend is not configured
        timeoutN  )rS   r2   F)rs   rd   )
get_argumentr>   r   r*   r   r2   rf   r7   readyr]   )r!   taskidrs   r)   r6   s        r$   rf   zTaskResult.get  s    @ ##It44$+$7%...TV$$&&v.. 	!C.. %== 	:JJw%J888''&9999\\^^ 	:''&999

8r+   NrE   rF   rG   r	   rg   rf   rI   r+   r$   rq   rq     s1        , , , , ,r+   rq   c                   .    e Zd Zej        d             ZdS )	TaskAbortc                    t                               d|           t          |          }|                     |          st	          d          |                                 |                     t          d| d                     dS )a)  
Abort a running task

**Example request**:

.. sourcecode:: http

  POST /api/task/abort/c60be250-fe52-48df-befb-ac66174076e6 HTTP/1.1
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

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

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

:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 503: result backend is not configured
        zAborting task '%s'rt   z	Aborted 'rP   )messageN)rT   infor   r*   r   abortr]   r   )r!   rw   r)   s      r$   rb   zTaskAbort.post@  s    : 	(&111%f--&&v.. 	!C.. 

4 5F 5 5 566677777r+   Nrk   rI   r+   r$   rz   rz   ?  s1        $8 $8 $8 $8 $8r+   rz   c                   .    e Zd Zej        d             ZdS )GetQueueLengthsc                   K   | j         }d}|j        dk    r|j        j        r|j        j        }t	          |j                                                            d          || j        j        j	        | j        j        j
                  }|                    |                                            d{V }|                     d|i           dS )aH  
Return length of all active queues

**Example request**:

.. sourcecode:: http

  GET /api/queues/length
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

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

  {
      "active_queues": [
          {"name": "celery", "messages": 0},
          {"name": "video-queue", "messages": 5}
      ]
  }

:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 503: result backend is not configured
        NamqpT)include_password)http_apibroker_optionsbroker_use_sslactive_queues)application	transportr"   
broker_apir   rV   
connectionas_uriconfbroker_transport_optionsr   queuesget_active_queue_namesr]   )r!   appr   brokerr   s        r$   rf   zGetQueueLengths.geti  s      @ =F""s{'="{-H++--44d4KK!)$).:a'+y~'DF F F }}T%@%@%B%BCCCCCCCC

OV,-----r+   Nrx   rI   r+   r$   r   r   h  s1        *. *. *. *. *.r+   r   c                   .    e Zd Zej        d             ZdS )	ListTasksc                 `   | j         }|                     dd          }|                     ddt                    }|                     dd          }|                     dd          }|                     dd          }|                     d	d          }|                     d
d          }|                     dd          }	|                     dd          }
|ot          |          }t          |d          }|dk    r|nd}|dk    r|nd}|dk    r|nd}g }t	          j        |j        |||	||||||

  
        D ]R\  }}t	          j        |          }|                    dd          }|
|j	        |d<   |
                    ||f           S|                     t          |                     dS )aU
  
List tasks

**Example request**:

.. sourcecode:: http

  GET /api/tasks HTTP/1.1
  Host: localhost:5555
  User-Agent: HTTPie/0.8.0

**Example response**:

.. sourcecode:: http

  HTTP/1.1 200 OK
  Content-Length: 1109
  Content-Type: application/json; charset=UTF-8
  Etag: "b2478118015c8b825f7b88ce6b660e5449746c37"
  Server: TornadoServer/3.1.1

  {
      "e42ceb2d-8730-47b5-8b4d-8e0d2a1ef7c9": {
          "args": "[3, 4]",
          "client": null,
          "clock": 1079,
          "eta": null,
          "exception": null,
          "exchange": null,
          "expires": null,
          "failed": null,
          "kwargs": "{}",
          "name": "tasks.add",
          "received": 1398505411.107885,
          "result": "'7'",
          "retried": null,
          "retries": 0,
          "revoked": null,
          "routing_key": null,
          "runtime": 0.01610181899741292,
          "sent": null,
          "started": 1398505411.108985,
          "state": "SUCCESS",
          "succeeded": 1398505411.124802,
          "timestamp": 1398505411.124802,
          "traceback": null,
          "uuid": "e42ceb2d-8730-47b5-8b4d-8e0d2a1ef7c9",
          "worker": "celery@worker1"
      },
      "f67ea225-ae9e-42a8-90b0-5de0b24507e0": {
          "args": "[1, 2]",
          "client": null,
          "clock": 1042,
          "eta": null,
          "exception": null,
          "exchange": null,
          "expires": null,
          "failed": null,
          "kwargs": "{}",
          "name": "tasks.add",
          "received": 1398505395.327208,
          "result": "'3'",
          "retried": null,
          "retries": 0,
          "revoked": null,
          "routing_key": null,
          "runtime": 0.012884548006695695,
          "sent": null,
          "started": 1398505395.3289,
          "state": "SUCCESS",
          "succeeded": 1398505395.341089,
          "timestamp": 1398505395.341089,
          "traceback": null,
          "uuid": "f67ea225-ae9e-42a8-90b0-5de0b24507e0",
          "worker": "celery@worker1"
      }
  }

:query limit: maximum number of tasks
:query offset: skip first n tasks
:query sort_by: sort tasks by attribute (name, state, received, started)
:query workername: filter task by workername
:query taskname: filter tasks by taskname
:query state: filter tasks by state
:query received_start: filter tasks by received date (must be greater than) format %Y-%m-%d %H:%M
:query received_end: filter tasks by received date (must be less than) format %Y-%m-%d %H:%M
:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
        limitNoffsetr   )defaulttype
workernamer_   r2   received_startreceived_endsort_bysearchAll)	r   r   r   r   workerr2   r   r   r   r   )r   ru   intmaxr   
iter_taskseventsas_dictr   hostnameappendr]   r   )r!   r   r   r   r   r   r2   r   r   r   r   r)   rY   r`   s                 r$   rf   zListTasks.get  s   x !!'400""8QS"AA""<66  T22!!'400**+;TBB((>>##It44""8T22$#e**VQ!U??u}}tt$%T"-
%dU-)
 
 
 	+ 	+MGT =&&DXXh--F!!'XMM7D/****

;v&&'''''r+   Nrx   rI   r+   r$   r   r     s6        y( y( y( y( y(r+   r   c                   .    e Zd Zej        d             ZdS )ListTaskTypesc                     | j         j        j                                        }i }||d<   |                     |           dS )a  
List (seen) task types

**Example request**:

.. sourcecode:: http

  GET /api/task/types HTTP/1.1
  Host: localhost:5555

**Example response**:

.. sourcecode:: http

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

  {
      "task-types": [
          "tasks.add",
          "tasks.sleep"
      ]
  }

:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
        z
task-typesN)r   r   r2   
task_typesr]   )r!   seen_task_typesr6   s      r$   rf   zListTaskTypes.get  sE    > *17BBDD!0

8r+   Nrx   rI   r+   r$   r   r     s1        " " " " "r+   r   c                   .    e Zd Zej        d             ZdS )TaskInfoc                     t          j        | j        j        |          }|st	          dd| d          |                                }|j        |j        j        |d<   |                     |           dS )a  
Get a task info

**Example request**:

.. sourcecode:: http

  GET /api/task/info/91396550-c228-4111-9da4-9d88cfd5ddc6 HTTP/1.1
  Accept: */*
  Accept-Encoding: gzip, deflate, compress
  Host: localhost:5555


**Example response**:

.. sourcecode:: http

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

  {
      "args": "[2, 2]",
      "client": null,
      "clock": 25,
      "eta": null,
      "exception": null,
      "exchange": null,
      "expires": null,
      "failed": null,
      "kwargs": "{}",
      "name": "tasks.add",
      "received": 1400806241.970742,
      "result": "'4'",
      "retried": null,
      "retries": null,
      "revoked": null,
      "routing_key": null,
      "runtime": 2.0037889280356467,
      "sent": null,
      "started": 1400806241.972624,
      "state": "SUCCESS",
      "succeeded": 1400806243.975336,
      "task-id": "91396550-c228-4111-9da4-9d88cfd5ddc6",
      "timestamp": 1400806243.975336,
      "traceback": null,
      "worker": "celery@worker1"
  }

:reqheader Authorization: optional OAuth token to authenticate
:statuscode 200: no error
:statuscode 401: unauthorized request
:statuscode 404: unknown task
        rN   rO   rP   Nr   )	r   get_task_by_idr   r   r   r   r   r   r]   )r!   rw   r`   r6   s       r$   rf   zTaskInfo.get=  s}    r #D$4$;VDD 	=C!;&!;!;!;<<<<<>>;"!%!5HX

8r+   Nrx   rI   r+   r$   r   r   <  s6        @ @ @ @ @r+   r   )(rA   loggingcollectionsr   r   celeryr   celery.backends.baser   celery.contrib.abortabler   celery.resultr   tornador	   tornado.escaper
   tornado.ioloopr   tornado.webr   utilsr   utils.brokerr    r   	getLoggerrE   rT   r   rK   ri   rm   rq   rz   r   r   r   r   rI   r+   r$   <module>r      s     # # # # # #             0 0 0 0 0 0 9 9 9 9 9 9 % % % % % %       & & & & & & ! ! ! ! ! ! ! ! ! ! ! !       ! ! ! ! ! !      		8	$	$7 7 7 7 7n 7 7 7tF F F F F F F FR> > > > >_ > > >B1 1 1 1 1 1 1 1h. . . . . . . .b&8 &8 &8 &8 &8 &8 &8 &8R,. ,. ,. ,. ,.o ,. ,. ,.^{( {( {( {( {( {( {( {(|$ $ $ $ $O $ $ $NB B B B B B B B B Br+   