Class: Minicron::Alert

Inherits:
Object
  • Object
show all
Defined in:
lib/minicron/alert.rb

Overview

Allows the sending of alerts via multiple mediums

Instance Method Summary collapse

Instance Method Details

#send(options = {}) ⇒ Object

Send an individual alert

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • kind (String)

    'fail' or 'miss'

  • schedule_id (Integer, nil)

    only applies to 'miss' alerts

  • execution_id (Integer, nil)

    only used by 'fail' alerts

  • job_id (Integer)

    used to look up the job name for the alert message

  • expected_at (Time)

    when the schedule was expected to execute

  • medium (String)

    the medium to send the alert via


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/minicron/alert.rb', line 41

def send(options = {})
  # Look up the job for this schedule
  options[:job] = Minicron::Hub::Job.find(options[:job_id])

  # Switch the medium that the alert will be sent via
  case options[:medium]
  when 'email'
    send_email(options)
  when 'sms'
    send_sms(options)
  when 'pagerduty'
    send_pagerduty(options)
  else
    fail Exception, "The medium '#{options[:medium]}' is not supported!"
  end

  # Store that we sent the alert
  Minicron::Hub::Alert.create(
    :schedule_id => options[:schedule_id],
    :execution_id => options[:execution_id],
    :kind => options[:kind],
    :expected_at => options[:expected_at],
    :medium => options[:medium],
    :sent_at => Time.now.utc
  )
end

#send_all(options = {}) ⇒ Object

Send an alert using all enabled mediums

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • kind (String)

    'fail' or 'miss'

  • schedule_id (Integer, nil)

    only applies to 'miss' alerts

  • execution_id (Integer, nil)

    only used by 'fail' alerts

  • job_id (Integer)

    used by the #send method

  • expected_at (Time)

    only applies to 'miss' alerts


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/minicron/alert.rb', line 17

def send_all(options = {})
  Minicron.config['alerts'].each do |medium, value|
    # Check if the medium is enabled and alert hasn't already been sent
    if value['enabled'] && !sent?(options)
      send(
        :kind => options[:kind],
        :schedule_id => options[:schedule_id],
        :execution_id => options[:execution_id],
        :job_id => options[:job_id],
        :expected_at => options[:expected_at],
        :medium => medium
      )
    end
  end
end

#send_email(options = {}) ⇒ Object

Send an email alert, this has the same options as #send


69
70
71
72
73
74
75
76
77
# File 'lib/minicron/alert.rb', line 69

def send_email(options = {})
  email = Minicron::Email.new
  email.send(
    Minicron.config['alerts']['email']['from'],
    Minicron.config['alerts']['email']['to'],
    "minicron alert for job '#{options[:job].name}'!",
    email.get_message(options)
  )
end

#send_pagerduty(options = {}) ⇒ Object

Send a pagerduty alert, this has the same options as #send


90
91
92
93
94
95
96
# File 'lib/minicron/alert.rb', line 90

def send_pagerduty(options = {})
  pagerduty = Minicron::PagerDuty.new
  pagerduty.send(
    options[:kind] == 'fail' ? 'Job failed!' : 'Job missed!',
    pagerduty.get_message(options)
  )
end

#send_sms(options = {}) ⇒ Object

Send an sms alert, this has the same options as #send


80
81
82
83
84
85
86
87
# File 'lib/minicron/alert.rb', line 80

def send_sms(options = {})
  sms = Minicron::SMS.new
  sms.send(
    Minicron.config['alerts']['sms']['from'],
    Minicron.config['alerts']['sms']['to'],
    sms.get_message(options)
  )
end

#sent?(options = {}) ⇒ Boolean

Queries the database to determine if an alert for this kind has already been sent

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • kind (String)

    'fail' or 'miss'

  • schedule_id (Integer, nil)

    only applies to 'miss' alerts

  • execution_id (Integer, nil)

    only used by 'fail' alerts

  • expected_at (Time)

    when the schedule was expected to execute

  • medium (String)

    the medium to send the alert via

Returns:

  • (Boolean)

106
107
108
109
110
111
112
113
114
# File 'lib/minicron/alert.rb', line 106

def sent?(options = {})
  Minicron::Hub::Alert.exists?(
    :kind => options[:kind],
    :schedule_id => options[:schedule_id],
    :execution_id => options[:execution_id],
    :expected_at => options[:expected_at],
    :medium => options[:medium]
  )
end