2018-11-18 01:39:04 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Background Jobs.
|
|
|
|
*
|
|
|
|
* Copyright © 2018 Riley Trautman
|
|
|
|
*
|
|
|
|
* Background Jobs is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Background Jobs is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Background Jobs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use futures::future::{Either, Future, IntoFuture};
|
2018-12-16 18:43:44 +00:00
|
|
|
use log::{error, info};
|
2018-11-18 01:39:04 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
|
2019-05-25 20:22:26 +00:00
|
|
|
use crate::{JobError, JobInfo, Processor, ReturnJobInfo};
|
2018-11-18 01:39:04 +00:00
|
|
|
|
|
|
|
/// A generic function that processes a job
|
|
|
|
///
|
|
|
|
/// Instead of storing
|
2018-12-16 19:44:25 +00:00
|
|
|
/// [`Processor`](https://docs.rs/background-jobs/0.4.0/background_jobs/trait.Processor.html) type
|
2018-11-18 01:39:04 +00:00
|
|
|
/// directly, the
|
2018-12-16 19:44:25 +00:00
|
|
|
/// [`ProcessorMap`](https://docs.rs/background-jobs-core/0.4.0/background_jobs_core/struct.ProcessorMap.html)
|
2018-11-18 01:39:04 +00:00
|
|
|
/// struct stores these `ProcessFn` types that don't expose differences in Job types.
|
2019-05-24 03:41:34 +00:00
|
|
|
pub type ProcessFn<S> =
|
|
|
|
Box<dyn Fn(Value, S) -> Box<dyn Future<Item = (), Error = JobError> + Send> + Send>;
|
|
|
|
|
|
|
|
pub type StateFn<S> = Box<dyn Fn() -> S + Send + Sync>;
|
2018-11-18 01:39:04 +00:00
|
|
|
|
|
|
|
/// A type for storing the relationships between processor names and the processor itself
|
|
|
|
///
|
2018-12-16 19:44:25 +00:00
|
|
|
/// [`Processor`s](https://docs.rs/background-jobs/0.4.0/background_jobs/trait.Processor.html) must
|
2018-11-18 14:43:10 +00:00
|
|
|
/// be registered with the `ProcessorMap` in the initialization phase of an application before
|
2018-11-18 01:39:04 +00:00
|
|
|
/// workers are spawned in order to handle queued jobs.
|
2018-11-18 21:05:03 +00:00
|
|
|
pub struct ProcessorMap<S>
|
|
|
|
where
|
|
|
|
S: Clone,
|
|
|
|
{
|
2019-05-24 03:41:34 +00:00
|
|
|
inner: HashMap<String, ProcessFn<S>>,
|
|
|
|
state_fn: StateFn<S>,
|
2018-11-18 01:39:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 21:05:03 +00:00
|
|
|
impl<S> ProcessorMap<S>
|
|
|
|
where
|
2019-05-24 03:41:34 +00:00
|
|
|
S: Clone + 'static,
|
2018-11-18 21:05:03 +00:00
|
|
|
{
|
2018-11-18 01:39:04 +00:00
|
|
|
/// Intialize a `ProcessorMap`
|
2018-11-18 21:05:03 +00:00
|
|
|
///
|
|
|
|
/// The state passed into this method will be passed to all jobs executed through this
|
|
|
|
/// ProcessorMap. The state argument could be useful for containing a hook into something like
|
|
|
|
/// r2d2, or the address of an actor in an actix-based system.
|
2019-05-24 03:41:34 +00:00
|
|
|
pub fn new(state_fn: StateFn<S>) -> Self {
|
2018-11-18 21:05:03 +00:00
|
|
|
ProcessorMap {
|
|
|
|
inner: HashMap::new(),
|
2019-05-24 03:41:34 +00:00
|
|
|
state_fn,
|
2018-11-18 21:05:03 +00:00
|
|
|
}
|
2018-11-18 01:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Register a
|
2018-12-16 19:44:25 +00:00
|
|
|
/// [`Processor`](https://docs.rs/background-jobs/0.4.0/background_jobs/trait.Processor.html) with
|
2018-11-18 01:39:04 +00:00
|
|
|
/// this `ProcessorMap`.
|
|
|
|
///
|
|
|
|
/// `ProcessorMap`s are useless if no processors are registerd before workers are spawned, so
|
|
|
|
/// make sure to register all your processors up-front.
|
|
|
|
pub fn register_processor<P>(&mut self, processor: P)
|
|
|
|
where
|
2019-05-24 03:25:00 +00:00
|
|
|
P: Processor<S> + Send + 'static,
|
2018-11-18 01:39:04 +00:00
|
|
|
{
|
|
|
|
self.inner.insert(
|
2018-11-18 14:43:10 +00:00
|
|
|
P::NAME.to_owned(),
|
2019-05-24 03:41:34 +00:00
|
|
|
Box::new(move |value, state| processor.process(value, state)),
|
2018-11-18 01:39:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process a given job
|
|
|
|
///
|
|
|
|
/// This should not be called from outside implementations of a backgoround-jobs runtime. It is
|
|
|
|
/// intended for internal use.
|
2019-05-25 20:22:26 +00:00
|
|
|
pub fn process_job(&self, job: JobInfo) -> impl Future<Item = ReturnJobInfo, Error = ()> {
|
2018-11-18 01:39:04 +00:00
|
|
|
let opt = self
|
|
|
|
.inner
|
|
|
|
.get(job.processor())
|
2019-05-24 03:41:34 +00:00
|
|
|
.map(|processor| process(processor, (self.state_fn)(), job.clone()));
|
2018-11-18 01:39:04 +00:00
|
|
|
|
|
|
|
if let Some(fut) = opt {
|
|
|
|
Either::A(fut)
|
|
|
|
} else {
|
|
|
|
error!("Processor {} not present", job.processor());
|
2019-05-25 20:22:26 +00:00
|
|
|
Either::B(Ok(ReturnJobInfo::missing_processor(job.id())).into_future())
|
2018-11-18 01:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 20:22:26 +00:00
|
|
|
fn process<S>(
|
|
|
|
process_fn: &ProcessFn<S>,
|
|
|
|
state: S,
|
|
|
|
job: JobInfo,
|
|
|
|
) -> impl Future<Item = ReturnJobInfo, Error = ()> {
|
2018-11-18 01:39:04 +00:00
|
|
|
let args = job.args();
|
2019-05-25 20:22:26 +00:00
|
|
|
let id = job.id();
|
2018-11-18 01:39:04 +00:00
|
|
|
let processor = job.processor().to_owned();
|
|
|
|
|
2019-05-24 03:41:34 +00:00
|
|
|
process_fn(args, state).then(move |res| match res {
|
2018-11-18 01:39:04 +00:00
|
|
|
Ok(_) => {
|
2019-05-25 20:22:26 +00:00
|
|
|
info!("Job {} completed, {}", id, processor);
|
|
|
|
Ok(ReturnJobInfo::pass(id))
|
2018-11-18 01:39:04 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2019-05-25 20:22:26 +00:00
|
|
|
error!("Job {} errored, {}, {}", id, processor, e);
|
|
|
|
Ok(ReturnJobInfo::fail(id))
|
2018-11-18 01:39:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|