Class: PagerDuty::PollMaintenanceWindows

Inherits:
Object
  • Object
show all
Includes:
SentryLogging, Sidekiq::Job
Defined in:
app/sidekiq/pager_duty/poll_maintenance_windows.rb

Constant Summary collapse

MESSAGE_INDICATOR =
'USER_MESSAGE:'

Instance Method Summary collapse

Methods included from SentryLogging

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

Instance Method Details

#parse_user_message(raw_description) ⇒ Object



13
14
15
# File 'app/sidekiq/pager_duty/poll_maintenance_windows.rb', line 13

def parse_user_message(raw_description)
  raw_description.partition(MESSAGE_INDICATOR)[2].strip
end

#performObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/sidekiq/pager_duty/poll_maintenance_windows.rb', line 17

def perform
  client = PagerDuty::MaintenanceClient.new
  pd_windows = client.get_all

  # Add or update-in-place any open PagerDuty maintenance windows
  pd_windows.each do |pd_win|
    pd_win[:description] = parse_user_message(pd_win[:description])
    window = MaintenanceWindow.find_or_initialize_by(pagerduty_id: pd_win[:pagerduty_id],
                                                     external_service: pd_win[:external_service])
    window.update(pd_win)
  end

  # Delete any existing records that are not present in the PagerDuty API results
  # These indicate deleted maintenance windows so we want to stop reporting them
  open_ids = pd_windows.pluck(:pagerduty_id)
  MaintenanceWindow.end_after(Time.zone.now).each do |api_win|
    api_win.delete unless open_ids.include?(api_win.pagerduty_id)
  end
rescue Common::Exceptions::BackendServiceException, Common::Client::Errors::ClientError => e
  log_exception_to_sentry(e)
end