Class BaseScheduleAgentTask

java.lang.Object
io.agentscope.extensions.scheduler.BaseScheduleAgentTask
All Implemented Interfaces:
ScheduleAgentTask<io.agentscope.core.message.Msg>

public class BaseScheduleAgentTask extends Object implements ScheduleAgentTask<io.agentscope.core.message.Msg>
Default implementation of ScheduleAgentTask.

This class represents a scheduled agent task that manages the lifecycle and execution of agents according to a schedule. It uses AgentConfig to dynamically create fresh Agent instances for each execution, providing better state isolation and resource management.

Key Features:

  • Dynamic Agent Creation - Creates fresh Agent instances for each execution
  • State Isolation - Each execution gets a clean Agent state
  • Task Control - Provides methods to run and cancel the scheduled task
  • Status Monitoring - Access to execution metrics and scheduling information
  • Thread Safety - Safe for concurrent access from multiple threads

Usage Example:


 // Create agent configuration
 AgentConfig agentConfig = AgentConfig.builder()
     .name("MyAgent")
     .model(model)
     .toolkit(toolkit)
     .build();

 // Schedule the agent
 ScheduleConfig scheduleConfig = ScheduleConfig.builder()
     .cron("0 0 8 * * ?")
     .build();
 ScheduleAgentTask task = scheduler.schedule(agentConfig, scheduleConfig);

 // Control the task
 String taskId = task.getId();
 String taskName = task.getName();

 // Manually trigger execution
 task.run();

 // Cancel the task
 task.cancel();

 // Query execution info (if cast to BaseScheduleAgentTask)
 if (task instanceof BaseScheduleAgentTask baseTask) {
     boolean cancelled = baseTask.isCancelled();
     long count = baseTask.getExecutionCount();
 }
 

Thread Safety: This class is designed to be thread-safe as scheduling operations may be triggered from different threads. Each execution creates a new Agent instance, ensuring thread-safe execution.

See Also:
  • Constructor Details

    • BaseScheduleAgentTask

      public BaseScheduleAgentTask(RuntimeAgentConfig agentConfig, ScheduleConfig scheduleConfig, AgentScheduler scheduler)
      Constructor for BaseScheduleAgentTask.
      Parameters:
      agentConfig - The runtime agent configuration containing model, toolkit, and hooks
      scheduleConfig - The schedule configuration defining timing and execution policies
      scheduler - The scheduler managing this task
  • Method Details

    • getId

      public String getId()
      Get the unique identifier of this scheduled task.

      The ID is automatically generated as a UUID when the task is created.

      Specified by:
      getId in interface ScheduleAgentTask<io.agentscope.core.message.Msg>
      Returns:
      The unique task identifier
    • getName

      public String getName()
      Get the name of this scheduled task.

      The name is derived from the agent configuration.

      Specified by:
      getName in interface ScheduleAgentTask<io.agentscope.core.message.Msg>
      Returns:
      The task name
    • run

      public reactor.core.publisher.Mono<io.agentscope.core.message.Msg> run(io.agentscope.core.message.Msg... msgs)
      Execute the scheduled task immediately.

      This method creates a fresh Agent instance and executes it with the provided input messages. Each execution increments the execution count for tracking purposes.

      Specified by:
      run in interface ScheduleAgentTask<io.agentscope.core.message.Msg>
      Parameters:
      msgs - The input messages to pass to the agent (optional, can be empty or null)
      Returns:
      A Mono that emits the agent's response message when execution completes
      Throws:
      IllegalStateException - if the task has been cancelled
    • cancel

      public void cancel()
      Cancel this scheduled task permanently.

      This method marks the task as cancelled and delegates to the scheduler to remove the task from its schedule. After cancellation, calling run(Msg...) will throw an IllegalStateException.

      Specified by:
      cancel in interface ScheduleAgentTask<io.agentscope.core.message.Msg>
    • isCancelled

      public boolean isCancelled()
      Check if this task has been cancelled.

      A cancelled task will not be executed by the scheduler and cannot be manually run.

      Returns:
      true if the task is cancelled, false otherwise
    • getScheduleConfig

      public ScheduleConfig getScheduleConfig()
      Get the schedule configuration used for this agent.

      Returns the configuration that was provided when this agent was scheduled, including timing settings, execution policies, and other scheduling parameters.

      Returns:
      The ScheduleConfig for this scheduled agent
    • getAgentConfig

      public RuntimeAgentConfig getAgentConfig()
      Get the runtime agent configuration.

      Returns the RuntimeAgentConfig containing all configuration including runtime objects (Model, Toolkit, Hooks) used to create agent instances for this task.

      Returns:
      The RuntimeAgentConfig instance
    • getExecutionCount

      public long getExecutionCount()
      Get the total number of times this task has been executed.

      This count includes both scheduled executions and manual executions via run(Msg...). It tracks successful executions; if execution fails with an exception, the count is still incremented.

      Returns:
      The total execution count (starts at 0)
    • incrementExecutionCount

      public void incrementExecutionCount()
      Increment the execution count.

      This method is called internally after each execution and is intended for use by the scheduler implementation or subclasses.