Permalink
Browse files

Add an explicit connection check before working with the BDRB job que…

…ue tables.

This fixes an issue where after a certain period of time, the BDRB server can see the following exception:

BdrbJobQueue Load (26.009950) [0]   SELECT * FROM `bdrb_job_queues` WHERE ( worker_name = 'foo_worker' AND taken = 0 AND scheduled_at <= '2009-03-13 14:45:33' ) LIMIT 1 FOR UPDATE
BdrbJobQueue Load (31.594619) [0]   SELECT * FROM `bdrb_job_queues` WHERE ( worker_name = 'bar_worker' AND taken = 0 AND scheduled_at <= '2009-03-13 14:45:33' ) LIMIT 1 FOR UPDATE
SQL (0.000000) [0]   Mysql::Error: MySQL server has gone away: ROLLBACK
SQL (0.000000) [0]   Mysql::Error: MySQL server has gone away: ROLLBACK

Signed-off-by: Hemant Kumar <gethemant@gmail.com>
  • Loading branch information...
1 parent a55973f commit fd50495230591e32c4bc5f0b2821a64f537ee96f @peteonrails peteonrails committed with Mar 16, 2009
Showing with 5 additions and 0 deletions.
  1. +5 −0 lib/backgroundrb/bdrb_job_queue.rb
@@ -5,6 +5,7 @@ class BdrbJobQueue < ActiveRecord::Base
# find next task from the table
def self.find_next(worker_name,worker_key = nil)
returned_job = nil
+ ActiveRecord::Base.verify_active_connections!
transaction do
unless worker_key
#use ruby time stamps for time calculations as db might have different times than what is calculated by ruby/rails
@@ -25,6 +26,7 @@ def self.find_next(worker_name,worker_key = nil)
# release a job and mark it to be unfinished and free.
# useful, if inside a worker, processing of this job failed and you want it to process later
def release_job
+ ActiveRecord::Base.verify_active_connections!
self.class.transaction do
self.taken = 0
self.started_at = nil
@@ -34,6 +36,7 @@ def release_job
# insert a new job for processing. jobs added will be automatically picked by the appropriate worker
def self.insert_job(options = { })
+ ActiveRecord::Base.verify_active_connections!
transaction do
options.merge!(:submitted_at => Time.now.utc,:finished => 0,:taken => 0)
t_job = new(options)
@@ -43,6 +46,7 @@ def self.insert_job(options = { })
# remove a job from table
def self.remove_job(options = { })
+ ActiveRecord::Base.verify_active_connections!
transaction do
t_job_id = find(:first, :conditions => options.merge(:finished => 0,:taken => 0),:lock => true)
delete(t_job_id)
@@ -51,6 +55,7 @@ def self.remove_job(options = { })
# Mark a job as finished
def finish!
+ ActiveRecord::Base.verify_active_connections!
self.class.transaction do
self.finished = 1
self.finished_at = Time.now.utc

0 comments on commit fd50495

Please sign in to comment.