Added some optional callback functions for the workers#11
Conversation
robinfriedli
left a comment
There was a problem hiding this comment.
Seems like a neat feature, thanks. Just have some minor change requests
| keep_alive: Duration, | ||
| channel_data: Arc<ChannelData>, | ||
| worker_data: Arc<WorkerData>, | ||
| on_worker_start: Option<WorkerCallback>, |
There was a problem hiding this comment.
I'd add those fields to the WorkerData struct
| } | ||
|
|
||
| /// When a worker thread stops this callback function will be invoked | ||
| /// exactly once before |
| /// When a worker thread starts this callback function will be invoked | ||
| /// exactly once before it starts processing any jobs | ||
| pub fn on_worker_start<F>(&mut self, callback: F) | ||
| where F: Fn() + Send + Sync + 'static |
There was a problem hiding this comment.
make sure to run cargo fmt, this should look more like
pub fn on_worker_start<F>(&mut self, callback: F)
where
F: Fn() + Send + Sync + 'static,
{
self.on_worker_start.replace(Arc::new(callback));
}| assert!(val > 0); | ||
| }); | ||
| } | ||
| thread::sleep(Duration::from_secs(2)); |
There was a problem hiding this comment.
I'd use the join() function to reduce the chance of race conditions (if it takes longer than 2 seconds for whatever reason). And perhaps even use the on_worker_stop callback to signal a CondVar when the last worker stops and await it with a timeout, instead of sleeping much longer than necessary in most cases.
|
@john-sharratt please let me know if you don't have time to work on this, I'll make the requested changes myself |
Use cases for the callback functions are: