Class: UState::Emailer

Inherits:
Object
  • Object
show all
Defined in:
lib/ustate/emailer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, opts = {}) ⇒ Emailer

Registers self with index. Options:

:host: The SMTP host to connect to. Default 'localhost'
:name: The From name used. Default "ustate".
:from: The From address used: e.g. "ustate@your_domain.com"


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

def initialize(index, opts = {})
  opts = {
    :name => 'ustate',
    :host => 'localhost'
  }.merge opts

  @from = opts[:from]
  @name = opts[:name]
  @host = opts[:host]
  @server = opts[:server]

  @tell = {}

  index.on_state_change &method(:receive)
  index.on_state_once &method(:receive)
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



8
9
10
# File 'lib/ustate/emailer.rb', line 8

def from
  @from
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/ustate/emailer.rb', line 9

def host
  @host
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/ustate/emailer.rb', line 10

def name
  @name
end

Instance Method Details

#email(address, s) ⇒ Object

Send an email to address about state.

Raises:

  • (ArgumentError)


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
# File 'lib/ustate/emailer.rb', line 35

def email(address, s)
  raise ArgumentError, "no from address" unless @from
  
  # Subject
  subject = "#{s.host} #{s.service}"
  if s.once
    subject << " transient "
  else
    subject << " is "
  end
  subject << s.state

  # Body
  body = "#{subject}: #{s.description}"

  # SMTP message
  message = <<EOF
From: #{@name} <#{@from}>
To: <#{address}>
Subject: #{subject.gsub("\n", ' ')}

#{body}
EOF

  Net::SMTP.start(@host) do |smtp|
    smtp.send_message message, @from, address
  end
end

#receive(*states) ⇒ Object

Dispatch emails to each address which is interested in this state



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ustate/emailer.rb', line 65

def receive(*states)
  Thread.new do
    begin
      @tell.each do |address, q|
        if states.any? { |state| p  state; q === state }
          email address, states.last
        end
      end
    rescue Exception => e
      @server.log.error e
    end
  end
end

#tell(email, query_string) ⇒ Object

Notify email when a state matching query_string is received. Multiple calls are ORed together:

emailer.tell ‘[email protected]’, ‘state = “error”’ emailer.tell ‘[email protected]’, ‘host =~ “frontend%”’



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ustate/emailer.rb', line 84

def tell(email, query_string)
  parser = QueryStringParser.new
  q = parser.parse(query_string)
  unless q
    raise ArgumentError, "error parsing #{query_string.inspect} at line #{parser.failure_line}:#{parser.failure_column}: #{parser.failure_reason}"
  end
  q = q.query

  @tell[email] = if existing = @tell[email]
    Query::Or.new existing, q
  else
    q
  end
end