Skip to content

Commit

Permalink
Backport to older version of ActiveRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
betamatt committed Aug 3, 2011
1 parent 3d35952 commit a091820
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion delayed_job_active_record.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.test_files = Dir.glob('spec/**/*')

s.add_runtime_dependency 'activerecord', '~> 3.0'
s.add_runtime_dependency 'activerecord', '> 2.1.0'
s.add_runtime_dependency 'delayed_job', '3.0.0.pre'

s.add_development_dependency 'rspec', '~> 2.0'
Expand Down
23 changes: 18 additions & 5 deletions lib/delayed/backend/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_record'
require 'active_record/version'
module Delayed
module Backend
module ActiveRecord
Expand All @@ -8,11 +10,22 @@ class Job < ::ActiveRecord::Base
set_table_name :delayed_jobs

before_save :set_default_run_at

scope :ready_to_run, lambda {|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])
}
scope :by_priority, order('priority ASC, run_at ASC')

def self.rails3?
::ActiveRecord::VERSION::MAJOR == 3
end

if rails3?
scope :ready_to_run, lambda{|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)
}
scope :by_priority, order('priority ASC, run_at ASC')
else
named_scope :ready_to_run, lambda {|worker_name, max_run_time|
{ :conditions => ['(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] }
}
named_scope :by_priority, :order => 'priority ASC, run_at ASC'
end

def self.before_fork
::ActiveRecord::Base.clear_all_connections!
Expand Down

0 comments on commit a091820

Please sign in to comment.