-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtutorial.rs
More file actions
60 lines (50 loc) · 1.33 KB
/
tutorial.rs
File metadata and controls
60 lines (50 loc) · 1.33 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
use async_trait::async_trait;
use pg_task::{NextStep, Step, StepResult};
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
mod util;
// Creates an enum `Greeter` containing our task steps
pg_task::task!(Greeter { ReadName, SayHello });
// Creates an enum `Tasks` representing all the possible tasks
pg_task::scheduler!(Tasks { Greeter });
#[derive(Debug, Deserialize, Serialize)]
pub struct ReadName {
filename: String,
}
#[async_trait]
impl Step<Greeter> for ReadName {
const RETRY_LIMIT: i32 = 5;
async fn step(self, _db: &PgPool) -> StepResult<Greeter> {
let name = std::fs::read_to_string(self.filename)?;
NextStep::now(SayHello { name })
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SayHello {
name: String,
}
#[async_trait]
impl Step<Greeter> for SayHello {
async fn step(self, _db: &PgPool) -> StepResult<Greeter> {
println!("Hello, {}", self.name);
NextStep::none()
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let db = util::init().await?;
// Let's schedule the task
pg_task::enqueue(
&db,
&Tasks::Greeter(
ReadName {
filename: "name.txt".into(),
}
.into(),
),
)
.await?;
// And run a worker
pg_task::Worker::<Tasks>::new(db).run().await?;
Ok(())
}