o
    ioB                     @  sT  d Z ddlmZ ddlZddlZddlZddlmZm	Z	 ddl
mZ ddlmZmZmZmZmZmZ ddlmZ ddlZddlZddlZddlZddl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&m'Z' d
dl(m)Z) e*e+Z,G dd dZ-G dd deddZ.eddG dd dZ/eddG dd dZ0dS )z	Replayer.    )annotationsN)AbstractAsyncContextManagerasynccontextmanager)	dataclass)AsyncIteratorDictMappingOptionalSequenceType)	TypedDict   )HeaderCodecBehavior   )Interceptor)load_default_build_id)_WorkflowWorker)UnsandboxedWorkflowRunnerWorkflowRunner)SandboxedWorkflowRunnerc                   @  s   e Zd ZdZde e dejjj	g g ddg ddde
jdd;d#d$Zd<d&d'Zd(d)d=d.d/Zd(d)d>d3d4Zd?d6d7Zed@d9d:ZdS )AReplayerz*Replayer to replay workflows from history.NReplayNamespaceF)workflow_task_executorworkflow_runnerunsandboxed_workflow_runner	namespacedata_converterinterceptorspluginsbuild_ididentity workflow_failure_exception_types
debug_moderuntimedisable_safe_workflow_evictionheader_codec_behavior	workflowsSequence[Type]r   /Optional[concurrent.futures.ThreadPoolExecutor]r   r   r   r   strr   "temporalio.converter.DataConverterr   Sequence[Interceptor]r   "Sequence[temporalio.worker.Plugin]r   Optional[str]r    r!   Sequence[Type[BaseException]]r"   boolr#   $Optional[temporalio.runtime.Runtime]r$   r%   r   returnNonec                C  sh   t t||p
tj ||||||	|
|||||d| _|| _|D ]	}|| j| _q| jd s2tddS )ap  Create a replayer to replay workflows from history.

        See :py:meth:`temporalio.worker.Worker.__init__` for a description of
        most of the arguments. Most of the same arguments need to be passed to
        the replayer that were passed to the worker when the workflow originally
        ran.

        Note, unlike the worker, for the replayer the workflow_task_executor
        will default to a new thread pool executor with no max_workers set that
        will be shared across all replay calls and never explicitly shut down.
        Users are encouraged to provide their own if needing more control.
        )r&   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   z'At least one workflow must be specifiedN)	ReplayerConfiglist
concurrentfuturesThreadPoolExecutor_configr   configure_replayer
ValueError)selfr&   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   plugin r=   a/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/worker/_replayer.py__init__#   s,   
zReplayer.__init__r3   c                 C  s   | j  }t|d |d< |S )z|Config, as a dictionary, used to create this replayer.

        Returns:
            Configuration, shallow-copied.
        r&   )r8   copyr4   )r;   configr=   r=   r>   rA   ^   s   
zReplayer.configT)raise_on_replay_failurehistory!temporalio.client.WorkflowHistoryrB   WorkflowReplayResultc             	     s    fdd}|  | 4 I dH $}|2 z3 dH W }|r"|jr"|j|  W  d  I dH  S 6 td1 I dH s;w   Y  dS )a  Replay a workflow for the given history.

        Args:
            history: The history to replay. Can be fetched directly, or use
                :py:meth:`temporalio.client.WorkflowHistory.from_json` to parse
                a history downloaded via ``Temporal CLI`` or the web UI.
            raise_on_replay_failure: If ``True`` (the default), this will raise
                a :py:attr:`WorkflowReplayResult.replay_failure` if it is
                present.
        c                    s    V  d S Nr=   r=   rC   r=   r>   history_iteratory   s   
