+
    }i-                    n   ^ RI Ht ^ RIt^ RIt^ RIHt ^ RIHt ^ RIH	t	 ^ RI
Ht ^ RIHtHtHtHtHtHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIHt ^ RIHt ^ RIHtHtH t H!t!H"t"H#t#H$t$H%t% ]'       d   ^ RI&H't'H(t( ^ RI)H*t* ^ RI+H,t, ^ RIH-t-H.t. ]! ]/4      t0 ! R R]4      t1]! R4       ! R R4      4       t2R# )    )annotationsN)defaultdict)	timedelta)wraps)	getLogger)TYPE_CHECKINGAnyLiteral	TypedDictcastoverload)AsyncIOEventEmitter)ensure_context)
docs_group)RecurringTask)wait_for_all_tasks_for_finish)EventEventAbortingDataEventCrawlerStatusDataEventExitDataEventListenerEventMigratingDataEventPersistStateDataEventSystemInfoData)	AwaitableCallable)TracebackType)NotRequired)	EventDataWrappedListenerc                  2    ] tR t^)t$ RtR]R&    R]R&   RtR# )EventManagerOptionsz{Arguments for the `EventManager` constructor.

It is intended for typing forwarded `__init__` arguments in the subclasses.
zNotRequired[timedelta]persist_state_intervalzNotRequired[timedelta | None]close_timeout N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r%       o/Users/ahmad/.openclaw/workspace/my-crawler/.venv/lib/python3.14/site-packages/crawlee/events/_event_manager.pyr"   r"   )   s    
 32W00^r-   r"   zEvent managersc                     ] tR t^6tRtR]! ^R7      RR/R R llt]R R	 l4       tR
 R lt	R R lt
]R R l4       t]R R l4       t]R R l4       t]R R l4       t]R R l4       t]R R l4       t]R R l4       tR R ltRR/R R  llt]R! R" l4       t]R# R$ l4       t]R% R& l4       t]R' R( l4       t]R) R* l4       t]R+ R, l4       t]R- R. l4       t]R/ R0 l4       t]R1R/R2 R3 ll4       tR4 R5 ltR6tR# )7EventManagera  Manage events and their listeners, enabling registration, emission, and execution control.

It allows for registering event listeners, emitting events, and ensuring all listeners complete their execution.
Built on top of `pyee.asyncio.AsyncIOEventEmitter`. It implements additional features such as waiting for all
listeners to complete and emitting `PersistState` events at regular intervals.
r#   )minutesr$   Nc               $    V ^8  d   QhRRRRRR/# )   r#   r   r$   timedelta | NonereturnNoner%   )formats   "r.   __annotate__EventManager.__annotate__?   s*     " " !*" (	"
 
"r-   c                   Wn         W n        \        4       V n        \	        4       V n        \        R 4      V n        \        V P                  V P                   R7      V n
        RV n        R# )zInitialize a new instance.

Args:
    persist_state_interval: Interval between emitted `PersistState` events to maintain state persistence.
    close_timeout: Optional timeout for canceling pending event listeners if they exceed this duration.
c                      \        \        4      # N)r   listr%   r-   r.   <lambda>'EventManager.__init__.<locals>.<lambda>W   s	    K%r-   )funcdelayFN)_persist_state_interval_close_timeoutr   _event_emitterset_listener_tasksr   _listeners_to_wrappersr   _emit_persist_state_event"_emit_persist_state_event_rec_task_active)selfr#   r$   s   &$$r.   __init__EventManager.__init__?   si     (>$+ 23 36% ep%e
#
 3@//..3
/ r-   c                   V ^8  d   QhRR/# )r3   r5   boolr%   )r7   s   "r.   r8   r9   d   s       r-   c                    V P                   # )z'Indicate whether the context is active.)rJ   rK   s   &r.   activeEventManager.activec   s     ||r-   c                   V ^8  d   QhRR/# )r3   r5   r0   r%   )r7   s   "r.   r8   r9   h   s      , r-   c                   "   V P                   '       d$   \        RV P                  P                   R24      hRV n         V P                  P                  4        V # 5i)zInitialize the event manager upon entering the async context.

Raises:
    RuntimeError: If the context manager is already active.
The z is already active.T)rJ   RuntimeError	__class__r&   rI   startrQ   s   &r.   
__aenter__EventManager.__aenter__h   sN      <<<dnn&=&=%>>QRSS//557s   AAc               (    V ^8  d   QhRRRRRRRR/# )	r3   exc_typeztype[BaseException] | None	exc_valuezBaseException | Noneexc_tracebackzTracebackType | Noner5   r6   r%   )r7   s   "r.   r8   r9   u   s2      , ( ,	
 
r-   c                  "   V P                   '       g$   \        RV P                  P                   R24      hV P                  P                  4       G Rj  xL
  V P                  4       G Rj  xL
  V P                  V P                  R7      G Rj  xL
  V P                  P                  4        V P                  P                  4        V P                  P                  4        RV n         R#  L L L_5i)zClose the local event manager upon exiting the async context.

This will stop listening for the events, and it will wait for all the event listeners to finish.

Raises:
    RuntimeError: If the context manager is not active.
rV   z is not active.N)timeoutF)rJ   rW   rX   r&   rI   stoprH   "wait_for_all_listeners_to_completerC   rD   remove_all_listenersrF   clearrG   )rK   r]   r^   r_   s   &&&&r.   	__aexit__EventManager.__aexit__u   s      |||dnn&=&=%>oNOO 55::<<<,,...55d>Q>Q5RRR002""$##))+ 	=.Rs7   AC3C-C3-C/.#C3C1AC3/C31C3c               $    V ^8  d   QhRRRRRR/# )r3   eventLiteral[Event.PERSIST_STATE]listenerz$EventListener[EventPersistStateData]r5   r6   r%   )r7   s   "r.   r8   r9      s    uu7uCgulpur-   c               	    R # r<   r%   rK   ri   rk   s   &$$r.   onEventManager.on   s    rur-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   Literal[Event.SYSTEM_INFO]rk   z"EventListener[EventSystemInfoData]r5   r6   r%   )r7   s   "r.   r8   r9      s    qq5qAcqhlqr-   c               	    R # r<   r%   rm   s   &$$r.   rn   ro      s    nqr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   Literal[Event.MIGRATING]rk   z!EventListener[EventMigratingData]r5   r6   r%   )r7   s   "r.   r8   r9      s    nn3n?`neinr-   c               	    R # r<   r%   rm   s   &$$r.   rn   ro      s    knr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   Literal[Event.ABORTING]rk   z EventListener[EventAbortingData]r5   r6   r%   )r7   s   "r.   r8   r9      s    ll2l>^lcglr-   c               	    R # r<   r%   rm   s   &$$r.   rn   ro          ilr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   Literal[Event.EXIT]rk   zEventListener[EventExitData]r5   r6   r%   )r7   s   "r.   r8   r9      s    dd.d:Vd[_dr-   c               	    R # r<   r%   rm   s   &$$r.   rn   ro      s    adr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   Literal[Event.CRAWLER_STATUS]rk   z%EventListener[EventCrawlerStatusData]r5   r6   r%   )r7   s   "r.   r8   r9      s    ww8wDiwnrwr-   c               	    R # r<   r%   rm   s   &$$r.   rn   ro      s    twr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   r   rk   zEventListener[None]r5   r6   r%   )r7   s   "r.   r8   r9      s    MM5M,?MDMr-   c               	    R # r<   r%   rm   s   &$$r.   rn   ro      s    JMr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   r   rk   zEventListener[Any]r5   r6   r%   )r7   s   "r.   r8   r9      s'     0H 0H5 0H,> 0H4 0Hr-   c                  a aaa \         P                  ! S4      o\        \        RS4      4      R VVV V3R ll4       pS P                  S,          S,          P                  V4       S P                  P                  SP                  V4       R# )zRegister an event listener for a specific event.

Args:
    event: The event for which to listen to.
    listener: The function (sync or async) which is to be called when the event is emitted.
