-
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
9 changed files
with
140 additions
and
47 deletions.
There are no files selected for viewing
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,53 @@ | ||
use async_trait::async_trait; | ||
use pg_task::{NextStep, Step, StepResult}; | ||
use serde::{Deserialize, Serialize}; | ||
use sqlx::PgPool; | ||
use std::{env, time::Duration}; | ||
|
||
// It wraps the task step into an enum which proxies necessary methods | ||
pg_task::task!(FooBar { Foo, Bar }); | ||
|
||
// Also we need a enum representing all the possible tasks | ||
pg_task::scheduler!(Tasks { FooBar }); | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
let db = connect().await?; | ||
|
||
// Let's schedule a few tasks | ||
for delay in [3, 1, 2] { | ||
pg_task::enqueue(&db, &Tasks::FooBar(Foo(delay).into())).await?; | ||
} | ||
|
||
// And run a worker | ||
pg_task::Worker::<Tasks>::new(db).run().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct Foo(u64); | ||
#[async_trait] | ||
impl Step<FooBar> for Foo { | ||
async fn step(self, _db: &PgPool) -> StepResult<FooBar> { | ||
println!("Sleeping for {} sec", self.0); | ||
NextStep::delay(Bar(self.0), Duration::from_secs(self.0)) | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct Bar(u64); | ||
#[async_trait] | ||
impl Step<FooBar> for Bar { | ||
async fn step(self, _db: &PgPool) -> StepResult<FooBar> { | ||
println!("Woke up after {} sec", self.0); | ||
NextStep::none() | ||
} | ||
} | ||
|
||
async fn connect() -> anyhow::Result<sqlx::PgPool> { | ||
dotenv::dotenv().ok(); | ||
let db = PgPool::connect(&env::var("DATABASE_URL")?).await?; | ||
sqlx::migrate!().run(&db).await?; | ||
Ok(db) | ||
} |
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
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,29 @@ | ||
use crate::StepResult; | ||
use std::time::Duration; | ||
|
||
/// Represents next step of the task | ||
pub enum NextStep<T> { | ||
/// The task is done | ||
None, | ||
/// Run the next step immediately | ||
Now(T), | ||
/// Delay the next step | ||
Delayed(T, Duration), | ||
} | ||
|
||
impl<T> NextStep<T> { | ||
/// The task is done | ||
pub fn none() -> StepResult<T> { | ||
Ok(Self::None) | ||
} | ||
|
||
/// Run the next step immediately | ||
pub fn now(step: impl Into<T>) -> StepResult<T> { | ||
Ok(Self::Now(step.into())) | ||
} | ||
|
||
/// Delay the next step | ||
pub fn delay(step: impl Into<T>, delay: Duration) -> StepResult<T> { | ||
Ok(Self::Delayed(step.into(), delay)) | ||
} | ||
} |
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,11 @@ | ||
/// Converts a chrono duration to std, it uses absolute value of the chrono duration | ||
pub fn chrono_duration_to_std(chrono_duration: chrono::Duration) -> std::time::Duration { | ||
let seconds = chrono_duration.num_seconds(); | ||
let nanos = chrono_duration.num_nanoseconds().unwrap_or(0) % 1_000_000_000; | ||
std::time::Duration::new(seconds.unsigned_abs(), nanos.unsigned_abs() as u32) | ||
} | ||
|
||
/// Converts a std duration to chrono | ||
pub fn std_duration_to_chrono(std_duration: std::time::Duration) -> chrono::Duration { | ||
chrono::Duration::from_std(std_duration).unwrap_or_else(|_| chrono::Duration::max_value()) | ||
} |
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