Module: Sidekiq::Form526JobStatusTracker::JobTracker

Extended by:
ActiveSupport::Concern, BackupSubmission
Includes:
SentryLogging
Included in:
EVSS::DisabilityCompensationForm::JobStatus, EVSS::DisabilityCompensationForm::Metrics, Lighthouse::PollForm526Pdf
Defined in:
lib/sidekiq/form526_job_status_tracker/job_tracker.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

STATSD_KEY_PREFIX =
'worker.evss.submit_form526.job_status'

Instance Method Summary collapse

Methods included from BackupSubmission

send_backup_submission_if_enabled

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Instance Method Details

#error_message(error) ⇒ Object (private)



193
194
195
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 193

def error_message(error)
  error.try(:messages) ? error.messages.to_s : error.message
end

#job_successObject

Metrics and logging for when the job succeeds



113
114
115
116
117
118
119
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 113

def job_success
  upsert_job_status(Form526JobStatus::STATUS[:success])
  log_info('success')
  metrics.increment_success(@is_bdd, @service_provider)
rescue => e
  ::Rails.logger.error('error tracking job success', error: e, class: klass, trace: e.backtrace)
end

#job_tryObject

Metrics and logging for each Sidekiq try



103
104
105
106
107
108
109
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 103

def job_try
  upsert_job_status(Form526JobStatus::STATUS[:try])
  log_info('try')
  metrics.increment_try
rescue => e
  ::Rails.logger.error('error tracking job try', error: e, class: klass)
end

#klassObject (private)



216
217
218
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 216

def klass
  self.class.name.demodulize
end

#log_error(status, error) ⇒ Object (private)



206
207
208
209
210
211
212
213
214
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 206

def log_error(status, error)
  ::Rails.logger.error(@status_job_title,
                       'saved_claim_id' => @status_saved_claim_id,
                       'submission_id' => @status_submission_id,
                       'service_provider' => @service_provider,
                       'job_id' => jid,
                       'status' => status,
                       'error_message' => error)
end

#log_info(status) ⇒ Object (private)



197
198
199
200
201
202
203
204
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 197

def log_info(status)
  ::Rails.logger.info(@status_job_title,
                      'saved_claim_id' => @status_saved_claim_id,
                      'submission_id' => @status_submission_id,
                      'service_provider' => @service_provider,
                      'job_id' => jid,
                      'status' => status)
end

#metricsObject (private)



220
221
222
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 220

def metrics
  @metrics ||= Metrics.new(self.class::STATSD_KEY_PREFIX)
end

#non_retryable_error_handler(error) ⇒ Object

Metrics and logging for any non-retryable errors that occurred. Non-retryable errors will always fail, e.g. an ArgumentError in the job class



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 133

def non_retryable_error_handler(error)
  upsert_job_status(Form526JobStatus::STATUS[:non_retryable_error], error)
  error_class = error.class
  error_message = error.message
  JobTracker.send_backup_submission_if_enabled(form526_submission_id: @status_submission_id,
                                               job_class: klass, job_id: jid,
                                               error_class:, error_message:)
  log_exception_to_sentry(error, status: :non_retryable_error, jid:)
  log_error('non_retryable_error', error)
  metrics.increment_non_retryable(error, @is_bdd, @service_provider)
end

#retryable_error_handler(error) ⇒ Object

Metrics and logging for any retryable errors that occurred. Retryable are system recoverable, e.g. an upstream http timeout



124
125
126
127
128
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 124

def retryable_error_handler(error)
  upsert_job_status(Form526JobStatus::STATUS[:retryable_error], error)
  log_error('retryable_error', error)
  metrics.increment_retryable(error, @is_bdd, @service_provider)
end

#update_background_job_errors(job_id:, error_class:, error_message:, caller_method:, timestamp: Time.now.utc) ⇒ Object (private)



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 175

def update_background_job_errors(job_id:, error_class:, error_message:, caller_method:, timestamp: Time.now.utc)
  form_job_status = Form526JobStatus.find_by(job_id:)
  return unless form_job_status

  bgjob_errors = form_job_status.bgjob_errors || {}

  new_error = {
    "#{timestamp.to_i}": {
      caller_method:,
      error_class:,
      error_message:,
      timestamp:
    }
  }

  bgjob_errors.merge!(new_error)
end

#upsert_job_status(status, error = nil) ⇒ Object (private)



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 147

def upsert_job_status(status, error = nil)
  timestamp = Time.now.utc

  values = { form526_submission_id: @status_submission_id,
             job_id: jid,
             job_class: klass,
             status:,
             error_class: nil,
             error_message: nil,
             bgjob_errors: {},
             updated_at: timestamp }

  caller_method = caller[0][/`.*'/][1..-2]
  error_class = error.class if error
  error_message = error_message(error) if error
  values[:error_class] = error_class
  values[:error_message] = error_message

  values[:bgjob_errors] = update_background_job_errors(job_id: jid,
                                                       error_class:,
                                                       error_message:,
                                                       caller_method:,
                                                       timestamp:)
  # rubocop:disable Rails/SkipsModelValidations
  Form526JobStatus.upsert(values, unique_by: :job_id)
  # rubocop:enable Rails/SkipsModelValidations
end

#with_tracking(job_title, saved_claim_id, submission_id, is_bdd = nil, service_provider = nil) ⇒ Object

Code wrapped by this block will run between the #job_try and #job_success methods

Parameters:

  • job_title (String)

    Description of the job being run

  • saved_claim_id (Integer)

    The SavedClaim id

  • submission_id (Integer)
  • service_provider (String) (defaults to: nil)

    Either ‘lighthouse’ or ‘evss’



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sidekiq/form526_job_status_tracker/job_tracker.rb', line 82

def with_tracking(job_title, saved_claim_id, submission_id, is_bdd = nil, service_provider = nil)
  @status_job_title = job_title
  @status_saved_claim_id = saved_claim_id
  @status_submission_id = submission_id
  @is_bdd = is_bdd
  @service_provider = service_provider

  job_try
  begin
    yield
  rescue
    exception_raised = true
    raise
  ensure
    job_status = Form526JobStatus.find_by(job_id: jid)
    job_success unless exception_raised || job_status&.error_class
  end
end