我有如下的task_scheduler文件,
require 'rubygems'
require 'rufus/scheduler'
scheduler = Rufus::Scheduler.start_new
scheduler.cron '0 20 * * *' do
StartJOb1.perform_async
Startjob2.perform_async
Startjob3.perform_async
Startjob4.perform_async
Startjob5.perform_async
end
我需要拆分这些作业,并希望运行Startjob4.perform_async,Startjob5.perform_async在作业1、作业2和作业3完成后。问题是我不知道完成这些作业需要多长时间。
我正在使用sidekiq进行这些后台任务
感谢
这在Cron中是不可能的,您可能应该从当前作业中调用下一个作业。
而且,由于使用了cron,您不需要sidekiq。
我没有使用sidekiq,所以不确定这是否有效。您可以尝试使用rails中所称的信号量或Mutex。类似于这个的东西
mutex1, mutex2, mutex3 = new_mutexes_already_locked
StartJOb1.perform_async(mutex1) #-> unlocks mutex1 as soon as work is finished
Startjob2.perform_async(mutex2) #-> unlocks mutex2 as soon as work is finished
Startjob3.perform_async(mutex3) #-> unlocks mutex3 as soon as work is finished
Startjob4.perform_async(mutex1, mutex2, mutex3) #starts work if mutex1..3 unlocked
Startjob5.perform_async(mutex1, mutex2, mutex3) #dito
你也可以用一个方法一起启动Job4+5,并让这个方法等待为互斥1..3获取锁
但我不确定这是否很适合你正在做的事情,也不确定它是否能与sidekiq一起工作,因为Mutex是实际用于并行你自己的线程的(因此它可能不会在进程上工作,我想sidekiq在它自己的进程中运行?)。
作为互斥锁的替代方案,您可以将作业的状态写入数据库,并从作业4+5进行轮询,以查看它们何时完成。
您也可以使用Sidekiq Superworker来实现这一点,因为它可以定义worker的依赖关系图。(作为免责声明,我是gem的作者。)