o
    i^B                     @  s   U d dl m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
 d dlmZmZmZmZmZmZmZ d dlmZmZ d dlZd dlmZ d	ZeeZed
dG dd dZed
dG dd dZed
dG dd dZ ed
dG dd dZ!G dd dZ"G dd deZ#eG dd deZ$eG dd deZ%eG dd deZ&eG dd deZ'ee$e%e&e'f Z(d e)d!< ed
dG d"d# d#eZ*ed
dG d$d% d%Z+G d&d' d'eZ,eee!e,f Z-d e)d(< G d)d* d*Z.d6d0d1Z/G d2d3 d3eZ0ed
dG d4d5 d5e0Z1dS )7    )annotationsN)ABCabstractmethod)	dataclass)	timedelta)AnyCallableLiteralOptionalProtocolUnionruntime_checkable)Self	TypeAlias)WorkerDeploymentVersioni  T)frozenc                   @     e Zd ZU dZded< dS )FixedSizeSlotSupplierzSA fixed-size slot supplier that will never issue more than a fixed number of slots.int	num_slotsN__name__
__module____qualname____doc____annotations__ r   r   _/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/temporalio/worker/_tuning.pyr      s   
 r   c                   @  s$   e Zd ZU dZded< 	 ded< dS )ResourceBasedTunerConfigzXOptions for a :py:class:`ResourceBasedTuner` or a :py:class:`ResourceBasedSlotSupplier`.floattarget_memory_usagetarget_cpu_usageNr   r   r   r   r   r      s   
 r   c                   @  s:   e Zd ZU dZdZded< 	 dZded< 	 dZded< dS )ResourceBasedSlotConfigzYOptions for a specific slot type being used with a :py:class:`ResourceBasedSlotSupplier`.NOptional[int]minimum_slotsmaximum_slotszOptional[timedelta]ramp_throttle)r   r   r   r   r$   r   r%   r&   r   r   r   r   r"   )   s   
 r"   c                   @  "   e Zd ZU dZded< ded< dS )ResourceBasedSlotSupplierzYA slot supplier that will dynamically adjust the number of slots based on resource usage.r"   slot_configr   tuner_configNr   r   r   r   r   r(   :   s
   
 r(   c                   @  s   e Zd ZdZdS )
SlotPermitzA permit to use a slot for a workflow/activity/local activity task.

    You can inherit from this class to add your own data to the permit.
    N)r   r   r   r   r   r   r   r   r+   D   s    r+   c                   @  sL   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S )SlotReserveContextzCContext for reserving a slot from a :py:class:`CustomSlotSupplier`.z1Literal['workflow', 'activity', 'local-activity']	slot_typestr
task_queueworker_identityworker_build_idz!Optional[WorkerDeploymentVersion]worker_deployment_versionbool	is_stickyNr   r   r   r   r   r,   N   s   
 r,   c                   @  r'   )WorkflowSlotInfoz&Info about a workflow task slot usage.r.   workflow_typer3   r4   Nr   r   r   r   r   r5   d      
 r5   c                   @  r   )ActivitySlotInfoz'Info about an activity task slot usage.r.   activity_typeNr   r   r   r   r   r8   m      
 r8   c                   @  r   )LocalActivitySlotInfoz,Info about a local activity task slot usage.r.   r9   Nr   r   r   r   r   r;   u   r:   r;   c                   @  s"   e Zd ZU dZded< ded< dS )NexusSlotInfoz#Info about a nexus task slot usage.r.   service	operationNr   r   r   r   r   r<   }   r7   r<   r   SlotInfoc                   @  $   e Zd ZU dZded< 	 ded< dS )SlotMarkUsedContextzFContext for marking a slot used from a :py:class:`CustomSlotSupplier`.r?   	slot_infor+   permitNr   r   r   r   r   rA         
 rA   c                   @  r@   )SlotReleaseContextzCContext for releasing a slot from a :py:class:`CustomSlotSupplier`.zOptional[SlotInfo]rB   r+   rC   Nr   r   r   r   r   rE      rD   rE   c                   @  sH   e Zd ZdZedddZedd	d
