Method: Notable.track_job

Defined in:
lib/notable.rb

.track_job(job, job_id, queue, created_at, slow_job_threshold = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/notable.rb', line 68

def self.track_job(job, job_id, queue, created_at, slow_job_threshold = nil)
  slow_job_threshold ||= Notable.slow_job_threshold
  exception = nil
  notes = nil
  started_at = Time.now # wall time
  start_time = monotonic_time
  begin
    yield
  rescue Exception => e
    exception = e
    track_error(e)
  ensure
    notes = Notable.notes
    Notable.clear_notes
  end
  runtime = monotonic_time - start_time

  Safely.safely do
    notes << {note_type: "Slow Job"} if runtime > slow_job_threshold

    if notes.any?
      created_at = Time.parse(created_at) if created_at.is_a?(String)
      queued_time = created_at ? [started_at - created_at, 0].max : nil
    end

    notes.each do |note|
      data = {
        note_type: note[:note_type],
        note: note[:note],
        job: job,
        job_id: job_id,
        queue: queue,
        runtime: runtime,
        queued_time: queued_time
      }

      Notable.track_job_method.call(data)
    end
  end

  raise exception if exception
end