Class: Patriot::Command::PostProcessor::MailNotification

Inherits:
Base
  • Object
show all
Defined in:
lib/patriot/command/post_processor/mail_notification.rb

Constant Summary collapse

TO_PROP_KEY =
:to
ON_PROP_KEY =
:on

Instance Attribute Summary

Attributes inherited from Base

#props

Instance Method Summary collapse

Methods inherited from Base

declare_post_processor_name, #initialize

Constructor Details

This class inherits a constructor from Patriot::Command::PostProcessor::Base

Instance Method Details

#deliver(from_addr, to_addr, msg_subj, msg_body) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/patriot/command/post_processor/mail_notification.rb', line 47

def deliver(from_addr, to_addr, msg_subj, msg_body)
  Mail.deliver do
    from from_addr
    to   to_addr
    subject msg_subj
    body    msg_body
  end
end

#process(cmd, worker, job_ticket) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/patriot/command/post_processor/mail_notification.rb', line 17

def process(cmd, worker, job_ticket)
  on = @props[ON_PROP_KEY]
  on = [on] unless on.is_a?(Array)
  on = on.map{|o| Patriot::Command::ExitCode.value_of(o)}
  exit_code = job_ticket.exit_code
  return true unless on.include?(exit_code)
  case exit_code
  when Patriot::Command::ExitCode::SUCCEEDED then process_success(cmd, worker, job_ticket)
  when Patriot::Command::ExitCode::FAILED    then process_failure(cmd, worker, job_ticket)
  end
  return true
end

#process_failure(cmd, worker, job_ticket) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/patriot/command/post_processor/mail_notification.rb', line 38

def process_failure(cmd, worker, job_ticket)
  from = worker.config.get(Patriot::Util::Config::ADMIN_USER_KEY)
  to   = @props[TO_PROP_KEY]
  subject = "#{job_ticket.job_id} has been failed"
  body = "#{job_ticket.job_id} has been failed \n\n --- \n #{job_ticket.description}"
  to = [to] unless to.is_a? Array
  to.each{|to_addr| deliver(from, to_addr, subject, body) }
end

#process_success(cmd, worker, job_ticket) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/patriot/command/post_processor/mail_notification.rb', line 30

def process_success(cmd, worker, job_ticket)
  from = worker.config.get(Patriot::Util::Config::ADMIN_USER_KEY)
  to   = @props[TO_PROP_KEY]
  subject = "#{job_ticket.job_id} has been successfully finished"
  body = "#{job_ticket.job_id} has been successfully finished \n\n --- \n #{job_ticket.description}"
  deliver(from, to, subject, body)
end

#validate_props(props) ⇒ Object



12
13
14
15
# File 'lib/patriot/command/post_processor/mail_notification.rb', line 12

def validate_props(props)
  raise "#{TO_PROP_KEY} is not specified" unless props.has_key?(TO_PROP_KEY)
  raise "#{ON_PROP_KEY} is not specified" unless props.has_key?(ON_PROP_KEY)
end