o
    dN                     @   s   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 G dd	 d	eZd
d ZG dd deZdS )    N)copy_context)PlotlyJSONEncoder)context_value)AttributeDict)PreventUpdate)BaseLongCallbackManagerc                       st   e Zd ZdZd fdd	Zdd Zdd Zd	d
 ZdddZdd Z	dd Z
dd Zdd Zdd Zdd Z  ZS )CeleryManagerz=Manage background execution of callbacks with a celery queue.Nc              
      s   zddl }ddlm} W n ty } ztd|d}~ww t||js(tdt|j|r2td|| _|| _	t
 | dS )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__ o/var/www/html/visualizacion-main/env/lib/python3.10/site-packages/dash/long_callback/managers/celery_manager.pyr      s$   zCeleryManager.__init__c                 C   s   |d u rd S | j j| d S N)r   control	terminater   jobr   r   r   terminate_job9   s   zCeleryManager.terminate_jobc                 C   s&   |  |}|r|jdv r| |S dS )N)FAILUREREVOKEDF)get_taskstatusr"   )r   r!   taskr   r   r   terminate_unhealthy_job?   s   

z%CeleryManager.terminate_unhealthy_jobc                 C   s   |  |}|o|jdv S )N)PENDINGRECEIVEDSTARTEDRETRYPROGRESS)r%   r&   )r   r!   futurer   r   r   job_runningE   s   
zCeleryManager.job_runningc                 C   s   t || j||S r   )_make_job_fnr   )r   fnprogresskeyr   r   r   make_job_fnO   s   zCeleryManager.make_job_fnc                 C   s   |r| j |S d S r   )r   AsyncResultr    r   r   r   r%   R   s   zCeleryManager.get_taskc                 C   s   | j j| d S r   )r   r   deleter   r3   r   r   r   clear_cache_entryX      zCeleryManager.clear_cache_entryc                 C   s   | || |||}|jS r   )delay_make_progress_keytask_id)r   r3   job_fnargscontextr'   r   r   r   call_job_fn[   s   zCeleryManager.call_job_fnc                 C   s8   |  |}| jj|}|r| jj| t|S d S r   )r;   r   r   getr6   jsonloads)r   r3   progress_keyprogress_datar   r   r   get_progress_   s   

zCeleryManager.get_progressc                 C   s   | j j|d uS r   )r   r   rA   r7   r   r   r   result_readyh   r9   zCeleryManager.result_readyc                 C   sr   | j j|}|d u r| jS t|}| jd u r| | n| jr*| j j|| j | | 	| | 
| |S r   )r   r   rA   	UNDEFINEDrB   rC   r   r8   r   r;   r"   )r   r3   r!   resultr   r   r   
get_resultk   s   


zCeleryManager.get_result)NNr   )__name__
__module____qualname____doc__r   r"   r(   r/   r4   r%   r8   r@   rF   rG   rJ   __classcell__r   r   r   r   r      s    )

	r   c                    s.   |j  |jd| dd fdd	}|S )Nlong_callback_)namec                    sF   fdd}r|gng t  } fdd}|| d S )Nc                    s.   t | ttfs
| g}  tj| td d S )Ncls)r   listtuplesetrB   dumpsr   )progress_value)cacherD   r   r   _set_progress   s   z3_make_job_fn.<locals>.job_fn.<locals>._set_progressc                     s  t di } d| _t|  z)ttri }ntttfr.g R  }n	g R  }W n; tyO    t	j
dditd Y d S  tys } z t	
dt|t di W Y d }~d S d }~ww  t	j
|td d S )NF_dash_no_updaterR   long_callback_error)msgtbr   )r   ignore_register_pager   rV   r   dictrT   rU   r   rB   rW   r   	Exceptionstr	traceback
format_exc)cuser_callback_outputerr)rY   r?   r1   maybe_progress
result_keyuser_callback_argsr   r   run   sB   


z)_make_job_fn.<locals>.job_fn.<locals>.run)r   rk   )ri   rD   rj   r?   rZ   ctxrk   rY   r1   r2   )r?   rh   rD   ri   rj   r   r=      s
   $z_make_job_fn.<locals>.job_fnr   )r   r'   )r1   r   r2   r3   r=   r   rm   r   r0      s   1r0   c                   @   s   e Zd ZdZdS )CeleryLongCallbackManagerz9Deprecated: use `from dash import CeleryManager` instead.N)rK   rL   rM   rN   r   r   r   r   rn      s    rn   )rB   rc   contextvarsr   _plotly_utils.utilsr   dash._callback_contextr   dash._utilsr   dash.exceptionsr   dash.long_callback.managersr   r   r0   rn   r   r   r   r   <module>   s    s8