Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove thread pool, switch to tokio #10

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
518 changes: 457 additions & 61 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ readme = "README.md"
keywords = ["sidekiq", "worker", "resque", "ruby"]

[dependencies]
crossbeam-channel = "0.5.5"
anyhow = { version = "1.0.65", features = ["backtrace"] }
async-channel = "1.7.1"
async-trait = "0.1.57"
signal-hook = "0.3.14"
chrono = { version = "0.4.22", default-features = false, features = ["serde", "clock"] }
env_logger = "0.9.0"
error-chain = "0.12.4" # 2 years since last release. consider replacing with a different crate
futures = "0.3.24"
gethostname = "0.2.3"
log = "0.4.17"
r2d2 = "0.8.10"
rand = "0.8.5"
redis = { version = "0.21.5", features = ["r2d2"] }
redis = { version = "0.21.5", features = ["connection-manager", "tokio-comp", "tokio-native-tls-comp"] }
serde = "1.0.137"
serde_json = "1.0.81"
threadpool = "1.8.1" # 2 years since last release. there is a 2.0 branch currently in development
gethostname = "0.2.3"
tokio = { version = "1.21.1", features = ["full"] }

[dev-dependencies]
structopt = "0.3.26"
Expand Down
63 changes: 49 additions & 14 deletions examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use sidekiq_server::{error_handler, panic_handler, printer_handler, retry_middleware, SidekiqServer};
use anyhow::anyhow;
use async_trait::async_trait;
use log::*;
use sidekiq_server::{SidekiqServer, Job, JobHandler, JobHandlerResult, JobSuccessType::*};
use structopt::StructOpt;

#[derive(StructOpt, Debug, Clone)]
Expand Down Expand Up @@ -36,22 +39,54 @@ fn main() {
})
.collect();

let mut server = SidekiqServer::new(&params.redis, params.concurrency).unwrap();
let runtime = {
let mut builder = tokio::runtime::Builder::new_multi_thread();
builder.worker_threads(params.concurrency + 3);
builder.enable_all();
builder.build().unwrap()
};
runtime.handle().clone().block_on(async {
let mut server = SidekiqServer::new(&params.redis, params.concurrency).await.unwrap();
server.namespace = params.namespace;
server.force_quite_timeout = params.timeout;

server.attach_handler("Printer", printer_handler);
server.attach_handler("Error", error_handler);
server.attach_handler("Panic", panic_handler);
server.attach_handler("DefaultJob", DefaultJob());
server.attach_handler("FailingJob", FailingJob());
server.attach_handler("PanickingJob", PanickingJob());

server.attach_middleware(retry_middleware);
for (name, weight) in queues {
server.new_queue(&name, weight);
for (name, weight) in queues {
server.new_queue(&name, weight);
}

server.start().await;
runtime.shutdown_background();
});
}

pub struct DefaultJob();

#[async_trait]
impl JobHandler for DefaultJob {
async fn perform(&self, job: &Job) -> JobHandlerResult {
info!("handling {:?}", job);
Ok(Success)
}
}

server.namespace = params.namespace;
server.force_quite_timeout = params.timeout;
start(server)
pub struct FailingJob();

#[async_trait]
impl JobHandler for FailingJob {
async fn perform(&self, _job: &Job) -> JobHandlerResult {
Err(anyhow!("oh no"))
}
}

fn start(mut server: SidekiqServer) {
server.start();
}
pub struct PanickingJob();

#[async_trait]
impl JobHandler for PanickingJob {
async fn perform(&self, _job: &Job) -> JobHandlerResult {
panic!("oh no")
}
}
35 changes: 6 additions & 29 deletions src/job_handler.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
use anyhow::Result;
use async_trait::async_trait;

use crate::job::Job;
use crate::JobSuccessType;
use crate::JobSuccessType::*;
use crate::errors::{ErrorKind, Result};

pub type JobHandlerResult = Result<JobSuccessType>;

