Recurring Tasks
Recurring Tasks
Recurring task workflows execute operations on a schedule — daily reports, weekly cleanups, hourly health checks — while maintaining state across iterations. Unlike external cron schedulers, the workflow itself owns the schedule, making it easy to add conditional logic, skip iterations, or gracefully shut down.
Start Loop
iteration = 0
loop
Execute Task
e.g. generate_report()
Should continue?
sleep(interval)
↻
Complete
max iterations or cancelled
1use orcher::prelude::*;2use anyhow::Result;3use std::time::Duration;45#[derive(Debug, Clone, Serialize, Deserialize)]6struct RecurringTaskConfig {7 task_name: String,8 interval_hours: u64,9 max_iterations: u32,10}1112#[derive(Debug, Clone, Serialize, Deserialize)]13struct RecurringTaskResult {14 task_name: String,15 iterations_completed: u32,16 total_hours: u64,17}1819#[task]20async fn execute_recurring_task(task_name: String) -> Result<()> {21 // Execute the recurring task22 Ok(())23}2425#[task]26async fn generate_report(report_type: String) -> Result<()> { Ok(()) }2728#[task]29async fn email_report(report_type: String) -> Result<()> { Ok(()) }3031#[task]32async fn check_report_active(report_type: String) -> Result<bool> { Ok(true) }3334// Recurring task workflow35#[workflow(name = "recurring_task", version = "1.0.0")]36pub async fn recurring_task(37 ctx: WorkflowContext,38 config: RecurringTaskConfig,39) -> Result<RecurringTaskResult> {40 println!("Starting recurring task: {} every {} hours",41 config.task_name, config.interval_hours);4243 let interval = Duration::from_secs(config.interval_hours * 3600);44 let mut iteration = 0;4546 loop {47 // Execute the task48 println!("Executing {} (iteration {})", config.task_name, iteration + 1);49 ctx.execute_task(execute_recurring_task, config.task_name.clone()).await?;5051 iteration += 1;5253 // Check if we've reached max iterations54 if iteration >= config.max_iterations {55 println!("Reached max iterations ({}) - stopping", config.max_iterations);56 break;57 }5859 // Wait until next execution60 println!("Waiting {} hours until next execution", config.interval_hours);61 ctx.sleep(interval).await?;62 }6364 Ok(RecurringTaskResult {65 task_name: config.task_name,66 iterations_completed: iteration,67 total_hours: config.interval_hours * iteration as u64,68 })69}7071// Daily report generation72#[workflow(name = "daily_report", version = "1.0.0")]73pub async fn daily_report(74 ctx: WorkflowContext,75 report_type: String,76) -> Result<()> {77 loop {78 // Generate report79 println!("Generating daily {} report", report_type);80 ctx.execute_task(generate_report, report_type.clone()).await?;81 ctx.execute_task(email_report, report_type.clone()).await?;8283 // Wait 24 hours until next report84 ctx.sleep(Duration::from_secs(86400)).await?;8586 // Check if report should continue87 let should_continue: bool = ctx88 .execute_task(check_report_active, report_type.clone())89 .await?;9091 if !should_continue {92 println!("Report {} has been disabled - stopping", report_type);93 break;94 }95 }9697 Ok(())98}