nativedb/
nativedb.rs

1use std::time::Duration;
2
3use polly_scheduler::{
4    core::{
5        context::TaskContext,
6        store::TaskStore,
7        task::{Task, TaskFuture},
8        task_kind::TaskKind,
9    },
10    nativedb::meta::NativeDbTaskStore,
11};
12use serde::{Deserialize, Serialize};
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();
27
28    context
29        .add_task(MyTask1::new("name1".to_string(), 32))
30        .await
31        .unwrap();
32
33    context
34        .add_task(MyTask2::new("namexxxxxxx".to_string(), 3900))
35        .await
36        .unwrap();
37
38    tokio::time::sleep(Duration::from_secs(100000000)).await;
39}
40
41#[derive(Clone, Debug, Deserialize, Serialize)]
42struct MyTask1 {
43    pub name: String,
44    pub age: i32,
45}
46
47impl MyTask1 {
48    pub fn new(name: String, age: i32) -> Self {
49        Self { name, age }
50    }
51}
52
53impl Task for MyTask1 {
54    const TASK_KEY: &'static str = "my_task_a";
55
56    const TASK_QUEUE: &'static str = "default";
57
58    const TASK_KIND: TaskKind = TaskKind::Once;
59    //const RETRY_POLICY: RetryPolicy = RetryPolicy::linear(10, Some(5));
60
61    fn run(self) -> TaskFuture {
62        Box::pin(async move {
63            println!("{}", self.name);
64            println!("{}", self.age);
65
66            println!("my task1 is running");
67            Err("return error".to_string())
68        })
69    }
70}
71
72#[derive(Clone, Debug, Deserialize, Serialize)]
73struct MyTask2 {
74    pub name: String,
75    pub age: i32,
76}
77
78impl MyTask2 {
79    pub fn new(name: String, age: i32) -> Self {
80        Self { name, age }
81    }
82}
83
84impl Task for MyTask2 {
85    const TASK_KEY: &'static str = "my_task_c";
86    const TASK_QUEUE: &'static str = "default";
87    const TASK_KIND: TaskKind = TaskKind::Cron;
88    const REPEAT_INTERVAL: Option<u32> = Some(2);
89    const SCHEDULE: Option<&'static str> = Some("1/2 * * * * *");
90    const TIMEZONE: Option<&'static str> = Some("Asia/Shanghai");
91
92    fn run(self) -> TaskFuture {
93        Box::pin(async move {
94            println!("{}", self.name);
95            println!("{}", self.age);
96            //tokio::time::sleep(Duration::from_secs(100000)).await;
97            println!("my_task_c is running");
98            Ok(())
99        })
100    }
101}