Class: Appydays::Loggable::SidekiqJobLogger

Inherits:
Sidekiq::JobLogger
  • Object
show all
Includes:
Configurable, Appydays::Loggable
Defined in:
lib/appydays/loggable/sidekiq_job_logger.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Appydays::Loggable

[], configure_12factor, default_level=, ensure_stderr_appender, included, set_default_level, with_log_tags

Methods included from Configurable

included

Class Method Details

.death_handler(job, ex) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 111

def self.death_handler(job, ex)
  self.logger.error(
    "job_retries_exhausted",
    {
      job_class: job["class"],
      job_args: job["args"],
      job_retry: job["retry"],
      job_queue: job["queue"],
      job_dead: job["dead"],
      job_id: job["jid"],
      job_created_at: job["created_at"],
      job_enqueued_at: job["enqueued_at"],
      job_error_message: job["error_message"],
      job_error_class: job["error_class"],
      job_failed_at: job["failed_at"],
      job_retry_count: job["retry_count"],
    },
    ex,
  )
end

.error_handler(ex, ctx) ⇒ Object



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/appydays/loggable/sidekiq_job_logger.rb', line 71

def self.error_handler(ex, ctx)
  # ctx looks like:
  # {
  # :context=>"Job raised exception",
  # :job=>
  #  {"class"=>"App::Async::FailingJobTester",
  #   "args"=>
  #    [{"id"=>"e8e03571-9851-4daa-a801-a0b43282f317",
  #      "name"=>"app.test_failing_job",
  #      "payload"=>[true]}],
  #   "retry"=>true,
  #   "queue"=>"default",
  #   "jid"=>"cb00c4fe9b2f16b72797d35c",
  #   "created_at"=>1567811837.798969,
  #   "enqueued_at"=>1567811837.79901},
  # :jobstr=>
  #  "{\"class\":\"App::Async::FailingJobTester\", <etc>"
  # }
  job = ctx[:job]
  # If there was a connection error, you may end up with no job context.
  # It's very difficult to test this usefully, so it's not tested.
  unless job
    self.logger.error("job_error_no_job", {}, ex)
    return
  end
  self.logger.error(
    "job_error",
    {
      job_class: job["class"],
      job_args: job["args"],
      job_retry: job["retry"],
      job_queue: job["queue"],
      job_id: job["jid"],
      job_created_at: job["created_at"],
      job_enqueued_at: job["enqueued_at"],
    },
    ex,
  )
end

.job_tagsObject



69
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 69

def self.job_tags = Thread.current[:appydays_sidekiq_job_logger_job_tags] || {}

.set_job_tags(tags) ⇒ Object

Set job tags that get logged out in the “job_done” and “job_fail” messages. See README for more info. We do NOT merge the job_tags in with critical errors (death and job_error), since those will log the job args, and they aren’t properly tested right now. We may add support in the future.



64
65
66
67
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 64

def self.set_job_tags(tags)
  Thread.current[:appydays_sidekiq_job_logger_job_tags] ||= {}
  Thread.current[:appydays_sidekiq_job_logger_job_tags].merge!(tags)
end

Instance Method Details

#call(item, _queue) ⇒ Object



23
24
25
26
27
28
# File 'lib/appydays/loggable/sidekiq_job_logger.rb', line 23

def call(item, _queue, &)
  start = self.now
  self.with_log_tags(job_id: item["jid"]) do
    self.call_inner(item, start, &)
  end
end