z2Replayer.replay_workflow.<locals>.history_iteratorNzNo histories)workflow_replay_iteratorreplay_failureRuntimeError)r;   rC   rB   rH   replay_iteratorresultr=   rG   r>   replay_workflowh   s   
zReplayer.replay_workflow	histories0AsyncIterator[temporalio.client.WorkflowHistory]WorkflowReplayResultsc             	     s   |  |4 I dH +}i }|2 z3 dH W }|jr$|r|j|j||jj< q6 t|dW  d  I dH  S 1 I dH s;w   Y  dS )a  Replay workflows for the given histories.

        This is a shortcut for :py:meth:`workflow_replay_iterator` that iterates
        all results and aggregates information about them.

        Args:
            histories: The histories to replay, from an async iterator.
            raise_on_replay_failure: If ``True`` (the default), this will raise
                the first replay failure seen.

        Returns:
            Aggregated results.
        N)replay_failures)rI   rJ   rC   run_idrQ   )r;   rO   rB   rL   rR   rM   r=   r=   r>   replay_workflows   s   0zReplayer.replay_workflows@AbstractAsyncContextManager[AsyncIterator[WorkflowReplayResult]]c                 C  s4   dd }dd }t | jD ]}|||}q|| |S )a}  Replay workflows for the given histories.

        This is a context manager for use via ``async with``. The value is an
        iterator for use via ``async for``.

        Args:
            histories: The histories to replay, from an async iterator.

        Returns:
            An async iterator that returns replayed workflow results as they are
            replayed.
        c                   s    fddS )Nc                   s    | | S rF   )run_replayerrhsnextr<   r=   r>   <lambda>   s    zHReplayer.workflow_replay_iterator.<locals>.make_lambda.<locals>.<lambda>r=   )r<   r[   r=   rZ   r>   make_lambda   s   z6Replayer.workflow_replay_iterator.<locals>.make_lambdac                 S  s
   |  |S rF   )_workflow_replay_iteratorrW   r=   r=   r>   r\      s   
 z3Replayer.workflow_replay_iterator.<locals>.<lambda>)reversedr   )r;   rO   r]   next_functionr<   r=   r=   r>   rI      s
   
z!Replayer.workflow_replay_iterator2AsyncIterator[AsyncIterator[WorkflowReplayResult]]c                  s  zt  dEfdd}d	| jd
  }| jd p!tjj }tdFi d fddd| jd d|d| jd d| jd ddd| jd d| jd d| jd d| jd d| jd d| jd d|jd|ddd| jd  d!dd"d#d d$| jd% t	j
k}tjjj|jtjjjdFi d| jd d|d&| jd' d(| d)| d*d+d,tjjjtjjd+tjjd-tjjd-tjjd-d.d/d-d0d1d2d3d4d3d5d3d6d d7d d8d9d:tjjj| jd
 pt d;d<tjjd+d=tjjd-d>tjjd-\ t | dGfd@dA}| V  W   z@z s?I d H  W n tyQ   tjdBd1dC Y nw W z      I d H  W d S  tys   tjdDd1dC Y d S w z      I d H  W w  ty   tjdDd1dC Y w w   z>z sI d H  W n ty   tjdBd1dC Y nw W z      I d H  W w  ty   tjdDd1dC Y w w z      I d H  W w  ty   tjdDd1dC Y w w )HNrS   r)   
remove_job;temporalio.bridge.proto.workflow_activation.RemoveFromCacher1   r2   c                   sz   |j tjjjjjjkrtj	|j
n$|j tjjjjjjkr5|j tjjjjjjkr5t|j  d|j
 nd    d S )Nz: )reason
temporaliobridgeprotoworkflow_activationRemoveFromCacheEvictionReasonNONDETERMINISMworkflowNondeterminismErrormessage
CACHE_FULLLANG_REQUESTEDrK   set)rS   rb   )last_replay_completelast_replay_failurer=   r>   on_eviction_hook   s"   z<Replayer._workflow_replay_iterator.<locals>.on_eviction_hookzreplay-r   r#   bridge_workerc                     s    S rF   r=   r=   )ru   r=   r>   r\          z4Replayer._workflow_replay_iterator.<locals>.<lambda>r   
task_queuer&   r   max_concurrent_workflow_tasks   r   r   r   r   r!   r"   metric_meterrt    disable_eager_activity_executionFdisable_safe_evictionr$   "should_enforce_versioning_behaviorassert_local_activity_validc                 S  s   d S rF   r=   )ar=   r=   r>   r\      rv   encode_headersr%   identity_overrider    nondeterminism_as_workflow_fail)nondeterminism_as_workflow_fail_for_typesmax_cached_workflowsr   tunerr   )workflow_slot_supplieractivity_slot_supplierlocal_activity_slot_suppliernexus_slot_suppliernonsticky_to_sticky_poll_rationo_remote_activitiesT-sticky_queue_schedule_to_start_timeout_millisi  &max_heartbeat_throttle_interval_millis*default_heartbeat_throttle_interval_millismax_activities_per_second$max_task_queue_activities_per_secondgraceful_shutdown_period_millisr   versioning_strategy)build_id_no_versioningworkflow_task_poller_behavioractivity_task_poller_behaviornexus_task_poller_behavior#AsyncIterator[WorkflowReplayResult]c                   s    2 zF3 d H W }    | jtjjjj| jd	 I d H  t
jt
 gt
jdI d H   r<I d H   sBJ t| dV  q6 d S )N)events)return_when)rC   rJ   )clearpush_historyworkflow_idre   apirC   v1Historyr   SerializeToStringasynciowaitcreate_taskFIRST_COMPLETEDdoneis_setrE   rG   )rO   rr   rs   pusherworkflow_worker_taskr=   r>   rL   .  s0   



z;Replayer._workflow_replay_iterator.<locals>.replay_iteratorzFailed to shutdown worker)exc_infozFailed to finalize shutdown)rS   r)   rb   rc   r1   r2   r=   )r1   r   )!r   Eventr8   re   r#   Runtimedefaultr   rz   r   NO_CODECrf   workerWorker
for_replay_core_runtimeWorkerConfigr   r   TunerHolderFixedSizeSlotSupplierWorkerVersioningStrategyNoner   PollerBehaviorSimpleMaximumr   runcloser   	Exceptionloggerwarninginitiate_shutdownfinalize_shutdown)r;   rO   rt   rw   r#   workflow_workerrL   r=   )ru   rO   rr   rs   r   r   r>   r^      s<  





	







 !
%(+3
!



z"Replayer._workflow_replay_iterator) r&   r'   r   r(   r   r   r   r   r   r)   r   r*   r   r+   r   r,   r   r-   r    r-   r!   r.   r"   r/   r#   r0   r$   r/   r%   r   r1   r2   )r1   r3   )rC   rD   rB   r/   r1   rE   )rO   rP   rB   r/   r1   rQ   )rO   rP   r1   rU   )rO   rP   r1   ra   )__name__
__module____qualname____doc__r   r   re   	converterDataConverterr   r   r   r?   rA   rN   rT   rI   r   r^   r=   r=   r=   r>   r       s2    
; 
r   c                   @  s   e Zd ZU dZded< ded< ded< ded< d	ed
< ded< ded< ded< ded< ded< ded< ded< ded< ded< dS )r3   z>TypedDict of config originally passed to :py:class:`Replayer`.r'   r&   r(   r   r   r   r   r)   r   r*   r   r+   r   r-   r   r    r.   r!   r/   r"   r0   r#   r$   r   r%   Nr   r   r   r   __annotations__r=   r=   r=   r>   r3   b  s    
 r3   F)totalT)frozenc                   @  s$   e Zd ZU dZded< 	 ded< dS )rE   zSingle workflow replay result.rD   rC   zOptional[Exception]rJ   Nr   r=   r=   r=   r>   rE   u  s   
 rE   c                   @  s   e Zd ZU dZded< dS )rQ   z(Results of replaying multiple workflows.zMapping[str, Exception]rR   Nr   r=   r=   r=   r>   rQ     s   
 rQ   )1r   
__future__r   r   concurrent.futuresr5   logging
contextlibr   r   dataclassesr   typingr   r   r   r	   r
   r   typing_extensionsr   temporalio.api.history.v1re   +temporalio.bridge.proto.workflow_activationtemporalio.bridge.workertemporalio.clienttemporalio.convertertemporalio.runtimetemporalio.workflowcommonr   _interceptorr   _workerr   	_workflowr   _workflow_instancer   r   workflow_sandboxr   	getLoggerr   r   r   r3   rE   rQ   r=   r=   r=   r>   <module>   s>     
  D