lib/stalker.rb in stalker-0.3.0 vs lib/stalker.rb in stalker-0.4.0

- old
+ new

@@ -3,24 +3,29 @@ require 'uri' module Stalker extend self - def enqueue(job, args={}) + def enqueue(job, args={}, opts={}) + pri = opts[:pri] || 65536 + delay = opts[:delay] || 0 + ttr = opts[:ttr] || 120 beanstalk.use job - beanstalk.put [ job, args ].to_json + beanstalk.put [ job, args ].to_json, pri, delay, ttr + rescue Beanstalk::NotConnected => e + failed_connection(e) end def job(j, &block) @@handlers ||= {} @@handlers[j] = block end class NoJobsDefined < RuntimeError; end class NoSuchJob < RuntimeError; end - def work(jobs=nil) + def prep(jobs=nil) raise NoJobsDefined unless defined?(@@handlers) jobs ||= all_jobs jobs.each do |job| @@ -32,18 +37,19 @@ jobs.each { |job| beanstalk.watch(job) } beanstalk.list_tubes_watched.each do |server, tubes| tubes.each { |tube| beanstalk.ignore(tube) unless jobs.include?(tube) } end - - loop do - work_one_job - end rescue Beanstalk::NotConnected => e failed_connection(e) end + def work(jobs=nil) + prep(jobs) + loop { work_one_job } + end + def work_one_job job = beanstalk.reserve name, args = JSON.parse job.body log_job_begin(name, args) handler = @@handlers[name] @@ -118,7 +124,11 @@ msg.join("\n") end def all_jobs @@handlers.keys + end + + def clear! + @@handlers = nil end end