From e9dc3b69f47fdf6ab3a4f6e3e31d969dcee35b02 Mon Sep 17 00:00:00 2001 From: asonix Date: Sat, 1 Jun 2019 10:58:05 -0500 Subject: [PATCH] Clonable ProcessorMap --- jobs-core/src/processor_map.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/jobs-core/src/processor_map.rs b/jobs-core/src/processor_map.rs index 66d869c..5858eed 100644 --- a/jobs-core/src/processor_map.rs +++ b/jobs-core/src/processor_map.rs @@ -17,7 +17,7 @@ * along with Background Jobs. If not, see . */ -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; use futures::future::{Either, Future, IntoFuture}; use log::{error, info}; @@ -33,15 +33,16 @@ use crate::{Job, JobError, JobInfo, Processor, ReturnJobInfo}; /// [`ProcessorMap`](https://docs.rs/background-jobs-core/0.4.0/background_jobs_core/struct.ProcessorMap.html) /// struct stores these `ProcessFn` types that don't expose differences in Job types. pub type ProcessFn = - Box Box + Send> + Send>; + Arc Box + Send> + Send + Sync>; -pub type StateFn = Box S + Send + Sync>; +pub type StateFn = Arc S + Send + Sync>; /// A type for storing the relationships between processor names and the processor itself /// /// [`Processor`s](https://docs.rs/background-jobs/0.4.0/background_jobs/trait.Processor.html) must /// be registered with the `ProcessorMap` in the initialization phase of an application before /// workers are spawned in order to handle queued jobs. +#[derive(Clone)] pub struct ProcessorMap where S: Clone, @@ -74,12 +75,12 @@ where /// make sure to register all your processors up-front. pub fn register_processor(&mut self, processor: P) where - P: Processor + Send + 'static, + P: Processor + Sync + Send + 'static, J: Job, { self.inner.insert( P::NAME.to_owned(), - Box::new(move |value, state| processor.process(value, state)), + Arc::new(move |value, state| processor.process(value, state)), ); }