Class: Delayed::Backend::DataMapper::Job

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource, Base
Defined in:
lib/delayed/backend/data_mapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_locks!(worker_name) ⇒ Object

When a worker is exiting, make sure we don’t have any locked jobs.



45
46
47
# File 'lib/delayed/backend/data_mapper.rb', line 45

def self.clear_locks!(worker_name)
  all(:locked_by => worker_name).update(:locked_at => nil, :locked_by => nil)
end

.db_time_nowObject



20
21
22
# File 'lib/delayed/backend/data_mapper.rb', line 20

def self.db_time_now
  Time.now
end

.delete_allObject

these are common to the other backends, so we provide an implementation



76
77
78
# File 'lib/delayed/backend/data_mapper.rb', line 76

def self.delete_all
  Delayed::Job.auto_migrate!
end

.find(id) ⇒ Object



80
81
82
# File 'lib/delayed/backend/data_mapper.rb', line 80

def self.find id
  get id
end

.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/delayed/backend/data_mapper.rb', line 24

def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
  
  simple_conditions = { :run_at.lte => db_time_now, :limit => limit, :failed_at => nil, :order => [:priority.asc, :run_at.asc]  }

  # respect priorities
  simple_conditions[:priority.gte] = Worker.min_priority if Worker.min_priority
  simple_conditions[:priority.lte] = Worker.max_priority if Worker.max_priority
  
  # lockable 
  lockable = (
    # not locked or past the max time
    ( all(:locked_at => nil ) | all(:locked_at.lt => db_time_now - max_run_time)) |

    # OR locked by our worker
    all(:locked_by => worker_name))
    
  # plus some other boring junk 
  (lockable).all( simple_conditions )
end

Instance Method Details

#lock_exclusively!(max_run_time, worker = worker_name) ⇒ Object

Lock this job for this worker. Returns true if we have the lock, false otherwise.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/delayed/backend/data_mapper.rb', line 51

def lock_exclusively!(max_run_time, worker = worker_name)

  now = self.class.db_time_now
  overtime = now - max_run_time
  
  # FIXME - this is a bit gross
  # DM doesn't give us the number of rows affected by a collection update
  # so we have to circumvent some niceness in DM::Collection here
  collection = locked_by != worker ?
    (self.class.all(:id => id, :run_at.lte => now) & ( self.class.all(:locked_at => nil) | self.class.all(:locked_at.lt => overtime) ) ) :
    self.class.all(:id => id, :locked_by => worker)
  
  attributes = collection.model.new(:locked_at => now, :locked_by => worker).dirty_attributes
  affected_rows = self.repository.update(attributes, collection)
    
  if affected_rows == 1
    self.locked_at = now
    self.locked_by = worker
    return true
  else
    return false
  end
end

#update_attributes(attributes) ⇒ Object



84
85
86
87
88
89
# File 'lib/delayed/backend/data_mapper.rb', line 84

def update_attributes(attributes)
  attributes.each do |k,v|
    self[k] = v
  end
  self.save
end