
    !<i"                         d Z ddlZddlmZmZmZ ddlmZmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZmZ  e
d	          Z ed
          Z G d d          ZdS )z
Agenda - A collection of tasks that can be scheduled together.

The Agenda class provides a way to collect multiple tasks and then scatter them
evenly over a time period to avoid overwhelming the system with immediate work.
    N)datetime	timedeltatimezone)Any	AwaitableCallableIterator	ParamSpecTypeVaroverload   )uuid7)Docket)	ExecutionTaskFunctionPRc                   n   e Zd ZdZddZdefdZdeee	e
z  eedf         ee
ef         f                  fdZedeeee         f         deedf         fd	            Zede
ded
         fd            Zdeeee         f         e
z  ded
         fdZddZ	 	 ddedededz  dedz  dee         f
dZdS )Agendaa  A collection of tasks to be scheduled together on a Docket.

    The Agenda allows you to build up a collection of tasks with their arguments,
    then schedule them all at once using various timing strategies like scattering.

    Example:
        >>> agenda = Agenda()
        >>> agenda.add(process_item)(item1)
        >>> agenda.add(process_item)(item2)
        >>> agenda.add(send_email)(email)
        >>> await agenda.scatter(docket, over=timedelta(minutes=50))
    returnNc                     g | _         dS )zInitialize an empty Agenda.N)_tasksselfs    /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/docket/agenda.py__init__zAgenda.__init__#   s      	    c                 *    t          | j                  S )z)Return the number of tasks in the agenda.)lenr   r   s    r   __len__zAgenda.__len__)   s    4;r   .c                 *    t          | j                  S )z!Iterate over tasks in the agenda.)iterr   r   s    r   __iter__zAgenda.__iter__-   s     DK   r   functionc                     dS )zAdd a task function to the agenda.

        Args:
            function: The task function to add.

        Returns:
            A callable that accepts the task arguments.
        N r   r$   s     r   addz
Agenda.add3         r   ).Nc                     dS )zAdd a task by name to the agenda.

        Args:
            function: The name of a registered task.

        Returns:
            A callable that accepts the task arguments.
        Nr&   r'   s     r   r(   z
Agenda.addA   r)   r   c                 8     dt           dt           ddf fd}|S )zAdd a task to the agenda.

        Args:
            function: The task function or name to add.

        Returns:
            A callable that accepts the task arguments and adds them to the agenda.
        argskwargsr   Nc                  B    j                             | |f           d S )N)r   append)r,   r-   r$   r   s     r   	schedulerzAgenda.add.<locals>.scheduler\   s&    K$788888r   )r   )r   r$   r0   s   `` r   r(   z
Agenda.addO   sD    	9S 	9C 	9D 	9 	9 	9 	9 	9 	9 	9 r   c                 8    | j                                          dS )z Clear all tasks from the agenda.N)r   clearr   s    r   r2   zAgenda.cleara   s    r   docketoverstartjitterc           
      l  K   |                                 dk    rt          d          | j        sg S t          j        t
          j                  t          | j                  }|dk    r
|dz  z   g}n$||dz
  z  fdt          |          D             }|ryg }|D ]r}t          t          j        |                                  |                                                     }	t          ||	z             }
|                    |
           s|}g }t          | j        |          D ]\  \  }}}}t          |t                     r*||j        vrt%          d| d	          |j        |         }n2||j                                        vr|                    |           |}t!          t+                                }t-          ||||||d
          }|                    |           |D ]A}|                    |j        |j        |j                  } ||j        i |j         d{V  B|S )am  Scatter the tasks in this agenda over a time period.

        Tasks are distributed evenly across the specified time window,
        optionally with random jitter to prevent thundering herd effects.

        If an error occurs during scheduling, some tasks may have already been
        scheduled successfully before the failure occurred.

        Args:
            docket: The Docket to schedule tasks on.
            over: Time period to scatter tasks over (required).
            start: When to start scattering from. Defaults to now.
            jitter: Maximum random offset to add/subtract from each scheduled time.

        Returns:
            List of Execution objects for the scheduled tasks.

        Raises:
            KeyError: If any task name is not registered with the docket.
            ValueError: If any task is stricken or 'over' is not positive.
        r   z,'over' parameter must be a positive durationNr      c                      g | ]
}|z  z   S r&   r&   ).0iintervalr5   s     r   
<listcomp>z"Agenda.scatter.<locals>.<listcomp>   s"    NNNqehl2NNNr   )secondszTask 'z' is not registered)r3   r$   r,   r-   keywhenattempt)r@   r?   )total_seconds
ValueErrorr   r   nowr   utcr   ranger   randomuniformmaxr/   zip
isinstancestrtasksKeyErrorvaluesregisterr   r   r(   r$   r@   r?   r,   r-   )r   r3   r4   r5   r6   
task_countschedule_timesjittered_timesschedule_timeoffsetjittered_time
executions	task_funcr,   r-   resolved_funcr?   	executionr0   r<   s      `               @r   scatterzAgenda.scattere   s     8 1$$KLLL{ 	I=L..E %%
??#dQh./NN zA~.HNNNNNE*<M<MNNNN  	,-/N!/ 	5 	5""N--///1E1E1G1G    !$MF$:E B B%%m4444+N ')
8;K9
 9
 	) 	)4%Yf} )S)) *FL00"#JI#J#J#JKKK &Y 7 FL$7$7$9$999OOI... ) egg,,C!&"  I i(((( $ 	A 	AI

"Y] #  I )Y^@y/?@@@@@@@@@@r   )r   N)NN)__name__
__module____qualname____doc__r   intr    r	   tupler   rL   r   dictr#   r   r   r   r   r   r(   r2   r   r   r   listr   r[   r&   r   r   r   r      s                    !	%s*E#s(OT#s(^KL	M! ! ! ! 1il?+ 
!T'	   X  
)	   X1il?+c1 
)	   $    "&#'e ee e $	e
 D e 
ie e e e e er   r   )r_   rG   r   r   r   typingr   r   r   r	   r
   r   r   _uuid7r   r3   r   rZ   r   r   r   r   r   r&   r   r   <module>rf      s      2 2 2 2 2 2 2 2 2 2 S S S S S S S S S S S S S S S S S S             . . . . . . . .IcNNGCLLu u u u u u u u u ur   