0
@@ -10,7 +10,7 @@ module Delayed
0
self.worker_name = "pid:#{Process.pid}"
0
- NextTaskSQL = '`run_at` <= ? AND (`locked_
until` IS NULL OR `locked_until` < ?) OR (`locked_by`=?)'
0
+ NextTaskSQL = '`run_at` <= ? AND (`locked_
at` IS NULL OR `locked_at` < ?) OR (`locked_by` = ?)'
0
NextTaskOrder = 'priority DESC, run_at ASC'
0
ParseObjectFromYaml = /\!ruby\/\w+\:([^\s]+)/
0
@@ -18,7 +18,7 @@ module Delayed
0
- connection.execute "UPDATE #{table_name} SET `locked_by`=NULL, `locked_
until`=NULL WHERE `locked_by`=#{quote_value(worker_name)}"
0
+ connection.execute "UPDATE #{table_name} SET `locked_by`=NULL, `locked_
at`=NULL WHERE `locked_by`=#{quote_value(worker_name)}"
0
@@ -57,13 +57,13 @@ module Delayed
0
# Get the payload of the next job we can get an exclusive lock on.
0
# If no jobs are left we return nil
0
- def self.reserve(
timeout = 4 * 60 * 60)
0
+ def self.reserve(
max_run_time = 4.hours)
0
# We get up to 5 jobs from the db. In face we cannot get exclusive access to a job we try the next.
0
# this leads to a more even distribution of jobs across the worker processes
0
find_available(5).each do |job|
0
- job.lock_exclusively!(
self.db_time_now + timeout, worker_name)
0
+ job.lock_exclusively!(
max_run_time, worker_name)
0
yield job.payload_object
0
@@ -81,24 +81,26 @@ module Delayed
0
# This method is used internally by reserve method to ensure exclusive access
0
# to the given job. It will rise a LockError if it cannot get this lock.
0
- def lock_exclusively!(lock_until, worker = worker_name)
0
+ def lock_exclusively!(max_run_time, worker = worker_name)
0
+ now = self.class.db_time_now
0
- affected_rows = if locked_by != worker
0
+ affected_rows = if locked_by != worker
0
- # We don't own this job so we will update the locked_by name and the locked_until
0
+ # We don't own this job so we will update the locked_by name and the locked_at
0
connection.update(<<-end_sql, "#{self.class.name} Update to aquire exclusive lock")
0
UPDATE #{self.class.table_name}
0
- SET `locked_until`=#{quote_value(lock_until)}, `locked_by`=#{quote_value(worker)}
0
- WHERE #{self.class.primary_key} = #{quote_value(id)} AND (`locked_until`<#{quote_value(self.class.db_time_now)} OR `locked_until` IS NULL)
0
+ SET `locked_at`=#{quote_value(now)}, `locked_by`=#{quote_value(worker)}
0
+ WHERE #{self.class.primary_key} = #{quote_value(id)} AND (`locked_at` IS NULL OR `locked_at` < #{quote_value(now + max_run_time)})
0
# We alrady own this job, this may happen if the job queue crashes.
0
- # Simply
update the lock timeout
0
+ # Simply
resume and update the locked_at
0
connection.update(<<-end_sql, "#{self.class.name} Update exclusive lock")
0
UPDATE #{self.class.table_name}
0
- SET `locked_
until`=#{quote_value(lock_until)}
0
+ SET `locked_
at`=#{quote_value(now)}
0
WHERE #{self.class.primary_key} = #{quote_value(id)} AND (`locked_by`=#{quote_value(worker)})
0
@@ -108,12 +110,12 @@ module Delayed
0
raise LockError, "Attempted to aquire exclusive lock failed"
0
- self.locked_
until = lock_until0
self.locked_by = worker
0
- self.locked_
until = nil
Comments
No one has commented yet.