Skip to content

Commit

Permalink
Rails 4 compatibility
Browse files Browse the repository at this point in the history
* also bumps the activerecord dependency requirement to be 3.0 or higher as delayed_job 3.x does not support activerecord 2.x
* because of the activerecord 3.0+ requirement change, the scoping done in backend/active_record.rb can be updated to use Rails 3 query methods
* also removes a spec that tested `attr_accessible` which is no longer part of Rails 4
  • Loading branch information
agrobbin committed Mar 2, 2013
1 parent a297748 commit 0cb4496
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 30 deletions.
2 changes: 1 addition & 1 deletion delayed_job_active_record.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8

Gem::Specification.new do |spec|
spec.add_dependency 'activerecord', ['>= 2.1.0', '< 4']
spec.add_dependency 'activerecord', ['>= 3.0', '< 4.1']
spec.add_dependency 'delayed_job', '~> 3.0'
spec.authors = ["Brian Ryckbost", "Matt Griffin", "Erik Michaels-Ober"]
spec.description = 'ActiveRecord backend for Delayed::Job, originally authored by Tobias Lütke'
Expand Down
20 changes: 7 additions & 13 deletions lib/delayed/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module ActiveRecord
class Job < ::ActiveRecord::Base
include Delayed::Backend::Base

attr_accessible :priority, :run_at, :queue, :payload_object,
:failed_at, :locked_at, :locked_by
scope :by_priority, lambda { order('priority ASC, run_at ASC') }

before_save :set_default_run_at

Expand All @@ -23,10 +22,6 @@ def self.ready_to_run(worker_name, max_run_time)
where('(run_at <= ? AND (locked_at IS NULL OR locked_at < ?) OR locked_by = ?) AND failed_at IS NULL', db_time_now, db_time_now - max_run_time, worker_name)
end

def self.by_priority
order('priority ASC, run_at ASC')
end

def self.before_fork
::ActiveRecord::Base.clear_all_connections!
end
Expand All @@ -37,22 +32,21 @@ def self.after_fork

# When a worker is exiting, make sure we don't have any locked jobs.
def self.clear_locks!(worker_name)
update_all("locked_by = null, locked_at = null", ["locked_by = ?", worker_name])
where(:locked_by => worker_name).update_all(:locked_by => nil, :locked_at => nil)
end

def self.reserve(worker, max_run_time = Worker.max_run_time)
# scope to filter to records that are "ready to run"
readyScope = self.ready_to_run(worker.name, max_run_time)

# scope to filter to the single next eligible job
nextScope = readyScope.scoped
nextScope = nextScope.scoped(:conditions => ['priority >= ?', Worker.min_priority]) if Worker.min_priority
nextScope = nextScope.scoped(:conditions => ['priority <= ?', Worker.max_priority]) if Worker.max_priority
nextScope = nextScope.scoped(:conditions => ["queue IN (?)", Worker.queues]) if Worker.queues.any?
nextScope = nextScope.scoped.by_priority.limit(1)
readyScope = readyScope.where('priority >= ?', Worker.min_priority) if Worker.min_priority
readyScope = readyScope.where('priority <= ?', Worker.max_priority) if Worker.max_priority
readyScope = readyScope.where(:queue => Worker.queues) if Worker.queues.any?
job = readyScope.by_priority.first

now = self.db_time_now
job = nextScope.first

return unless job
job.with_lock do
job.locked_at = now
Expand Down
15 changes: 0 additions & 15 deletions spec/delayed/backend/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,6 @@
end
end

context "ActiveRecord::Base.send(:attr_accessible, nil)" do
before do
Delayed::Backend::ActiveRecord::Job.send(:attr_accessible, nil)
end

after do
Delayed::Backend::ActiveRecord::Job.send(:attr_accessible, *Delayed::Backend::ActiveRecord::Job.new.attributes.keys)
end

it "is still accessible" do
job = Delayed::Backend::ActiveRecord::Job.enqueue :payload_object => EnqueueJobMod.new
expect(Delayed::Backend::ActiveRecord::Job.find(job.id).handler).to_not be_blank
end
end

context "ActiveRecord::Base.table_name_prefix" do
it "when prefix is not set, use 'delayed_jobs' as table name" do
::ActiveRecord::Base.table_name_prefix = nil
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Story < ActiveRecord::Base
self.primary_key = :story_id
def tell; text; end
def whatever(n, _); tell*n; end
default_scope where(:scoped => true)
default_scope { where(:scoped => true) }

handle_asynchronously :whatever
end
Expand Down

0 comments on commit 0cb4496

Please sign in to comment.