-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathone_task_example.rs
45 lines (40 loc) · 1.49 KB
/
one_task_example.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use log::info;
use simple_logger::SimpleLogger;
use tasklet::task::TaskStepStatusErr::Error;
use tasklet::task::TaskStepStatusOk::Success;
use tasklet::{TaskBuilder, TaskScheduler};
/// A simple example of a task with two steps,
/// that might work or fail depending on the execution configuration.
#[tokio::main]
async fn main() {
// Init the logger.
SimpleLogger::new().init().unwrap();
// A variable to be passed in the task.
let mut exec_count = 0;
// Task scheduler with 2000ms loop frequency.
let mut scheduler = TaskScheduler::new(2000, chrono::Local);
// Create a task with 2 steps and add it to the scheduler.
// The second step fails every second execution.
// Append the task to the scheduler.
scheduler.add_task(
TaskBuilder::new(chrono::Local)
.every("1 * * * * * *")
.description("A simple task")
.add_step_default(|| {
info!("Hello from step 1");
Ok(Success) // Let the scheduler know this step was a success.
})
.add_step_default(move || {
if exec_count % 2 == 0 {
exec_count += 1;
return Err(Error); // Indicate that this step was a fail.
}
info!("Hello from step 2");
exec_count += 1;
Ok(Success) // Indicate that this step was a success.
})
.build(),
);
// Execute the scheduler.
scheduler.run().await;
}