Skip to content

Instantly share code, notes, and snippets.

@KellyMahan
Created September 14, 2011 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KellyMahan/1217726 to your computer and use it in GitHub Desktop.
Save KellyMahan/1217726 to your computer and use it in GitHub Desktop.
I needed a queue system just like Resque but that would halt on the first error.
# I came up with this code because I needed a queue system just like Resque but that would halt on the first error waiting for user intervention.
# It works well for what i need but I think it would be a great feature to have built into resque that could simplify what i've done.
class QueWorker
def self.start
loop do
begin
QueWorker.qloop
rescue Exception => e
job = Resque.pop(:queuename)
Resque.lenqueue(ClassName, job['args'][0], job['args'][1], job['args'][2], e.message)
sleep 60
end
end
end
def self.qloop
loop do
if job = Resque.peek(:queuename)
if eval("#{job["class"]}").perform(*job['args'])
job = Resque.pop(:queuename)
end
else
sleep 5
end
end
end
end
module Resque
def lpush(queue, item)
watch_queue(queue)
redis.lpush "queue:#{queue}", encode(item)
end
def lenqueue(klass, *args)
# Perform before_enqueue hooks. Don't perform enqueue if any hook returns false
before_hooks = Plugin.before_enqueue_hooks(klass).collect do |hook|
klass.send(hook, *args)
end
return if before_hooks.any? { |result| result == false }
Job.lcreate(queue_from_class(klass), klass, *args)
Plugin.after_enqueue_hooks(klass).each do |hook|
klass.send(hook, *args)
end
end
class Job
def self.lcreate(queue, klass, *args)
Resque.validate(klass, queue)
if Resque.inline?
constantize(klass).perform(*decode(encode(args)))
else
Resque.lpush(queue, :class => klass.to_s, :args => args)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment