1

我试图弄清楚如何能够做到以下几点。

我想每天下午 12:00 开始一项工作,计算一个事物列表,然后分批处理这些事物,大小为“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)
4

1 回答 1

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 分钟。

于 2014-03-20T21:22:06.937 回答