Monday, December 22, 2008

How to monkey hack backgroundrb's persitant queue to not suck

BackgrounDRB has the problem that if you use the persistant queue stuff (example: "worker.enq_some_task()"), then it only unloads that queue one item per delay rate. (the default delay is 5 seconds, that means if you're doing more than 1 item per 5 seconds, the queue is never going to shrink. Thats not right! What I want it to do is unload the queue every 5 seconds, no matter how long it takes.


This code will do it, but it kinda sucks, since its doing double sql selects.


class CommentInstantEmailsWorker < BackgrounDRb::MetaWorker
set_worker_name :comment_instant_emails_worker

def check_for_enqueued_tasks_with_repeat
while BdrbJobQueue.count(:conditions =>
[" worker_name = ? AND taken = ? AND scheduled_at <= ? ",
worker_name.to_s, 0, Time.now.utc ]) > 0
check_for_enqueued_tasks_without_repeat
end
end

alias_method_chain :check_for_enqueued_tasks, :repeat
...

0 comments: