Add scheduled jobs, fix spawning non-default jobs

This commit is contained in:
asonix 2018-12-13 11:08:28 -06:00
parent 8a78f9e129
commit 6e79341b38
No known key found for this signature in database
GPG key ID: 6986797E36BFA1D4
7 changed files with 23 additions and 8 deletions

View file

@ -35,7 +35,7 @@ fn main() {
tokio::run(lazy(move || {
for job in jobs {
tokio::spawn(spawner.queue::<MyProcessor>(job).map_err(|_| ()));
tokio::spawn(spawner.queue::<MyProcessor, _>(job).map_err(|_| ()));
}
Ok(())

View file

@ -34,7 +34,7 @@ fn main() -> Result<(), Error> {
let spawner = SpawnerConfig::new("localhost", 5555);
for job in jobs {
spawner.queue_sync::<MyProcessor>(job)?;
spawner.queue_sync::<MyProcessor, _>(job)?;
}
Ok(())

View file

@ -1,7 +1,7 @@
[package]
name = "background-jobs-core"
description = "Core types for implementing an asynchronous jobs processor on tokio"
version = "0.3.1"
version = "0.3.2"
license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/asonix/background-jobs"

View file

@ -128,6 +128,10 @@ impl JobInfo {
self.next_queue = Some(next_queue);
}
pub(crate) fn schedule(&mut self, time: DateTime<Utc>) {
self.next_queue = Some(time);
}
pub(crate) fn is_stale(&self) -> bool {
self.updated_at < Utc::now() - OldDuration::days(1)
}

View file

@ -17,6 +17,7 @@
* along with Background Jobs. If not, see <http://www.gnu.org/licenses/>.
*/
use chrono::{offset::Utc, DateTime};
use failure::Error;
use futures::{
future::{Either, IntoFuture},
@ -127,6 +128,14 @@ where
Ok(job)
}
/// Create a JobInfo to schedule a job to be performed after a certain time
fn new_scheduled_job(job: Self::Job, after: DateTime<Utc>) -> Result<JobInfo, Error> {
let mut job = Self::new_job(job)?;
job.schedule(after);
Ok(job)
}
/// A provided method to coerce arguments into the expected type and run the job
///
/// Advanced users may want to override this method in order to provide their own custom

View file

@ -1,7 +1,7 @@
[package]
name = "background-jobs-server"
description = "Jobs processor server based on ZeroMQ"
version = "0.3.0"
version = "0.3.1"
license = "GPL-3.0"
authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/asonix/background-jobs"

View file

@ -74,9 +74,10 @@ impl SpawnerConfig {
}
/// Queue a job to be executed in the background
pub fn queue<P>(&self, job: P::Job) -> impl Future<Item = (), Error = Error>
pub fn queue<P, S>(&self, job: P::Job) -> impl Future<Item = (), Error = Error>
where
P: Processor,
P: Processor<S>,
S: Clone + Send + Sync + 'static,
{
let msg = P::new_job(job)
.map_err(Error::from)
@ -105,9 +106,10 @@ impl SpawnerConfig {
/// sending the message to the jobs server.
///
/// If you have a tokio-based application, you should use `queue` instead.
pub fn queue_sync<P>(&self, job: P::Job) -> Result<(), Error>
pub fn queue_sync<P, S>(&self, job: P::Job) -> Result<(), Error>
where
P: Processor,
P: Processor<S>,
S: Clone + Send + Sync + 'static,
{
use zmq::PUSH;