z%Callable[..., None | Awaitable[None]]c                    V ^8  d   QhRRRR/# )r3   
event_datar   r5   r6   r%   )r7   s   "r.   r8   %EventManager.on.<locals>.__annotate__   s     #	; #	;y #	;T #	;r-   c                N  <"    SP                  V 4      p\        P                  ! S4      '       d   S! VP                  / VP
                  B M7\        P                  ! \        R S4      .VP                  O5/ VP
                  B p\        SR4      '       d   SP                  MSP                  P                  p\        P                  ! VRSP                   RV 2R7      pSP                  P                  V4        \         P#                  R4       VG Rj  xL
  \         P#                  R4       \         P#                  R4       SP                  P)                  V4       R#   \         d    SP                  4       p ELli ; i Lm  \$         d[    \         P'                  RR	SP                  R
\        SR4      '       d   SP                  MSP                  P                  /R7        Li ; i  \         P#                  R4       SP                  P)                  T4       i ; i5i)zCallable[..., None]r&   Task--namez=EventManager.on.listener_wrapper(): Awaiting listener task...Nz<EventManager.on.listener_wrapper(): Listener task completed.zException in the event listener
event_namelistener_name)extrazJEventManager.on.listener_wrapper(): Removing listener task from the set...)bind	TypeErrorinspectiscoroutinefunctionargskwargsasyncio	to_threadr   hasattrr&   rX   create_taskvaluerF   addloggerdebug	Exception	exceptionremove)	r   
bound_argscoror   listener_taskri   rk   rK   	signatures	   &    r.   listener_wrapper)EventManager.on.<locals>.listener_wrapper   s    .&^^J7
 ..x88 *//?Z->->?&&t,A8'Ltztblbsbst  29:1N1NH--T\TfTfToToM#//U5;;-qQ^P_;`aM  $$]3;\]###[\ ij$$++M:A  .&^^-
." $ 
  5$ekk'"8Z88 *2):):%//88	 ! 
 ij$$++M:sp   H%E$ H%CH%>F FF 22H%$F?H%FH%F A"G-*G0 ,G--G0 02H""H%N)	r   r   r   r   rG   appendrD   add_listenerr   )rK   ri   rk   r   r   s   fdd @r.   rn   ro      sw     %%h/		t;XF	G#	; #	; 
H#	;J 	##E*84;;<LM((6FGr-   rk   c               $    V ^8  d   QhRRRRRR/# )r3   ri   r   rk   zEventListener[Any] | Noner5   r6   r%   )r7   s   "r.   r8   r9      s(     B BE B-F BRV Br-   c               V   V'       d`   V P                   V,          V,           F)  pV P                  P                  VP                  V4       K+  	  . V P                   V,          V&   R# \	        \
        4      V P                   V&   V P                  P                  VP                  4       R# )zRemove a specific listener or all listeners for an event.

Args:
    event: The Actor event for which to remove listeners.
    listener: The listener which is supposed to be removed. If not passed, all listeners of this event
        are removed.
