Browse Source

Add Every, a tool to create recurring jobs

tags/background-jobs-actix-v0.6.0
Arlo (Hyena) 4 months ago
parent
commit
ffa61b3c33
3 changed files with 58 additions and 2 deletions
  1. 54
    0
      jobs-actix/src/every.rs
  2. 3
    1
      jobs-actix/src/lib.rs
  3. 1
    1
      src/lib.rs

+ 54
- 0
jobs-actix/src/every.rs View File

@@ -0,0 +1,54 @@
use std::time::Duration;

use super::{Job, QueueHandle};
use actix::{Actor, AsyncContext, Context};
use log::error;

/// A type used to schedule recurring jobs.
///
/// ```rust,ignore
/// let server = ServerConfig::new(storage).start();
/// Every::new(server, Duration::from_secs(60 * 30), MyJob::new()).start();
/// ```
pub struct Every<J>
where
J: Job + Clone + 'static,
{
spawner: QueueHandle,
duration: Duration,
job: J,
}

impl<J> Every<J>
where
J: Job + Clone + 'static,
{
pub fn new(spawner: QueueHandle, duration: Duration, job: J) -> Self {
Every {
spawner,
duration,
job,
}
}
}

impl<J> Actor for Every<J>
where
J: Job + Clone + 'static,
{
type Context = Context<Self>;

fn started(&mut self, ctx: &mut Self::Context) {
match self.spawner.queue(self.job.clone()) {
Ok(_) => (),
Err(_) => error!("Failed to queue job"),
};

ctx.run_interval(self.duration.clone(), move |actor, _| {
match actor.spawner.queue(actor.job.clone()) {
Ok(_) => (),
Err(_) => error!("Failed to queue job"),
}
});
}
}

+ 3
- 1
jobs-actix/src/lib.rs View File

@@ -5,11 +5,13 @@ use background_jobs_core::{Job, Processor, ProcessorMap, Stats, Storage};
use failure::Error;
use futures::Future;

mod every;
mod pinger;
mod server;
mod storage;
mod worker;
pub use self::{server::Server, worker::LocalWorker};

pub use self::{every::Every, server::Server, worker::LocalWorker};

use self::{
pinger::Pinger,

+ 1
- 1
src/lib.rs View File

@@ -202,7 +202,7 @@ pub use background_jobs_core::{
};

#[cfg(feature = "background-jobs-actix")]
pub use background_jobs_actix::{QueueHandle, ServerConfig, WorkerConfig};
pub use background_jobs_actix::{Every, QueueHandle, ServerConfig, WorkerConfig};

#[cfg(feature = "background-jobs-sled-storage")]
pub mod sled_storage {

Loading…
Cancel
Save