Module: ActiveJobLog::Loggeable::ClassMethods

Defined in:
lib/active_job_log/loggeable.rb

Instance Method Summary collapse

Instance Method Details

#find_or_create_job(job_id) ⇒ Object



49
50
51
# File 'lib/active_job_log/loggeable.rb', line 49

def find_or_create_job(job_id)
  where(job_id: job_id).where.not(status: :failed).last || create(job_id: job_id)
end

#infer_duration_attr_from_status(status) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_job_log/loggeable.rb', line 61

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

#status_to_params(status) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/active_job_log/loggeable.rb', line 53

def status_to_params(status)
  time_attr = infer_duration_attr_from_status(status)
  {
    time_attr => DateTime.current,
    status: status
  }
end

#update_job!(job_id, status, params = {}) ⇒ Object



42
43
44
45
46
47
# File 'lib/active_job_log/loggeable.rb', line 42

def update_job!(job_id, status, params = {})
  params.merge!(status_to_params(status))
  job = find_or_create_job(job_id)
  job.update!(params)
  job
end