Guides

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
1
use orcher::prelude::*;
2
use anyhow::Result;
3
use std::time::Duration;
4
5
#[derive(Debug, Clone, Serialize, Deserialize)]
6
struct RecurringTaskConfig {
7
task_name: String,
8
interval_hours: u64,
9
max_iterations: u32,
10
}
11
12
#[derive(Debug, Clone, Serialize, Deserialize)]
13
struct RecurringTaskResult {
14
task_name: String,
15
iterations_completed: u32,
16
total_hours: u64,
17
}
18
19
#[task]
20
async fn execute_recurring_task(task_name: String) -> Result<()> {
21
// Execute the recurring task
22
Ok(())
23
}
24
25
#[task]
26
async fn generate_report(report_type: String) -> Result<()> { Ok(()) }
27
28
#[task]
29
async fn email_report(report_type: String) -> Result<()> { Ok(()) }
30
31
#[task]
32
async fn check_report_active(report_type: String) -> Result<bool> { Ok(true) }
33
34
// Recurring task workflow
35
#[workflow(name = "recurring_task", version = "1.0.0")]
36
pub 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);
42
43
let interval = Duration::from_secs(config.interval_hours * 3600);
44
let mut iteration = 0;
45
46
loop {
47
// Execute the task
48
println!("Executing {} (iteration {})", config.task_name, iteration + 1);
49
ctx.execute_task(execute_recurring_task, config.task_name.clone()).await?;
50
51
iteration += 1;
52
53
// Check if we've reached max iterations
54
if iteration >= config.max_iterations {
55
println!("Reached max iterations ({}) - stopping", config.max_iterations);
56
break;
57
}
58
59
// Wait until next execution
60
println!("Waiting {} hours until next execution", config.interval_hours);
61
ctx.sleep(interval).await?;
62
}
63
64
Ok(RecurringTaskResult {
65
task_name: config.task_name,
66
iterations_completed: iteration,
67
total_hours: config.interval_hours * iteration as u64,
68
})
69
}
70
71
// Daily report generation
72
#[workflow(name = "daily_report", version = "1.0.0")]
73
pub async fn daily_report(
74
ctx: WorkflowContext,
75
report_type: String,
76
) -> Result<()> {
77
loop {
78
// Generate report
79
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?;
82
83
// Wait 24 hours until next report
84
ctx.sleep(Duration::from_secs(86400)).await?;
85
86
// Check if report should continue
87
let should_continue: bool = ctx
88
.execute_task(check_report_active, report_type.clone())
89
.await?;
90
91
if !should_continue {
92
println!("Report {} has been disabled - stopping", report_type);
93
break;
94
}
95
}
96
97
Ok(())
98
}

Next Steps