Module: Delayed::Backend::Sequel::LazyJob

Defined in:
lib/delayed/backend/sequel.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



43
44
45
# File 'lib/delayed/backend/sequel.rb', line 43

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#==(other_job) ⇒ Object



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

def ==(other_job)
  id == other_job.id
end

#before_createObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/delayed/backend/sequel.rb', line 84

def before_create
  self.last_error ||= nil
  self.updated_at ||= Time.now
  self.locked_at  ||= nil
  self.locked_by  ||= nil
  self.failed_at  ||= nil
  self.attempts   ||= 0
  self.created_at ||= Time.now
  self.priority   ||= 0
end

#before_saveObject



95
96
97
98
# File 'lib/delayed/backend/sequel.rb', line 95

def before_save
  set_default_run_at
  super
end

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

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/delayed/backend/sequel.rb', line 102

def lock_exclusively!(max_run_time, worker = worker_name)
  right_now     = self.class.db_time_now
  overtime      = right_now - max_run_time.to_i

  affected_rows = if locked_by != worker
                    filter = "id = #{id} and (locked_at is null or locked_at < '#{make_db_timestamp(overtime)}') and (run_at <= '#{make_db_timestamp(right_now)}')"
                    Job.filter(filter).update(:locked_at => right_now, :locked_by => worker)
                  else
                    filter = "id = #{id} and locked_by = '#{worker}'"
                    Job.filter(filter).update(:locked_at => right_now)
                  end

  if affected_rows == 1
    self.locked_at = right_now
    self.locked_by = worker
    return true
  else
    return false
  end
end