geofmureithi / apalis

Simple, extensible multithreaded background job and message processing library for Rust
https://crates.io/crates/apalis
MIT License
446 stars 39 forks source link

How to shared Monitor object between multi thread #402

Closed gensuineixin closed 1 week ago

gensuineixin commented 2 weeks ago

How to shared Monitor object between multi thread? When I use Monitor with actix-web, I want to shared monitor object by app_data,like bellow:

let mut monitor = Arc::new(Mutex::new(Monitor::::new())); HttpServer::new(move || { App::new() .app_data(email_data.clone()) .app_data(monitor) .service(web::scope("/emails").route("/push",web::post().to(push_email))) })

but this code can not run. The error is : the trait bound std::sync::MutexGuard<'_, apalis_core::monitor::Monitor<apalis::utils::TokioExecutor>>: Clone is not satisfied in {closure@examples\rest-api\src/main.rs:220:25: 220:32} within {closure@examples\rest-api\src/main.rs:220:25: 220:32}, the trait Clone is not implemented for std::sync::MutexGuard<'_, apalis_core::monitor::Monitor<apalis::utils::TokioExecutor>>, which is required by {closure@examples\rest-api\src/main.rs:220:25: 220:32}: Clone

geofmureithi commented 2 weeks ago

I will look into why this doesn't work. I am curious though, why do you need to share the Monitor around? Monitor will not allow you to do much apart from registering workers and listening for shutdown.

gensuineixin commented 2 weeks ago

Yeah,I read some job info from http request,the job info contains cron info and some other,so,I share the monitor to register some worker,the worker run with diff crons

geofmureithi commented 1 week ago

This looks like an actix issue, it accepts a callback and everything needs to be clone You should do:

.app_data(monitor.clone())