N)rG   rD   remove_listenerr   r   r=   rd   )rK   ri   rk   r   s   &$$ r.   offEventManager.off   s     $($?$?$Fx$P$P ##33EKKAQR %Q;=D''.x81<T1BD''.44U[[Ar-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   rj   r   r   r5   r6   r%   )r7   s   "r.   r8   r9      s    jj9jG\jaejr-   c               	    R # r<   r%   rK   ri   r   s   &$$r.   emitEventManager.emit   s    gjr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   rq   r   r   r5   r6   r%   )r7   s   "r.   r8   r9      s    ff7fEXf]afr-   c               	    R # r<   r%   r   s   &$$r.   r   r      s    cfr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   rt   r   r   r5   r6   r%   )r7   s   "r.   r8   r9      s    cc5cCUcZ^cr-   c               	    R # r<   r%   r   s   &$$r.   r   r      s    `cr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   rw   r   r   r5   r6   r%   )r7   s   "r.   r8   r9      s    aa4aBSaX\ar-   c               	    R # r<   r%   r   s   &$$r.   r   r      s    ^ar-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   r{   r   r   r5   r6   r%   )r7   s   "r.   r8   r9      s    YY0YmYPTYr-   c               	    R # r<   r%   r   s   &$$r.   r   r      s    VYr-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   r~   r   r   r5   r6   r%   )r7   s   "r.   r8   r9      s    ll:lH^lcglr-   c               	    R # r<   r%   r   s   &$$r.   r   r      ry   r-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   r   r   r	   r5   r6   r%   )r7   s   "r.   r8   r9      s    AAUAAAr-   c               	    R # r<   r%   r   s   &$$r.   r   r      s    >Ar-   c               $    V ^8  d   QhRRRRRR/# )r3   ri   r   r   r   r5   r6   r%   )r7   s   "r.   r8   r9      s!     : :U :	 :d :r-   c               R    V P                   P                  VP                  V4       R# )zEmit an event with the associated data to all registered listeners.

Args:
    event: The event which will be emitted.
    event_data: The data which will be passed to the event listeners.
N)rD   r   r   r   s   &$$r.   r   r      s     	  j9r-   ra   c                    V ^8  d   QhRRRR/# )r3   ra   r4   r5   r6   r%   )r7   s   "r.   r8   r9      s      Y YCS Y_c Yr-   c                  a "   R V 3R llp\         P                  ! V! 4       RVP                   2R7      .p\        V\        VR7      G Rj  xL
  R#  L5i)zWait for all currently executing event listeners to complete.

Args:
    timeout: The maximum time to wait for the event listeners to finish. If they do not complete within
        the specified timeout, they will be canceled.
c                   V ^8  d   QhRR/# r3   r5   r6   r%   )r7   s   "r.   r8   EEventManager.wait_for_all_listeners_to_complete.<locals>.__annotate__  s     	] 	]$ 	]r-   c                    <"   \         P                  ! SP                  RR/ G Rj  xL
 p V  F2  p\        V\        4      '       g   K  \
        P                  RVR7       K4  	  R#  L>5i)z[Gathers all listener tasks and awaits their completion, logging any exceptions encountered.return_exceptionsTNz#Event listener raised an exception.)exc_info)r   gatherrF   
isinstancer   r   r   )resultsresultrK   s     r.   wait_for_listenersKEventManager.wait_for_all_listeners_to_complete.<locals>.wait_for_listeners  sU     #NND,@,@YTXYYG!fi00$$%JU[$\ " Zs   $A(A&A(	A(r   r   )tasksr   ra   N)r   r   r&   r   r   )rK   ra   r   r   s   f$  r.   rc   /EventManager.wait_for_all_listeners_to_complete   sO     	] 	] $$%7%9%HZHcHcGd@efg+%PWXXXs   A
AAAc                   V ^8  d   QhRR/# r   r%   )r7   s   "r.   r8   r9     s     c c cr-   c                d   "   V P                  \        P                  \        RR7      R7       R# 5i)z;Emit a persist state event with the given migration status.F)is_migrating)ri   r   N)r   r   PERSIST_STATEr   rQ   s   &r.   rH   &EventManager._emit_persist_state_event  s"     		++8M[`8a	bs   .0)rJ   rC   rI   rD   rF   rG   rB   )r&   r'   r(   r)   r*   r   rL   propertyrR   rZ   rf   r   rn   r   r   r   rc   rH   r,   r%   r-   r.   r0   r0   6   s^   " -6a,@" +/	"H  2 u uq qn nl ld dw wM M0HdB B  j jf fc ca aY Yl lA A: : YVZ Y Y&c cr-   r0   )3
__future__r   r   r   collectionsr   datetimer   	functoolsr   loggingr   typingr   r	   r
   r   r   r   pyee.asyncior   crawlee._utils.contextr   crawlee._utils.docsr   crawlee._utils.recurring_taskr   crawlee._utils.waitr   crawlee.events._typesr   r   r   r   r   r   r   r   collections.abcr   r   typesr   typing_extensionsr   r   r    r&   r   r"   r0   r%   r-   r.   <module>r      s    #   #    I I , 1 * 7 =	 	 	 3#-@	8	
_) 
_ Wc Wc Wcr-   