ruby on rails - resque-status and resque-scheduler for delayed jobs -
i used resque-scheduler delay jobs in previous code:
resque.enqueue_in(options[:delay].seconds, self, context)
now want include resque-status job have no idea how can work together. latest resque-status source code supports scheduler, in source code:
https://github.com/quirkey/resque-status/blob/master/lib/resque/plugins/status.rb
# wrapper api forward resque::job creation api call resque::plugins::status call. # needed used resque scheduler # http://github.com/bvandenbos/resque-scheduler def scheduled(queue, klass, *args) self.enqueue_to(queue, self, *args) end
end
but i'm not sure how use it. shall call samplejob.scheduled(queue, myclass, :delay => delay) instead of samplejob.create(options)?
======================================================================
also, there support resque-status (and other custom jobs):
https://github.com/bvandenbos/resque-scheduler
some resque extensions resque-status use custom job classes different api signature. resque-scheduler isn't trying support existing , future custom job classes, instead supports schedule flag can extend custom class , make support scheduled job.
let's pretend have jobwithstatus class called fakeleaderboard
class fakeleaderboard < resque::jobwithstatus def perform # , keep track of status end end
and schedule:
create_fake_leaderboards: cron: "30 6 * * 1" queue: scoring custom_job_class: fakeleaderboard args: rails_env: demo description: "this job auto-create leaderboards our online demo , status update worker makes progress"
but seems recurring jobs. can find params of cron, not delay. how can handle delayed jobs it?
thanks!
i had same issue , solved myself implementing module provide runner "statused" jobs.
module resque # :nodoc: # module include in worker class resque-status # , resque-scheduler integration module scheduledjobwithstatus extend activesupport::concern included # include status functionalities include resque::plugins::status end # :nodoc: module classmethods # method use custom worker class enqueue jobs # resque-scheduler plugin status support def enqueue_at(timestamp, *args) class_name = self.to_s # store class name since plain class object not "serializable" resque.enqueue_at(timestamp, jobwithstatusrunner, class_name, *args) end # identical enqueue_at takes number_of_seconds_from_now # instead of timestamp. def enqueue_in(number_of_seconds_from_now, *args) enqueue_at(time.now + number_of_seconds_from_now, *args) end end end # wrapper worker enqueuing class jobwithstatusrunner # default queue scheduling jobs status @queue = :delayed # receive jobs {resque::scheduledjobwithstatus} queue them in resque # support status informations def self.perform(status_klass, *args) # retrieve original worker class klass = status_klass.to_s.constantize # check if supports status jobs unless klass.included_modules.include? resque::plugins::status rails.logger.warn("class #{klass} doesn't support jobs status") return false end rails.logger.debug("enqueing jobs #{klass} arguments #{args}") klass.create(*args) rescue nameerror rails.logger.error("unable enqueue jobs class #{status_klass} args #{args}") false end end end
in way can enqueue jobs simple syntax:
# simple worker class class sleepjob # provides integrations both resque-status , resque-scheduler include resque::scheduledjobwithstatus # method triggered resque def perform total = (options['length'] || 60).to_i 1.upto(total) { |i| at(i, total, "at #{i} of #{total}"); sleep(1) } end end # run job delayed sleepjob.enqueue_in(5.minutes) # or sleepjob.enqueue_at(5.minutes.from_now)
just drop module in resque initializer or in lib
folder. in latter case remember require somewhere.
Comments
Post a Comment