Ruby 定期检查sidekiq作业是否已取消

Ruby 定期检查sidekiq作业是否已取消,ruby,sidekiq,Ruby,Sidekiq,sidekiq中的作业应该检查它们是否被取消,但如果我有一个长期运行的作业,我希望它定期检查自己。这个例子不起作用:我没有在未来的任何形式中包装假作品,在其中我可以提出一个例外——我甚至不确定这是可能的。我该怎么做 class ThingWorker def perform(phase, id) thing = Thing.find(id) # schedule the initial check schedule_cancellation_check(thing

sidekiq中的作业应该检查它们是否被取消,但如果我有一个长期运行的作业,我希望它定期检查自己。这个例子不起作用:我没有在未来的任何形式中包装假作品,在其中我可以提出一个例外——我甚至不确定这是可能的。我该怎么做

class ThingWorker

  def perform(phase, id)
    thing = Thing.find(id)

    # schedule the initial check
    schedule_cancellation_check(thing.updated_at, id)

    # maybe wrap this in something I can raise an exception within?
    sleep 10 # fake work
    @done = true

    return true
  end


  def schedule_cancellation_check(initial_time, thing_id)
    Concurrent.schedule(5) {

      # just check right away...
      return if @done

      # if our thing has been updated since we started this job, kill this job!
      if Thing.find(thing_id).updated_at != initial_time
        cancel!

      # otherwise, schedule the next check
      else
        schedule_cancellation_check(initial_time, thing_id)
      end
    }
  end

  # as per sidekiq wiki
  def cancelled?
    @cancelled
    Sidekiq.redis {|c| c.exists("cancelled-#{jid}") }
  end

  def cancel!
    @cancelled = true
    # not sure what this does besides marking the job as cancelled tho, read source
    Sidekiq.redis {|c| c.setex("cancelled-#{jid}", 86400, 1) }
  end

end

你想得太多了。您的工作者应该是一个循环,并检查每次迭代是否取消

def perform(thing_id, updated_at)
  thing = Thing.find(thing_id)
  while !cancel?(thing, updated_at)
    # do something
  end
end

def cancel?(thing, last_updated_at)
  thing.reload.updated_at > last_updated_at
end