Class: RailsErrorDashboard::AddIssueRecurrenceCommentJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/rails_error_dashboard/add_issue_recurrence_comment_job.rb

Overview

Background job to add a recurrence comment on a linked issue.

Triggered via :on_error_recurred plugin hook. Throttled: max 1 comment per hour per error to prevent spam on high-frequency errors.

Constant Summary collapse

THROTTLE_INTERVAL =

1 hour

3600
@@last_comment_at =

Track last comment time per error to throttle

{}

Instance Method Summary collapse

Instance Method Details

#perform(error_log_id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/jobs/rails_error_dashboard/add_issue_recurrence_comment_job.rb', line 20

def perform(error_log_id)
  # Throttle: max 1 comment per hour per error
  if throttled?(error_log_id)
    Rails.logger.debug("[RailsErrorDashboard] Skipping recurrence comment for error #{error_log_id} — throttled")
    return
  end

  error = ErrorLog.find(error_log_id)
  return unless error.external_issue_url.present? && error.external_issue_number.present?

  client = Services::IssueTrackerClient.from_config
  return unless client

  comment = "Error occurred again (#{error.occurrence_count} total occurrences).\n\n"
  comment += "- **Last seen:** #{error.last_seen_at&.utc&.strftime("%Y-%m-%d %H:%M:%S UTC")}\n"
  comment += "- **First seen:** #{error.first_seen_at&.utc&.strftime("%Y-%m-%d %H:%M:%S UTC")}"
  comment += "\n\n---\n*[RED](https://github.com/AnjanJ/rails_error_dashboard) (Rails Error Dashboard)*"

  result = client.add_comment(number: error.external_issue_number, body: comment)

  if result[:success]
    record_comment(error_log_id)
    Rails.logger.info("[RailsErrorDashboard] Added recurrence comment on issue ##{error.external_issue_number}")
  else
    Rails.logger.error("[RailsErrorDashboard] Failed to add recurrence comment: #{result[:error]}")
  end
rescue ActiveRecord::RecordNotFound
  # Error was deleted
rescue => e
  Rails.logger.error("[RailsErrorDashboard] AddIssueRecurrenceCommentJob failed: #{e.class}: #{e.message}")
  raise # retry
end