Add supported of distributed job scheduling #40
-
Current cronjob is scheduled and run in worker itself. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I am not sure what you are asking here. Do you mean when a cron job is created then it is added to a database? |
Beta Was this translation helpful? Give feedback.
-
@geofmureithi I'd like to schedule a repeat action(task) which will be triggered every morning. And then I can start a couple of workers, running in different servers, to deal with the task but only one worker is allow each time. |
Beta Was this translation helpful? Give feedback.
-
Maybe what you are looking for is a combination of async fn enqueue_daily(job: DailyJob, ctx: JobContext) {
let redis = ctx.data_opt::<RedisStorage>().unwrap();
redis.push(job).await.unwrap();
}
async fn do_distributed_stuff(job: DailyJob, _ctx: JobContext) {
.......
}
#[tokio::main]
async fn main() {
let redis = std::env::var("REDIS_URL").expect("Missing env variable REDIS_URL");
let storage = RedisStorage::new(redis).await?;
let schedule = Schedule::from_str("@daily").unwrap();
let service = ServiceBuilder::new()
.layer(Extension(storage.clone()))
.service(job_fn(enqueue_daily));
let cron_worker = CronWorker::new(schedule, service);
Monitor::new()
.register(cron_worker)
.register_with_count(2, move || {
WorkerBuilder::new(storage.clone())
.build_fn(do_distributed_stuff)
})
.run()
.await
.unwrap();
} Since You can connect to the same redis using multiple servers, you can extract the cron part to one bin the start multiple storage workers on different server. Hope this helps. |
Beta Was this translation helpful? Give feedback.
Maybe what you are looking for is a combination of
CronWorker
andStorageWorker
Here is a quick example: