geofmureithi/apalis

Stop worker if already running and start the new one.

Closed this issue · 1 comments

Hello, I am using WorkerBuilder with CronStream and if I have a job that starts every 10 seconds I would like to cancel the previous instance of that job if it is still running because sometimes I might have long running tasks that are not yet completed. How can I achieve that?

The end goal is if I have multiple workers I want only max one instance of them to be running at all times.

Also how can i stop the current job manually, if i try using .stop() it does not do anything.

pub async fn schedule_test(
    _schedule_test: ScheduleTest,
    worker_ctx: Data<WorkerCtx>,
    worker_id: WorkerId,
) -> Result<(), common::error::GlobalError> {
    worker_ctx.stop();
    for i in 0..10 {
        tokio::time::sleep(Duration::from_secs(1)).await; 
        println!("Number: {}", i);
    }
    Ok(())
}