Class: SayWhen::Storage::ActiveRecordStrategy::Job

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BaseJob
Defined in:
lib/say_when/storage/active_record_strategy.rb

Constant Summary

Constants included from BaseJob

BaseJob::STATE_ACQUIRED, BaseJob::STATE_COMPLETE, BaseJob::STATE_ERROR, BaseJob::STATE_EXECUTING, BaseJob::STATE_WAITING

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseJob

#execute_job, #get_task, #load_trigger, #lock, #trigger

Class Method Details

.acquire_next(no_later_than = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/say_when/storage/active_record_strategy.rb', line 67

def self.acquire_next(no_later_than = nil)
  next_job = nil
  no_later_than = (no_later_than || Time.now).in_time_zone('UTC')

  check_connection
  hide_logging do
    SayWhen::Storage::ActiveRecordStrategy::Job.transaction do
      # select and lock the next job that needs executing (status waiting, and after no_later_than)
      next_job = where(status: STATE_WAITING)
                 .where('next_fire_at < ?', no_later_than)
                 .order(next_fire_at: 'asc')
                 .lock(true)
                 .first

      # set status to acquired to take it out of rotation
      next_job&.update_attribute(:status, STATE_ACQUIRED)
    end
  end
  next_job
end

.check_connectionObject



95
96
97
98
99
100
101
# File 'lib/say_when/storage/active_record_strategy.rb', line 95

def self.check_connection
  if ActiveRecord::Base.respond_to?(:clear_active_connections!)
    ActiveRecord::Base.clear_active_connections!
  elsif ActiveRecord::Base.respond_to?(:verify_active_connections!)
    ActiveRecord::Base.verify_active_connections!
  end
end

.find_named_job(group, name) ⇒ Object



63
64
65
# File 'lib/say_when/storage/active_record_strategy.rb', line 63

def self.find_named_job(group, name)
  group && name && where(name: name, group: group).first
end

.hide_loggingObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/say_when/storage/active_record_strategy.rb', line 103

def self.hide_logging
  @_null_logger ||= Logger.new(IO::NULL)
  old_logger = ::ActiveRecord::Base.logger
  begin
    ::ActiveRecord::Base.logger = @_null_logger
    yield
  ensure
    ::ActiveRecord::Base.logger = old_logger
  end
end

.job_create(job) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/say_when/storage/active_record_strategy.rb', line 55

def self.job_create(job)
  if existing_job = find_named_job(job[:group], job[:name])
    existing_job.tap { |j| j.update(job) }
  else
    create(job)
  end
end

.reset_acquired(older_than_seconds) ⇒ Object



88
89
90
91
92
93
# File 'lib/say_when/storage/active_record_strategy.rb', line 88

def self.reset_acquired(older_than_seconds)
  return unless older_than_seconds.to_i > 0

  older_than = (Time.now - older_than_seconds.to_i)
  where('status = ? and updated_at < ?', STATE_ACQUIRED, older_than).update_all(status: STATE_WAITING)
end

Instance Method Details

#executeObject

default impl with some error handling and result recording



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/say_when/storage/active_record_strategy.rb', line 134

def execute
  result = nil
  if SayWhen.options[:store_executions]
    result = execute_with_stored_result
  else
    begin
      result = execute_job(data)
      SayWhen.logger.info("complete - job: #{inspect}, result: #{result}")
    rescue Object => e
      result = "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
      SayWhen.logger.error("error - job: #{inspect}, exception: #{result}")
    end
  end
  result
end

#execute_with_stored_resultObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/say_when/storage/active_record_strategy.rb', line 150

def execute_with_stored_result
  execution = JobExecution.create(job: self, status: STATE_EXECUTING, start_at: Time.now)

  begin
    execution.result = execute_job(data)
    execution.status = 'complete'
  rescue Object => e
    execution.result = "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
    execution.status = 'error'
  end

  execution.end_at = Time.now
  execution.save!

  execution.result
end

#fired(fired_at = Time.now) ⇒ Object



119
120
121
122
123
124
# File 'lib/say_when/storage/active_record_strategy.rb', line 119

def fired(fired_at = Time.now)
  self.class.transaction do
    super
    save!
  end
end

#releaseObject



126
127
128
129
130
131
# File 'lib/say_when/storage/active_record_strategy.rb', line 126

def release
  self.class.transaction do
    super
    save!
  end
end

#set_defaultsObject



114
115
116
117
# File 'lib/say_when/storage/active_record_strategy.rb', line 114

def set_defaults
  self.status = STATE_WAITING
  self.next_fire_at = trigger.next_fire_at
end