1use persistent_scheduler::{
2 core::{
3 context::{TaskConfiguration, TaskContext},
4 retry::{RetryPolicy, RetryStrategy},
5 store::TaskStore,
6 task::{Task, TaskFuture},
7 task_kind::TaskKind,
8 },
9 nativedb::meta::NativeDbTaskStore,
10};
11use serde::{Deserialize, Serialize};
12use std::{sync::Arc, time::Duration};
13
14#[tokio::main]
15async fn main() {
16 tracing_subscriber::fmt()
17 .with_max_level(tracing::Level::INFO)
18 .init();
19
20 let task_store = NativeDbTaskStore::default();
21 task_store.restore_tasks().await.unwrap();
22 let context = TaskContext::new(task_store)
23 .register::<MyTask1>()
24 .register::<MyTask2>()
25 .set_concurrency("default", 10)
26 .start_with_cleaner()
27 .await;
28 let context = Arc::new(context);
29 let mut tasks = Vec::new();
30
31 for _ in 0..10000 {
32 tasks.push(TaskConfiguration {
33 inner: MyTask1::new("name1".to_string(), 32),
34 kind: TaskKind::Once,
35 delay_seconds: None,
36 });
37 }
38
39 tokio::spawn(async move {
40 context.add_tasks(tasks).await.unwrap();
41 });
42
43 tokio::time::sleep(Duration::from_secs(10000)).await;
44}
45
46#[derive(Clone, Debug, Deserialize, Serialize)]
47struct MyTask1 {
48 pub name: String,
49 pub age: i32,
50}
51
52impl MyTask1 {
53 pub fn new(name: String, age: i32) -> Self {
54 Self { name, age }
55 }
56}
57
58impl Task for MyTask1 {
59 const TASK_KEY: &'static str = "my_task_a";
60
61 const TASK_QUEUE: &'static str = "default";
62
63 fn delay_seconds(&self) -> u32 {
64 1
65 }
66
67 fn retry_policy(&self) -> RetryPolicy {
68 RetryPolicy {
69 strategy: RetryStrategy::Linear { interval: 2 },
70 max_retries: Some(3),
71 }
72 }
73
74 fn run(self) -> TaskFuture {
75 Box::pin(async move {
76 Ok(())
81 })
82 }
83}
84
85#[derive(Clone, Debug, Deserialize, Serialize)]
86struct MyTask2 {
87 pub name: String,
88 pub age: i32,
89}
90
91impl MyTask2 {
92 pub fn new(name: String, age: i32) -> Self {
93 Self { name, age }
94 }
95}
96
97impl Task for MyTask2 {
98 const TASK_KEY: &'static str = "my_task_c";
99 const TASK_QUEUE: &'static str = "default";
100
101 fn run(self) -> TaskFuture {
102 Box::pin(async move {
103 println!("{}", self.name);
104 println!("{}", self.age);
105 println!("my_task_c is running");
107 Ok(())
108 })
109 }
110}