Skip to content
Browse files

When a job starts it will now check to whether the database connectio…

…n is still alive. This is necessary if the database used for the delayed_job table is different from the database for the rest of the app.
  • Loading branch information...
1 parent c5ccf8b commit b73a7e561b89fc09ae77bb9c1cfc680062867e19 @bracken bracken committed Mar 2, 2010
Showing with 19 additions and 0 deletions.
  1. +19 −0 lib/delayed/worker.rb
View
19 lib/delayed/worker.rb
@@ -38,6 +38,7 @@ def initialize(options={})
@queue = options[:queue] || self.class.queue
self.class.min_priority = options[:min_priority] if options.has_key?(:min_priority)
self.class.max_priority = options[:max_priority] if options.has_key?(:max_priority)
+ @already_retried = false
end
# Every worker has a unique name which by default is the pid of the process. There are some
@@ -106,6 +107,7 @@ def work_off(num = 100)
end
def run(job)
+ self.ensure_db_connection
runtime = Benchmark.realtime do
Timeout.timeout(self.class.max_run_time.to_i) { job.invoke_job }
job.destroy
@@ -163,5 +165,22 @@ def reserve_and_run_one_job
run(job) if job
end
+
+ # Makes a dummy call to the database to make sure we're still connected
+ def ensure_db_connection
+ begin
+ ActiveRecord::Base.connection.execute("select 'I am alive'")
+ rescue ActiveRecord::StatementInvalid
+ ActiveRecord::Base.connection.reconnect!
+ unless @already_retried
+ @already_retried = true
+ retry
+ end
+ raise
+ else
+ @already_retried = false
+ end
+ end
+
end
end

0 comments on commit b73a7e5

Please sign in to comment.
Something went wrong with that request. Please try again.