ZedddZedddZdS )CustomSlotSupplierzGThis class can be implemented to provide custom slot supplier behavior.ctxr,   returnr+   c                   s   dS )a]  This function is called before polling for new tasks. Your implementation must block until a
        slot is available then return a permit to use that slot.

        The only acceptable exception to throw is :py:class:`asyncio.CancelledError`, as invocations of this method may
        be cancelled. Any other exceptions thrown will be logged and ignored.

        It is technically possible but rare, during worker shutdown, for this method to be called and return a value,
        but the Rust Core may not have a chance to _observe_ that value. In such cases the returned permit will not be
        released. The permit will, however, be forgotten and python will garbage collect it. So if you use the same slot
        supplier over the lifetime of more than one worker and it is critically important for you to clean up some
        resources associated all permits you construct, then consider using a finalizer on your returned permits.

        Args:
            ctx: The context for slot reservation.

        Returns:
            A permit to use the slot which may be populated with your own data.
        Nr   selfrG   r   r   r   reserve_slot   s   zCustomSlotSupplier.reserve_slotOptional[SlotPermit]c                 C     dS )a  This function is called when trying to reserve slots for "eager" workflow and activity tasks.
        Eager tasks are those which are returned as a result of completing a workflow task, rather than
        from polling. Your implementation must not block, and if a slot is available, return a permit
        to use that slot.

        Args:
            ctx: The context for slot reservation.

        Returns:
            Maybe a permit to use the slot which may be populated with your own data.
        Nr   rI   r   r   r   try_reserve_slot   s   z#CustomSlotSupplier.try_reserve_slotrA   Nonec                 C  rM   )a  This function is called once a slot is actually being used to process some task, which may be
        some time after the slot was reserved originally. For example, if there is no work for a
        worker, a number of slots equal to the number of active pollers may already be reserved, but
        none of them are being used yet. This call should be non-blocking.

        Args:
            ctx: The context for marking a slot as used.
        Nr   rI   r   r   r   mark_slot_used   s   
z!CustomSlotSupplier.mark_slot_usedrE   c                 C  rM   )aX  This function is called once a permit is no longer needed. This could be because the task has
        finished, whether successfully or not, or because the slot was no longer needed (ex: the number
        of active pollers decreased). This call should be non-blocking.

        Args:
            ctx: The context for releasing a slot.
        Nr   rI   r   r   r   release_slot   s   	zCustomSlotSupplier.release_slotN)rG   r,   rH   r+   rG   r,   rH   rL   rG   rA   rH   rO   rG   rE   rH   rO   )	r   r   r   r   r   rK   rN   rP   rQ   r   r   r   r   rF      s    rF   SlotSupplierc                   @  s>   e Zd ZdddZdddZdddZdddZdddZdS )_BridgeSlotSupplierWrappersupplierrF   c                 C  s
   || _ d S N)	_supplier)rJ   rW   r   r   r   __init__   s   
z#_BridgeSlotSupplierWrapper.__init__rG   r,   
reserve_cbCallable[[Any], None]rH   r+   c                   sX   zt | j|}|| |I d H W S  t jy     ty+   tjddd  w )Nz,Error in custom slot supplier `reserve_slot`Texc_info)asynciocreate_taskrY   rK   CancelledError	Exceptionloggerwarning)rJ   rG   r[   reserve_futr   r   r   rK      s   z'_BridgeSlotSupplierWrapper.reserve_slotrL   c                 C  s2   z| j |W S  ty   tjddd Y d S w )Nz0Error in custom slot supplier `try_reserve_slot`Tr]   )rY   rN   rb   rc   rd   rI   r   r   r   rN      s   z+_BridgeSlotSupplierWrapper.try_reserve_slotrE   rO   c                 C  6   z	| j | W d S  ty   tjddd Y d S w )Nz,Error in custom slot supplier `release_slot`Tr]   )rY   rQ   rb   rc   rd   rI   r   r   r   rQ        z'_BridgeSlotSupplierWrapper.release_slotrA   c                 C  rf   )Nz.Error in custom slot supplier `mark_slot_used`Tr]   )rY   rP   rb   rc   rd   rI   r   r   r   rP   
  rg   z)_BridgeSlotSupplierWrapper.mark_slot_usedN)rW   rF   )rG   r,   r[   r\   rH   r+   rR   rT   rS   )r   r   r   rZ   rK   rN   rQ   rP   r   r   r   r   rV      s    



	rV   slot_supplierkind:Literal['workflow', 'activity', 'local_activity', 'nexus']rH   %temporalio.bridge.worker.SlotSupplierc              	   C  s   t | trtjj| jS t | trc|dkrdnd}t}|dkr%tddntdd}| j	j
d ur4| j	j
}| j	jd ur>| j	j}| j	jd urH| j	j}tjj||t|tdd tjj| jj| jjS t | trqtjjt| S td|  )	Nworkflow      r   )seconds2   )millisecondszUnknown slot supplier type: )
isinstancer   
temporaliobridgeworkerr   r(   _DEFAULT_RESOURCE_SLOTS_MAXr   r)   r$   r%   r&   r   r   r*   r    r!   rF   BridgeCustomSlotSupplierrV   	TypeError)rh   ri   	min_slots	max_slotsr&   r   r   r   _to_bridge_slot_supplier  s6   


	r{   c                   @  s   e Zd ZdZedddddd0ddZedddddd1ddZed2ddZed3ddZ	ed3d d!Z
ed3d"d#Zed3d$d%Zd4d'd(Zd5d)d*Zd5d+d,Zed6d.d/ZdS )7WorkerTunerzXWorkerTuners allow for the dynamic customization of some aspects of worker configurationN)workflow_configactivity_configlocal_activity_confignexus_configr    r   r!   r}   !Optional[ResourceBasedSlotConfig]r~   r   r   rH   c                C  sX   t ||}t|p
t |}t|pt |}	t|pt |}
t|p"t |}t||	|
|S )z8Create a resource-based tuner with the provided options.)r   r(   r"   _CompositeTuner)clsr    r!   r}   r~   r   r   resource_cfgwfact	local_actnexusr   r   r   create_resource_based9  s&   




z!WorkerTuner.create_resource_based)workflow_slotsactivity_slotslocal_activity_slotsnexus_slotsr   r#   r   r   r   c                C  sB   t t|r|ndt|r|ndt|r|ndt|r|S dS )zCreate a fixed-size tuner with the provided number of slots.

        Any unspecified slot numbers will default to 100.
        d   )r   r   )r   r   r   r   r   r   r   r   create_fixedY  s   

zWorkerTuner.create_fixedworkflow_supplierrU   activity_supplierlocal_activity_suppliernexus_supplierc                C  s   t ||||S )z7Create a tuner composed of the provided slot suppliers.)r   )r   r   r   r   r   r   r   r   create_compositeo  s   
zWorkerTuner.create_compositec                 C     t rX   NotImplementedErrorrJ   r   r   r    _get_workflow_task_slot_supplier     z,WorkerTuner._get_workflow_task_slot_supplierc                 C  r   rX   r   r   r   r   r    _get_activity_task_slot_supplier  r   z,WorkerTuner._get_activity_task_slot_supplierc                 C  r   rX   r   r   r   r   r   &_get_local_activity_task_slot_supplier  r   z2WorkerTuner._get_local_activity_task_slot_supplierc                 C  r   rX   r   r   r   r   r   _get_nexus_slot_supplier  r   z$WorkerTuner._get_nexus_slot_supplier$temporalio.bridge.worker.TunerHolderc                 C  s<   t jjt|  dt|  dt|  dt|  dS )Nrl   activitylocal_activityr   )	rs   rt   ru   TunerHolderr{   r   r   r   r   r   r   r   r   _to_bridge_tuner  s   zWorkerTuner._to_bridge_tunerc                 C     t |  S rX   )r|   _get_slot_supplier_maxr   r   r   r   r   _get_activities_max  s   zWorkerTuner._get_activities_maxc                 C  r   rX   )r|   r   r   r   r   r   r   _get_nexus_tasks_max  s   z WorkerTuner._get_nexus_tasks_maxrh   c                 C  s*   t | tr| jS t | tr| jjptS d S rX   )rr   r   r   r(   r)   r%   rv   )rh   r   r   r   r     s   


z"WorkerTuner._get_slot_supplier_max)r    r   r!   r   r}   r   r~   r   r   r   r   r   rH   r|   )
r   r#   r   r#   r   r#   r   r#   rH   r|   )
r   rU   r   rU   r   rU   r   rU   rH   r|   rH   rU   )rH   r   )rH   r#   )rh   rU   rH   r#   )r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r|   6  s:    


r|   c                   @  sZ   e Zd ZU dZded< ded< ded< ded< ddd	Zdd
dZdddZdddZdS )r   zHThis tuner allows for different slot suppliers for different slot types.rU   workflow_slot_supplieractivity_slot_supplierlocal_activity_slot_suppliernexus_slot_supplierrH   c                 C     | j S rX   )r   r   r   r   r   r        z0_CompositeTuner._get_workflow_task_slot_supplierc                 C  r   rX   )r   r   r   r   r   r     r   z0_CompositeTuner._get_activity_task_slot_supplierc                 C  r   rX   )r   r   r   r   r   r     r   z6_CompositeTuner._get_local_activity_task_slot_supplierc                 C  r   rX   )r   r   r   r   r   r     r   z(_CompositeTuner._get_nexus_slot_supplierNr   )	r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   
 


r   )rh   rU   ri   rj   rH   rk   )2
__future__r   r_   loggingabcr   r   dataclassesr   datetimer   typingr   r   r	   r
   r   r   r   typing_extensionsr   r   temporalio.bridge.workerrs   temporalio.commonr   rv   	getLoggerr   rc   r   r   r"   r(   r+   r,   r5   r8   r;   r<   r?   r   rA   rE   rF   rU   rV   r{   r|   r   r   r   r   r   <module>   s\    $
	


	@
.#{