/
growl_agent.rb
62 lines (51 loc) · 2.22 KB
/
growl_agent.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
61
62
require 'ruby-growl'
module Agents
class GrowlAgent < Agent
attr_reader :growler
cannot_be_scheduled!
cannot_create_events!
description <<-MD
The GrowlAgent sends any events it receives to a Growl GNTP server immediately.
It is assumed that events have a `message` or `text` key, which will hold the body of the growl notification, and a `subject` key, which will have the headline of the Growl notification. You can use Event Formatting Agent if your event does not provide these keys.
Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
MD
def default_options
{
'growl_server' => 'localhost',
'growl_password' => '',
'growl_app_name' => 'HuginnGrowl',
'growl_notification_name' => 'Notification',
'expected_receive_period_in_days' => "2"
}
end
def working?
last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
end
def validate_options
unless options['growl_server'].present? && options['expected_receive_period_in_days'].present?
errors.add(:base, "growl_server and expected_receive_period_in_days are required fields")
end
end
def register_growl
@growler = Growl.new options['growl_server'], options['growl_app_name'], "GNTP"
@growler.password = options['growl_password']
@growler.add_notification options['growl_notification_name']
end
def notify_growl(subject, message)
@growler.notify(options['growl_notification_name'],subject,message)
end
def receive(incoming_events)
register_growl
incoming_events.each do |event|
message = (event.payload['message'] || event.payload['text']).to_s
subject = event.payload['subject'].to_s
if message.present? && subject.present?
log "Sending Growl notification '#{subject}': '#{message}' to #{options['growl_server']} with event #{event.id}"
notify_growl(subject,message)
else
log "Event #{event.id} not sent, message and subject expected"
end
end
end
end
end