Class: Delayed::Backend::CouchRest::Job

Inherits:
CouchRest::ExtendedDocument
  • Object
show all
Includes:
Base
Defined in:
lib/delayed/backend/couch_rest.rb

Constant Summary

Constants included from Base

Base::ParseObjectFromYaml

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#failed?, included, #invoke_job, #name, #payload_object, #payload_object=, #unlock

Class Method Details

.clear_locks!(worker_name) ⇒ Object



68
69
70
71
72
# File 'lib/delayed/backend/couch_rest.rb', line 68

def self.clear_locks!(worker_name)
  jobs = my_jobs worker_name
  jobs.each { |j| j.locked_by, j.locked_at = nil, nil; }
  database.bulk_save jobs
end

.db_time_nowObject



58
# File 'lib/delayed/backend/couch_rest.rb', line 58

def self.db_time_now; Time.now; end

.delete_allObject



73
74
75
# File 'lib/delayed/backend/couch_rest.rb', line 73

def self.delete_all
  database.bulk_save all.each { |doc| doc['_deleted'] = true }
end

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



59
60
61
62
63
64
65
66
67
# File 'lib/delayed/backend/couch_rest.rb', line 59

def self.find_available(worker_name, limit = 5, max_run_time = ::Delayed::Worker.max_run_time)
  ready = ready_jobs
  mine = my_jobs worker_name
  expire = expired_jobs max_run_time
  jobs = (ready + mine + expire)[0..limit-1].sort_by { |j| j.priority }
  jobs = jobs.find_all { |j| j.priority >= Worker.min_priority } if Worker.min_priority
  jobs = jobs.find_all { |j| j.priority <= Worker.max_priority } if Worker.max_priority
  jobs
end

Instance Method Details

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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/delayed/backend/couch_rest.rb', line 77

def lock_exclusively!(max_run_time, worker = worker_name)
  return false if locked_by_other?(worker) and not expired?(max_run_time)
  case
  when locked_by_me?(worker)
    self.locked_at = self.class.db_time_now
  when (unlocked? or (locked_by_other?(worker) and expired?(max_run_time)))
    self.locked_at, self.locked_by = self.class.db_time_now, worker
  end
  save
rescue RestClient::Conflict
  false
end