Permalink
Browse files

Merge branch 'master' into backends

* master:
  Reverse priority so the jobs table can be indexed. Lower numbers have higher priority. The default priority is 0, so increase it for jobs that are not important.
  Make Worker#work_off public so it can be called in tests

Conflicts:
	lib/delayed/job.rb
	spec/job_spec.rb
  • Loading branch information...
2 parents 6c2abd7 + c743446 commit 72dd09946afa389a1c7992215672f21f8ca041aa @bkeepers bkeepers committed Jan 12, 2010
@@ -11,7 +11,8 @@ def self.up
table.string :locked_by # Who is working on this object (if locked)
table.timestamps
end
-
+
+ add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
end
def self.down
@@ -12,7 +12,7 @@ class Job < ::ActiveRecord::Base
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 DESC, run_at ASC'
+ named_scope :by_priority, :order => 'priority ASC, run_at ASC'
# When a worker is exiting, make sure we don't have any locked jobs.
def self.clear_locks!(worker_name)
View
@@ -81,6 +81,26 @@ def start
Delayed::Job.clear_locks!(name)
end
+ # Do num jobs and return stats on success/failure.
+ # Exit early if interrupted.
+ def work_off(num = 100)
+ success, failure = 0, 0
+
+ num.times do
+ case reserve_and_run_one_job
+ when true
+ success += 1
+ when false
+ failure += 1
+ else
+ break # leave if no work could be done
+ end
+ break if $exit # leave if we're exiting
+ end
+
+ return [success, failure]
+ end
+
def run(job)
runtime = Benchmark.realtime do
Timeout.timeout(self.class.max_run_time.to_i) { job.invoke_job }
@@ -139,25 +159,5 @@ def reserve_and_run_one_job
run(job) if job
end
-
- # Do num jobs and return stats on success/failure.
- # Exit early if interrupted.
- def work_off(num = 100)
- success, failure = 0, 0
-
- num.times do
- case reserve_and_run_one_job
- when true
- success += 1
- when false
- failure += 1
- else
- break # leave if no work could be done
- end
- break if $exit # leave if we're exiting
- end
-
- return [success, failure]
- end
end
end
@@ -188,7 +188,7 @@ def create_job(opts = {})
jobs = @backend.find_available('worker', 10)
ordered = true
jobs[1..-1].each_index{ |i|
- if (jobs[i].priority < jobs[i+1].priority)
+ if (jobs[i].priority > jobs[i+1].priority)
ordered = false
break
end

0 comments on commit 72dd099

Please sign in to comment.