class SayWhen::Storage::ActiveRecordStrategy::Job
Public Class Methods
acquire_next(no_later_than = nil)
click to toggle source
# 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_connection()
click to toggle source
# File lib/say_when/storage/active_record_strategy.rb, line 95 def self.check_connection if ActiveRecord::Base.respond_to?(:connection_handler) && ActiveRecord::Base.connection_handler ActiveRecord::Base.connection_handler.clear_active_connections! elsif 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)
click to toggle source
# 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_logging() { || ... }
click to toggle source
# File lib/say_when/storage/active_record_strategy.rb, line 105 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)
click to toggle source
# 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)
click to toggle source
# 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
Public Instance Methods
execute()
click to toggle source
default impl with some error handling and result recording
# File lib/say_when/storage/active_record_strategy.rb, line 136 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_result()
click to toggle source
# File lib/say_when/storage/active_record_strategy.rb, line 152 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)
click to toggle source
Calls superclass method
SayWhen::Storage::BaseJob#fired
# File lib/say_when/storage/active_record_strategy.rb, line 121 def fired(fired_at = Time.now) self.class.transaction do super save! end end
release()
click to toggle source
Calls superclass method
SayWhen::Storage::BaseJob#release
# File lib/say_when/storage/active_record_strategy.rb, line 128 def release self.class.transaction do super save! end end
set_defaults()
click to toggle source
# File lib/say_when/storage/active_record_strategy.rb, line 116 def set_defaults self.status = STATE_WAITING self.next_fire_at = trigger.next_fire_at end