-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple_example.rs
48 lines (43 loc) · 1.5 KB
/
simple_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
46
47
48
use log::info;
use simple_logger::SimpleLogger;
use tasklet::task::TaskStepStatusOk::Success;
use tasklet::{TaskBuilder, TaskScheduler};
/// An example of a `TaskScheduler` instance with 2 `Task` instances.
#[tokio::main]
async fn main() {
// Initialize the logger.
SimpleLogger::new().init().unwrap();
// Just a passed variable.
let mut count: usize = 5;
// Create a scheduler instance.
let mut scheduler = TaskScheduler::new(500, chrono::Utc);
// Add two tasks, the first one will die after 5 executions,
// the second will run forever.
// The first task will execute at seconds 1, 10, 20 of a minute,
// and the second at the second 30 of each minute.
scheduler
.add_task(
TaskBuilder::new(chrono::Utc)
.every("1, 10, 20 * * * * * *")
.description("Just some task")
.repeat(5)
.add_step_default(move || {
count = count - 1;
info!("I have {} more executions left!", count);
Ok(Success)
})
.build(),
)
.add_task(
TaskBuilder::new(chrono::Utc)
.every("1, 10 , 20 * * * * * *")
.description("Just another task")
.add_step_default(|| {
info!("I will run forever!");
Ok(Success)
})
.build(),
);
// Execute the tasks in the queue.
scheduler.run().await;
}