diff --git a/src/lib.rs b/src/lib.rs index 2e97ae2..24d2f34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -225,22 +225,30 @@ pub use traits::{Scheduler, Step}; pub use worker::Worker; use chrono::{DateTime, Utc}; -use sqlx::{types::Uuid, PgPool}; +use sqlx::{types::Uuid, PgExecutor}; use std::time::Duration; const LOST_CONNECTION_SLEEP: Duration = Duration::from_secs(1); /// Enqueues the task to be run immediately -pub async fn enqueue(db: &PgPool, task: &impl Scheduler) -> Result { +pub async fn enqueue<'e>(db: impl PgExecutor<'e>, task: &impl Scheduler) -> Result { task.enqueue(db).await } /// Schedules a task to be run after a specified delay -pub async fn delay(db: &PgPool, task: &impl Scheduler, delay: Duration) -> Result { +pub async fn delay<'e>( + db: impl PgExecutor<'e>, + task: &impl Scheduler, + delay: Duration, +) -> Result { task.delay(db, delay).await } /// Schedules a task to run at a specified time in the future -pub async fn schedule(db: &PgPool, task: &impl Scheduler, at: DateTime) -> Result { +pub async fn schedule<'e>( + db: impl PgExecutor<'e>, + task: &impl Scheduler, + at: DateTime, +) -> Result { task.schedule(db, at).await } diff --git a/src/traits.rs b/src/traits.rs index d904bf4..e782a5c 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -2,7 +2,7 @@ use crate::{Error, StepResult}; use async_trait::async_trait; use chrono::{DateTime, Utc}; use serde::{de::DeserializeOwned, Serialize}; -use sqlx::{types::Uuid, PgPool}; +use sqlx::{types::Uuid, PgExecutor, PgPool}; use std::{fmt, time::Duration}; /// A tait to implement on each task step @@ -36,19 +36,23 @@ where #[async_trait] pub trait Scheduler: fmt::Debug + DeserializeOwned + Serialize + Sized + Sync { /// Enqueues the task to be run immediately - async fn enqueue(&self, db: &PgPool) -> crate::Result { + async fn enqueue<'e>(&self, db: impl PgExecutor<'e>) -> crate::Result { self.schedule(db, Utc::now()).await } /// Schedules a task to be run after a specified delay - async fn delay(&self, db: &PgPool, delay: Duration) -> crate::Result { + async fn delay<'e>(&self, db: impl PgExecutor<'e>, delay: Duration) -> crate::Result { let delay = chrono::Duration::from_std(delay).unwrap_or_else(|_| chrono::Duration::max_value()); self.schedule(db, Utc::now() + delay).await } /// Schedules a task to run at a specified time in the future - async fn schedule(&self, db: &PgPool, at: DateTime) -> crate::Result { + async fn schedule<'e>( + &self, + db: impl PgExecutor<'e>, + at: DateTime, + ) -> crate::Result { let step = serde_json::to_string(self) .map_err(|e| Error::SerializeStep(e, format!("{self:?}")))?; sqlx::query!(