pub trait JobHandler: Send {
fn handle(&mut self, job: &Job) -> JobHandlerResult;
fn cloned(&mut self) -> Box<dyn JobHandler>;
}

impl<F> JobHandler for F
where F: FnMut(&Job) -> JobHandlerResult + Copy + Send + 'static
{
fn handle(&mut self, job: &Job) -> JobHandlerResult {
self(job)
}
fn cloned(&mut self) -> Box<dyn JobHandler> {
Box::new(*self)
}
}

pub fn printer_handler(job: &Job) -> JobHandlerResult {
info!("handling {:?}", job);
Ok(Success)
}

pub fn error_handler(_: &Job) -> JobHandlerResult {
Err(ErrorKind::JobHandlerError(Box::new("a".parse::<i8>().unwrap_err())).into())
#[async_trait]
pub trait JobHandler: Send + Sync {
async fn perform(&self, job: &Job) -> JobHandlerResult;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msepga has a different approach using a BoxFuture. That fixed the lifetime issues I had as of the first commit on this branch, but when integrating it into our own code, the futures generated by other crates caused compile errors because they aren't Sync.

Copy link
Contributor

@msepga msepga Oct 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle this should be equivalent to Box<dyn Sync + ...> given that JobHandler requires Sync here, though we can merge as-is regardless 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean by JobFuture -- there isn't a type like that around.

My understanding is that the BoxFuture version and my attempt in the first commit was running into issues because it required that the returned futures be Sync. This implementation only requires that the JobHandler is Sync.

Copy link
Contributor

@msepga msepga Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, should be JobHandler in my comment 🙂 So, BoxFuture doesn't require the future to be Sync, the linked implementation only required the handler closure to be Sync:

With the definition of BoxFuture:

pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a, Global>>;

Looking at the type now:

Box<dyn Send + Sync + FnMut(Job) -> BoxFuture<'static, Result<JobSuccessType>>>
    | The handler itself must: |    | The BoxFuture must:                    |
    | - impl Send              |    |  - Be Send                             |
    | - impl Sync              |    |  - Be 'static                          |
    | - Take a `Job` argument  |    |  - Return Result<JobSuccessType>       |
    | - Return `BoxFuture<..>`----> |                                        |
    `--------------------------'    |  The BoxFuture *does not* have to be   |
                                    |  Sync                                  |
                                    |                                        |
                                    `----------------------------------------'

Here's a playground link that illustrates how the handler can be sync without the returned future being sync. You'll notice that uncommenting line 25 will fail to compile, because the returned future doesn't implement Sync, even if the handler is required to do so.

We don't have to let this block the merge BTW, I just figured I'd leave a footnote in case it could help clear up how Box<dyn ... + Fn> is equivalent to the JobHandler trait here.

}

pub fn panic_handler(_: &Job) -> JobHandlerResult {
panic!("yeah, I do it deliberately")
}
17 changes: 1 addition & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
#![forbid(unsafe_code)]

extern crate serde;
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate log;
extern crate env_logger;
#[macro_use]
extern crate error_chain;
extern crate threadpool;
extern crate redis;
extern crate rand;
extern crate chrono;
#[macro_use]
extern crate crossbeam_channel;

mod server;
mod job_handler;
pub mod errors;
mod job;
mod worker;
mod middleware;

pub use server::SidekiqServer;
pub use job_handler::{JobHandler, JobHandlerResult, printer_handler, error_handler, panic_handler};
pub use middleware::{MiddleWare, MiddleWareResult, peek_middleware, retry_middleware,
time_elapse_middleware, NextFunc};
pub use job_handler::{JobHandler, JobHandlerResult};
pub use job::{Job, RetryInfo};
pub type RedisPool = r2d2::Pool<redis::Client>;

#[derive(Debug, Clone)]
pub enum JobSuccessType {
Expand Down
80 changes: 0 additions & 80 deletions src/middleware.rs

This file was deleted.

Loading