Scheduler

Schedule periodic agent and recipe execution in TypeScript

Schedule agents and recipes to run periodically.

Diagram

Scheduler

Agent

Scheduled Job

Schedule agents and recipes to run periodically. Perfect for monitoring tasks, periodic reports, and automated workflows.

Quick Start

  1. Simple Usage

    Scheduling a Recipe

    typescript
    import { recipe } from 'praisonai-ts';
    
    // Schedule a recipe
    const scheduler = await recipe.schedule('my-recipe', {
      interval: 'hourly',
      maxRetries: 3,
      timeoutSec: 300,
      maxCostUsd: 1.00,
      runImmediately: true,
    });
    
    // Start the scheduler
    scheduler.start();
    
    // Get statistics
    const stats = scheduler.getStats();
    console.log(`Executions: ${stats.totalExecutions}`);
    console.log(`Success rate: ${stats.successRate}%`);
    
    // Stop when done
    scheduler.stop();

    Scheduler Interface

    typescript
    interface RecipeScheduler {
      recipeName: string;
      interval: string;
      isRunning: boolean;
    
      start(): void;
      stop(): void;
      getStats(): SchedulerStats;
    }
    
    interface SchedulerStats {
      totalExecutions: number;
      successfulExecutions: number;
      failedExecutions: number;
      successRate: number;
      totalCost: number;
      lastExecutionAt?: Date;
      nextExecutionAt?: Date;
    }

Using AgentScheduler Directly

typescript
import { Agent, AgentScheduler } from 'praisonai-ts';

// Create agent
const agent = new Agent({
  name: 'News Monitor',
  instructions: 'Monitor and summarize AI news',
});

// Create scheduler
const scheduler = new AgentScheduler({
  agent,
  task: 'Check for latest AI news',
  timeout: 300,
  maxCost: 1.00,
});

// Start with interval
scheduler.start({
  scheduleExpr: 'hourly',
  maxRetries: 3,
  runImmediately: true,
});

// Monitor
console.log(`Running: ${scheduler.isRunning}`);
console.log(`Stats: ${JSON.stringify(scheduler.getStats())}`);

// Stop
scheduler.stop();

Interval Formats

FormatDescription
hourlyEvery hour
dailyEvery day
*/30mEvery 30 minutes
*/6hEvery 6 hours
3600Every 3600 seconds

Configuration

TEMPLATE.yaml Runtime Block

yaml
runtime:
  schedule:
    enabled: true
    interval: "hourly"
    max_retries: 3
    run_immediately: false
    timeout_sec: 300
    max_cost_usd: 1.00

Safe Defaults

SettingDefaultDescription
intervalhourlyExecution interval
maxRetries3Retry attempts on failure
timeoutSec300Timeout per execution
maxCostUsd1.00Budget limit

Error Handling

typescript
import { recipe, SchedulerError } from 'praisonai-ts';

try {
  const scheduler = await recipe.schedule('my-recipe', {
    interval: 'hourly',
    maxCostUsd: 0.50,
  });

  scheduler.start();

  // Monitor for errors
  scheduler.on('error', (error) => {
    console.error(`Execution failed: ${error.message}`);
  });

  scheduler.on('budgetExceeded', () => {
    console.log('Budget limit reached, stopping');
    scheduler.stop();
  });

} catch (error) {
  if (error instanceof SchedulerError) {
    console.error(`Scheduler error: ${error.message}`);
  }
}

Best Practices

Set budget limits

Prevent runaway costs

Use appropriate intervals

Match task requirements

Handle failures

Implement retry logic

Monitor statistics

Track success rates

Clean shutdown

Always call stop() when done

Related