Skip to content
Closed
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
22 changes: 16 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum Operation {

pub struct SidekiqServer<'a> {
redispool: RedisPool,
threadpool: ThreadPool,
threadpool: Option<ThreadPool>,
pub namespace: String,
job_handlers: BTreeMap<String, Box<dyn JobHandler + 'a>>,
middlewares: Vec<Box<dyn MiddleWare + 'a>>,
Expand All @@ -48,6 +48,7 @@ pub struct SidekiqServer<'a> {
signal_chan: Receiver<c_int>,
worker_info: BTreeMap<String, bool>, // busy?
concurrency: usize,
pub stack_size: Option<usize>,
pub force_quite_timeout: usize,
}

Expand All @@ -57,16 +58,16 @@ impl<'a> SidekiqServer<'a> {
pub fn new(redis: &str, concurrency: usize) -> Result<Self> {
let signal_chan = signal_listen(&[SIGINT, SIGUSR1])?;
let now = Utc::now();
let pool = r2d2::Pool::builder()
let redispool = r2d2::Pool::builder()
.max_size(concurrency as u32 + 3)
.build(redis::Client::open(redis)?)?;

let mut rng = rand::thread_rng();
let identity: Vec<u8> = iter::repeat(()).map(|()| rng.sample(distributions::Alphanumeric)).take(12).collect();

Ok(SidekiqServer {
redispool: pool,
threadpool: ThreadPool::with_name("worker".into(), concurrency),
redispool,
threadpool: None,
namespace: String::new(),
job_handlers: BTreeMap::new(),
queues: vec![],
Expand All @@ -75,6 +76,7 @@ impl<'a> SidekiqServer<'a> {
worker_info: BTreeMap::new(),
concurrency,
signal_chan,
stack_size: None,
force_quite_timeout: 10,
middlewares: vec![],
rs: String::from_utf8_lossy(&identity).to_string(),
Expand All @@ -96,6 +98,14 @@ impl<'a> SidekiqServer<'a> {
}

pub fn start(&mut self) {
let mut threadpool = threadpool::Builder::new()
.thread_name("worker".to_string())
.num_threads(self.concurrency);
if let Some(size) = self.stack_size {
threadpool = threadpool.thread_stack_size(size);
}
self.threadpool = Some(threadpool.build());

info!("sidekiq is running...");
if self.queues.len() == 0 {
error!("queue is empty, exiting");
Expand Down Expand Up @@ -140,7 +150,7 @@ impl<'a> SidekiqServer<'a> {
if let Ok(Err(e)) = sig.map(|s| self.deal_signal(s)) {
error!("error when dealing signal: '{}'", e);
}
let worker_count = self.threadpool.active_count();
let worker_count = self.threadpool.as_ref().unwrap().active_count();
// relaunch workers if they died unexpectly
if worker_count < self.concurrency {
warn!("worker down, restarting");
Expand Down Expand Up @@ -179,7 +189,7 @@ impl<'a> SidekiqServer<'a> {
self.middlewares.iter_mut().map(|v| v.cloned()).collect(),
self.namespace.clone());
self.worker_info.insert(worker.id.clone(), false);
self.threadpool.execute(move || worker.work());
self.threadpool.as_ref().unwrap().execute(move || worker.work());
}

fn inform_termination(&self, tox: Sender<Operation>) {
Expand Down