From f1c4709e41412d6defac02699924772cb9f56b46 Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 21 Apr 2020 12:04:18 -0500 Subject: [PATCH] Expose schedule in queuehandle, expose dev types in main lib --- Cargo.toml | 4 ++-- jobs-actix/Cargo.toml | 2 +- jobs-actix/src/lib.rs | 21 ++++++++++++++++++++- src/lib.rs | 8 ++++++++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d43472b..c269e9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "background-jobs" description = "Background Jobs implemented with actix and futures" -version = "0.8.0-alpha.1" +version = "0.8.0-alpha.2" license-file = "LICENSE" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/background-jobs" @@ -25,6 +25,6 @@ version = "0.8.0-alpha.0" path = "jobs-core" [dependencies.background-jobs-actix] -version = "0.8.0-alpha.0" +version = "0.8.0-alpha.1" path = "jobs-actix" optional = true diff --git a/jobs-actix/Cargo.toml b/jobs-actix/Cargo.toml index fbac10c..3242d5a 100644 --- a/jobs-actix/Cargo.toml +++ b/jobs-actix/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "background-jobs-actix" description = "in-process jobs processor based on Actix" -version = "0.8.0-alpha.0" +version = "0.8.0-alpha.1" license-file = "../LICENSE" authors = ["asonix "] repository = "https://git.asonix.dog/Aardwolf/background-jobs" diff --git a/jobs-actix/src/lib.rs b/jobs-actix/src/lib.rs index 4aa54b4..b12bf9c 100644 --- a/jobs-actix/src/lib.rs +++ b/jobs-actix/src/lib.rs @@ -119,7 +119,8 @@ use actix::Arbiter; use anyhow::Error; -use background_jobs_core::{new_job, Job, ProcessorMap, Stats, Storage}; +use background_jobs_core::{new_job, new_scheduled_job, Job, ProcessorMap, Stats, Storage}; +use chrono::{DateTime, Utc}; use log::error; use std::{collections::BTreeMap, sync::Arc, time::Duration}; @@ -260,6 +261,24 @@ impl QueueHandle { Ok(()) } + /// Schedule a job for execution later + /// + /// This job will be sent to the server for storage, and will execute after the specified time + /// and when a worker for the job's queue is free to do so. + pub fn schedule(&self, job: J, after: DateTime) -> Result<(), Error> + where + J: Job, + { + let job = new_scheduled_job(job, after)?; + let server = self.inner.clone(); + actix::spawn(async move { + if let Err(e) = server.new_job(job).await { + error!("Error creating job, {}", e); + } + }); + Ok(()) + } + /// Queues a job for recurring execution /// /// This job will be added to it's queue on the server once every `Duration`. It will be diff --git a/src/lib.rs b/src/lib.rs index 3416fde..be302c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,5 +163,13 @@ pub use background_jobs_core::{memory_storage, Backoff, Job, JobStat, MaxRetries, Stats}; +pub mod dev { + //! Useful types and methods for developing Storage and Processor implementations. + pub use background_jobs_core::{ + new_job, new_scheduled_job, process, CachedProcessorMap, JobInfo, NewJobInfo, ProcessorMap, + ReturnJobInfo, Storage, + }; +} + #[cfg(feature = "background-jobs-actix")] pub use background_jobs_actix::{create_server, ActixJob, QueueHandle, WorkerConfig};