2016-11-22 5 views
0

sidekiqのジョブは、ジョブがキャンセルされたかどうかを確認することを想定していますが、長時間実行しているジョブは定期的にチェックします。この例はうまくいきません。私は例外を発生させることができる将来のあらゆる種類の偽の作品をラップしていません。どうすればいい?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 

答えて

0

これはあまりにも難しいと思っています。あなたのワーカーはループになっており、繰り返しごとにキャンセルを確認する必要があります。

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 
関連する問題