-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
470 additions
and
4 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...9b01f4fd7a74b7f2edbef4982c3d4d9a72d8.json → ...3f3672bd5e1d5639fc2b06686bdbca16aa2d.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use async_trait::async_trait; | ||
use pg_task::{NextStep, Step, StepResult}; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::PgPool; | ||
|
||
mod util; | ||
|
||
// Creates a enum `Greeter` containing our task steps | ||
pg_task::task!(Greeter { ReadName, SayHello }); | ||
|
||
// Creates a 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(()) | ||
} |
Oops, something went wrong.