Sha256: 896701bcc6c555aaa3cfe163df5a4d39a3238b129ed68de4d95a065d105c5fbf
Contents?: true
Size: 1.73 KB
Versions: 2
Compression:
Stored size: 1.73 KB
Contents
module ActiveJobLog class Job < ApplicationRecord extend Enumerize STATUSES = %i{queued pending finished failed} validates :job_id, presence: true enumerize :status, in: STATUSES, scope: true serialize :params, Array serialize :stack_trace, Array before_save :set_queued_duration before_save :set_execution_duration before_save :set_total_duration def self.update_job!(job_id, status, params = {}) params.merge!(status_to_params(status)) job = find_or_create_job(job_id) job.update_attributes!(params) job end class << self private def find_or_create_job(job_id) Job.where(job_id: job_id).where.not(status: :failed).last || Job.create(job_id: job_id) end def status_to_params(status) time_attr = infer_duration_attr_from_status(status) { time_attr => DateTime.current, status: status } end def infer_duration_attr_from_status(status) case status when :queued :queued_at when :pending :started_at when :finished, :failed :ended_at else fail "invalid status" end end end private def set_queued_duration return if queued_at.blank? || started_at.blank? self.queued_duration = (started_at.to_f - queued_at.to_f).to_i end def set_execution_duration return if started_at.blank? || ended_at.blank? self.execution_duration = (ended_at.to_f - started_at.to_f).to_i end def set_total_duration from = queued_at || started_at return if from.blank? || ended_at.blank? self.total_duration = (ended_at.to_f - from.to_f).to_i end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
active_job_log-0.2.1 | app/models/active_job_log/job.rb |
active_job_log-0.2.0 | app/models/active_job_log/job.rb |