
    $g                         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  G d	 d
e      Zd Z G d de      Zy)    N)copy_context)PlotlyJSONEncoder)context_value)AttributeDict)PreventUpdate)ProxySetProps)BaseLongCallbackManagerc                   h     e Zd ZdZd fd	Zd Zd Zd ZddZd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )CeleryManagerz=Manage background execution of callbacks with a celery queue.c                    	 ddl }ddlm} t	        ||j
                        st        d      t	        |j                  |      rt        d      || _        || _	        t        | -  |       y# t        $ r}t        d      |d}~ww xY w)a  
        Long callback manager that runs callback logic on a celery task queue,
        and stores results using a celery result backend.

        :param celery_app:
            A celery.Celery application instance that must be configured with a
            result backend. See the celery documentation for information on
            configuration options.
        :param cache_by:
            A list of zero-argument functions.  When provided, caching is enabled and
            the return values of these functions are combined with the callback
            function's input arguments and source code to generate cache keys.
        :param expire:
            If provided, a cache entry will be removed when it has not been accessed
            for ``expire`` seconds.  If not provided, the lifetime of cache entries
            is determined by the default behavior of the celery result backend.
        r   N)DisabledBackendzuCeleryLongCallbackManager requires extra dependencies which can be installed doing

    $ pip install "dash[celery]"
z-First argument must be a celery.Celery objectz8Celery instance must be configured with a result backend)celerycelery.backends.baser   ImportError
isinstanceCelery
ValueErrorbackendhandleexpiresuper__init__)self
celery_appcache_byr   r   r   missing_imports	__class__s          g/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/dash/long_callback/managers/celery_manager.pyr   zCeleryManager.__init__   s    $	# *fmm4LMMj((/:WXX "!  	#&
 ##	#s   
A- -	B6BBc                 T    |y | j                   j                  j                  |       y N)r   control	terminater   jobs     r   terminate_jobzCeleryManager.terminate_job:   s"    ;%%c*    c                 h    | j                  |      }|r|j                  dv r| j                  |      S y)N)FAILUREREVOKEDF)get_taskstatusr%   )r   r$   tasks      r   terminate_unhealthy_jobz%CeleryManager.terminate_unhealthy_job@   s3    }}S!DKK#99%%c**r&   c                 H    | j                  |      }|xr |j                  dv S )N)PENDINGRECEIVEDSTARTEDRETRYPROGRESS)r*   r+   )r   r$   futures      r   job_runningzCeleryManager.job_runningF   s-    s# 
&-- ,
 
 	
r&   c                 2    t        || j                  ||      S r    )_make_job_fnr   )r   fnprogresskeys       r   make_job_fnzCeleryManager.make_job_fnP   s    BXs;;r&   c                 >    |r| j                   j                  |      S y r    )r   AsyncResultr#   s     r   r*   zCeleryManager.get_taskS   s    ;;**3//r&   c                 N    | j                   j                  j                  |       y r    )r   r   deleter   r:   s     r   clear_cache_entryzCeleryManager.clear_cache_entryY   s    ""3'r&   c                 `    |j                  || j                  |      ||      }|j                  S r    )delay_make_progress_keytask_id)r   r:   job_fnargscontextr,   s         r   call_job_fnzCeleryManager.call_job_fn\   s+    ||C!8!8!=tWM||r&   c                     | j                  |      }| j                  j                  j                  |      }|r:| j                  j                  j	                  |       t        j                  |      S y r    )rD   r   r   getr?   jsonloads)r   r:   progress_keyprogress_datas       r   get_progresszCeleryManager.get_progress`   sY    ..s3++//=KK&&|4::m,,r&   c                 P    | j                   j                  j                  |      d uS r    )r   r   rK   r@   s     r   result_readyzCeleryManager.result_readyi   s"    {{""&&s+477r&   c                    | j                   j                  j                  |      }|| j                  S t	        j
                  |      }| j                  | j                  |       n<| j                  r0| j                   j                  j                  || j                         | j                  | j                  |             | j                  |       |S r    )r   r   rK   	UNDEFINEDrL   rM   r   rA   r   rD   r%   )r   r:   r$   results       r   
get_resultzCeleryManager.get_resultl   s    $$((->>>!F# == ""3'{{##**3<t66s;<3r&   c                     | j                   j                  j                  | j                  |            }|i S | j	                  |       t        j                  |      S r    )r   r   rK   _make_set_props_keyrA   rL   rM   )r   r:   updated_propss      r   get_updated_propszCeleryManager.get_updated_props   sO    ++//0H0H0MN Is#zz-((r&   )NNr    )__name__
__module____qualname____doc__r   r%   r-   r5   r;   r*   rA   rI   rP   rR   rV   rZ   __classcell__)r   s   @r   r   r      sC    G'#R+
<(8()r&   r   c                 d     |j                   |j                  d|       d fd	       }|S )Nlong_callback_)namec                      	fd}r|gng 	 fdt               }	
 fd}|j                  |       y )Nc                     t        | t        t        f      s| g} j                  t	        j
                  | t                     y )Ncls)r   listtuplesetrL   dumpsr   )progress_valuecacherN   s    r   _set_progressz3_make_job_fn.<locals>.job_fn.<locals>._set_progress   s3    ntUm<"0!1IIlDJJ~CT$UVr&   c                 f    j                   dt        j                  | |it                     y )Nz
-set_propsre   )ri   rL   rj   r   )_idpropsrl   
result_keys     r   
_set_propsz0_make_job_fn.<locals>.job_fn.<locals>._set_props   s,    II,j)

C<->?r&   c            
         t        di } d| _        t              | _        t	        j
                  |        d}	 t        
t              r	 i 
}n+t        
t        t        f      r g 
 }n
 g 
 }|s,j                  	t        j                  t                     y y # t        $ r2 d}j                  	t        j                  ddit                     Y it        $ rQ}d}j                  	t        j                  dt        |      t!        j"                         di             Y d }~d }~ww xY w)NFT_dash_no_updatere   long_callback_error)msgtb )r   ignore_register_pager   rY   r   ri   r   dictrg   rh   r   rL   rj   r   	Exceptionstr	traceback
format_exc)cerroreduser_callback_outputerrrr   rl   rH   r8   maybe_progressrq   user_callback_argss       r   runz)_make_job_fn.<locals>.job_fn.<locals>.run   sD   ((A%*A"+J7AOa G0$7+-~+TAS+T( 2T5MB+-+S~+S@R+S(+-+R~+R?Q+R(0 		

+?EV W / ! 		JJ*,=>DU  		JJ1'*3x&/&:&:&<4
 
s   AB/ /8E)E1AD==E)r   r   )rq   rN   r   rH   rm   ctxr   rr   r   rl   r8   r9   s   ````   @@r   rF   z_make_job_fn.<locals>.job_fn   s<    	W -5-"	 n'	 '	R 	r&   r    )r   r,   )r8   r   r9   r:   rF   rl   s   ` `  @r   r7   r7      s:    E__N3%0_1: 2:x Mr&   c                       e Zd ZdZy)CeleryLongCallbackManagerz9Deprecated: use `from dash import CeleryManager` instead.N)r[   r\   r]   r^   rx   r&   r   r   r      s    Cr&   r   )rL   r}   contextvarsr   _plotly_utils.utilsr   dash._callback_contextr   dash._utilsr   dash.exceptionsr   #dash.long_callback._proxy_set_propsr   dash.long_callback.managersr	   r   r7   r   rx   r&   r   <module>r      sE      $ 1 0 % ) = ?y)+ y)x@FD Dr&   