Skip to content

Commit

Permalink
Use ActiveRecord::Base.establish_connection to reconnect after forking
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Sep 14, 2010
1 parent a92ee5c commit 0b866f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/delayed/backend/active_record.rb
Expand Up @@ -2,7 +2,7 @@

class ActiveRecord::Base
yaml_as "tag:ruby.yaml.org,2002:ActiveRecord"

def self.yaml_new(klass, tag, val)
klass.find(val['attributes']['id'])
rescue ActiveRecord::RecordNotFound
Expand All @@ -22,7 +22,7 @@ module ActiveRecord
class Job < ::ActiveRecord::Base
include Delayed::Backend::Base
set_table_name :delayed_jobs

before_save :set_default_run_at

scope :ready_to_run, lambda {|worker_name, max_run_time|
Expand All @@ -31,7 +31,7 @@ class Job < ::ActiveRecord::Base
scope :by_priority, order('priority ASC, run_at ASC')

def self.after_fork
::ActiveRecord::Base.connection.reconnect!
::ActiveRecord::Base.establish_connection
end

# When a worker is exiting, make sure we don't have any locked jobs.
Expand All @@ -44,7 +44,7 @@ def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_ti
scope = self.ready_to_run(worker_name, max_run_time)
scope = scope.scoped(:conditions => ['priority >= ?', Worker.min_priority]) if Worker.min_priority
scope = scope.scoped(:conditions => ['priority <= ?', Worker.max_priority]) if Worker.max_priority

::ActiveRecord::Base.silence do
scope.by_priority.all(:limit => limit)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/active_record_job_spec.rb
Expand Up @@ -5,15 +5,15 @@
after do
Time.zone = nil
end

it_should_behave_like 'a delayed_job backend'

context "db_time_now" do
it "should return time in current time zone if set" do
Time.zone = 'Eastern Time (US & Canada)'
%w(EST EDT).should include(Delayed::Job.db_time_now.zone)
end

it "should return UTC time if that is the AR default" do
Time.zone = nil
ActiveRecord::Base.default_timezone = :utc
Expand All @@ -26,10 +26,10 @@
%w(CST CDT).should include(Delayed::Backend::ActiveRecord::Job.db_time_now.zone)
end
end

describe "after_fork" do
it "should call reconnect on the connection" do
ActiveRecord::Base.connection.should_receive(:reconnect!)
ActiveRecord::Base.should_receive(:establish_connection)
Delayed::Backend::ActiveRecord::Job.after_fork
end
end
Expand Down

0 comments on commit 0b866f9

Please sign in to comment.