Class: Mutx::Alert
- Inherits:
-
Object
- Object
- Mutx::Alert
- Defined in:
- lib/mutx/models/alert.rb
Defined Under Namespace
Modules: Script
Constant Summary collapse
- ALERT_FOLDER =
File.join("#{Dir.pwd}", 'alerts')
- STATUS =
{ ok: 0, unknown: 1, skip: 2, warning: 3, critical: 4 }
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#job ⇒ Object
readonly
Returns the value of attribute job.
-
#notification ⇒ Object
readonly
Returns the value of attribute notification.
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
- #cron ⇒ Object
- #email ⇒ Object
- #enqueue? ⇒ Boolean
-
#initialize(filename) ⇒ Alert
constructor
A new instance of Alert.
- #last_enqueue ⇒ Object
- #name ⇒ Object
- #off! ⇒ Object
- #on! ⇒ Object
- #result ⇒ Object
- #result=(value) ⇒ Object
- #slack ⇒ Object
- #status ⇒ Object
- #telegram ⇒ Object
- #update_status(a_new_status, info: nil) ⇒ Object
Constructor Details
#initialize(filename) ⇒ Alert
Returns a new instance of Alert.
64 65 66 67 68 69 |
# File 'lib/mutx/models/alert.rb', line 64 def initialize(filename) @filename = filename extract_info @enqueue = true @job = Sidekiq::Cron::Job.find(name) || ((@enqueue = false) || create_job ) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
62 63 64 |
# File 'lib/mutx/models/alert.rb', line 62 def attributes @attributes end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
62 63 64 |
# File 'lib/mutx/models/alert.rb', line 62 def filename @filename end |
#job ⇒ Object (readonly)
Returns the value of attribute job.
62 63 64 |
# File 'lib/mutx/models/alert.rb', line 62 def job @job end |
#notification ⇒ Object (readonly)
Returns the value of attribute notification.
62 63 64 |
# File 'lib/mutx/models/alert.rb', line 62 def notification @notification end |
Class Method Details
.all ⇒ Object
43 44 45 46 47 |
# File 'lib/mutx/models/alert.rb', line 43 def self.all alerts = Dir.entries(ALERT_FOLDER).select { |file| file.match(/_alert.rb$/) }.map {|file| self.new(file) } end |
.find(args = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mutx/models/alert.rb', line 49 def self.find(args={}) if args.is_a? Hash filename = "#{args[:name]}_alert.rb" if File.exists?(File.join(ALERT_FOLDER, filename)) return self.new(filename) else return nil end else return nil end end |
Instance Method Details
#clear ⇒ Object
135 136 137 138 139 |
# File 'lib/mutx/models/alert.rb', line 135 def clear Sidekiq.redis do |redis| redis.del("alerts:#{name}") end end |
#cron ⇒ Object
87 88 89 |
# File 'lib/mutx/models/alert.rb', line 87 def cron attributes['cron'] end |
#email ⇒ Object
75 76 77 |
# File 'lib/mutx/models/alert.rb', line 75 def email attributes['email'] end |
#enqueue? ⇒ Boolean
95 96 97 |
# File 'lib/mutx/models/alert.rb', line 95 def enqueue? @enqueue end |
#last_enqueue ⇒ Object
91 92 93 |
# File 'lib/mutx/models/alert.rb', line 91 def last_enqueue @job.last_enqueue_time end |
#name ⇒ Object
71 72 73 |
# File 'lib/mutx/models/alert.rb', line 71 def name filename.gsub('_alert.rb', '') end |
#off! ⇒ Object
107 108 109 110 |
# File 'lib/mutx/models/alert.rb', line 107 def off! @job.destroy @enqueue = false end |
#on! ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/mutx/models/alert.rb', line 99 def on! if @job.valid? @job.save @enqueue = true end return self.enqueue? end |
#result ⇒ Object
123 124 125 126 127 |
# File 'lib/mutx/models/alert.rb', line 123 def result Sidekiq.redis do |redis| redis.get("alerts:#{name}") end end |
#result=(value) ⇒ Object
129 130 131 132 133 |
# File 'lib/mutx/models/alert.rb', line 129 def result=(value) Sidekiq.redis do |redis| redis.set("alerts:#{name}", value.to_s) end end |
#slack ⇒ Object
83 84 85 |
# File 'lib/mutx/models/alert.rb', line 83 def slack attributes['slack'] end |
#status ⇒ Object
112 113 114 |
# File 'lib/mutx/models/alert.rb', line 112 def status STATUS.invert[self.result.to_i] || :unknown end |
#telegram ⇒ Object
79 80 81 |
# File 'lib/mutx/models/alert.rb', line 79 def telegram attributes['telegram'] end |
#update_status(a_new_status, info: nil) ⇒ Object
116 117 118 119 120 121 |
# File 'lib/mutx/models/alert.rb', line 116 def update_status(a_new_status, info: nil) last_result = self.result return if ((last_result.to_s == a_new_status.to_s) or (a_new_status.to_i == STATUS[:skip])) self.result = a_new_status notify(info) if last_result || a_new_status.to_i == STATUS[:unknown] end |