我正在试图弄清楚如何能够做到以下几点。
我想在每天中午12点开始工作,计算一系列事物然后按批量'b'处理这些事情。并保证一批结束与另一批结束之间的间隔为x分钟。
schedule.cron '00 12 * * *' do
# things = Compute the list of things
schedule.interval '10m', # Job.new(things[index, 10]) ???
end
我尝试了类似下面的内容,但我讨厌我必须将调度程序传递给作业或访问单例调度程序。
class Job < Struct.new(:things, :batch_index, :scheduler)
def call
if (batch_index+1) >= things.length
ap "Done with all batches"
else
# Process batch
scheduler.in('10m', Dummy.new(things, batch_index+1, scheduler))
end
end
end
scheduler = Rufus::Scheduler.new
schedule.cron '00 12 * * *' , Dummy.new([ [1,2,3] , [4,5,6,7], [8,9,10]], 0, scheduler)
答案 0 :(得分:1)
来自https://github.com/jmettraux/rufus-scheduler#scheduling-handler-instances
的一些帮助我准备了一个类:
class Job
def initialize(batch_size)
@batch_size = batch_size
end
def call(job, time)
# > I want to start a job (...) which computes a list of things and
# > then processes these things in batches of size 'b'.
things = compute_list_of_things()
loop do
ts = things.shift(@batch_size)
do_something_with_the_batch(ts)
break if things.empty?
# > and guarantee an interval of x minutes between the end of one batch
# > and the beginning of another.
sleep 10 * 60
end
end
end
我将该类的实例传递给调度程序而不是块:
scheduler = Rufus::Scheduler.new
# > I want to start a job at 12:00pm every day which computes...
scheduler.cron '00 12 * * *', Job.new(10) # batch size of 10...
我不打扰使用调度程序等待10分钟。