Class: Patriot::Command::PostProcessor::SlackNotification

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

Constant Summary collapse

API_KEY =
:api_key
CHANNEL =
:channel
USERNAME =
:username
ON_PROP_KEY =
:on
COUNT_PROP_KEY =

retrial

:count

Instance Attribute Summary

Attributes inherited from Base

#props

Instance Method Summary collapse

Methods inherited from Base

declare_post_processor_name, #initialize, #process_failure, #process_success

Constructor Details

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

Instance Method Details

#http_request(job_ticket, state, url) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/patriot/command/post_processor/slack_notification.rb', line 58

def http_request(job_ticket, state, url)
  icon_emoji = {}
  icon_emoji['SUCCEEDED'] = ':good:'
  icon_emoji['FAILED']    = ':no_good:'

  retries = 0
  begin
    return RestClient.post(
      url,
      {
        channel: @props[CHANNEL],
        username: @props[USERNAME],
        icon_emoji: icon_emoji[state],
        text: <<-EOT
job_id: #{job_ticket.job_id} #{state}!!!

---
exec_host: #{job_ticket.exec_host}
#{job_ticket.description}
EOT
      }.to_json,
      :content_type => 'application/json'
    )
  rescue RestClient::Exception => error
    retries += 1
    if retries < 3
      retry
    else
      raise error
    end
  end
end

#process(cmd, worker, job_ticket) ⇒ Object



28
29
30
31
32
33
# File 'lib/patriot/command/post_processor/slack_notification.rb', line 28

def process(cmd, worker, job_ticket)
  if should_notice?(cmd, job_ticket)
    http_request(job_ticket, Patriot::Command::ExitCode.name_of(job_ticket.exit_code), url(worker))
  end
  return true
end

#should_notice?(cmd, job_ticket) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/patriot/command/post_processor/slack_notification.rb', line 39

def should_notice?(cmd, job_ticket)
  on = @props[ON_PROP_KEY]
  on = [on] unless on.is_a?(Array)
  on = on.map{|o| Patriot::Command::ExitCode.value_of(o)}

  if on.include?(job_ticket.exit_code)
    if Patriot::Command::ExitCode.name_of(job_ticket.exit_code) == 'SUCCEEDED'
      return true
    else
      retrial = cmd.post_processors.select{|pp| pp.is_a?(Patriot::Command::PostProcessor::Retrial)}[0]
      if retrial == nil || retrial.props[COUNT_PROP_KEY] <= 1
        return true
      end
    end
  end

  return false
end

#url(worker) ⇒ Object



35
36
37
# File 'lib/patriot/command/post_processor/slack_notification.rb', line 35

def url(worker)
  return worker.config.get("slack.notification.#{@props[API_KEY]}.url")
end

#valid_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/patriot/command/post_processor/slack_notification.rb', line 91

def valid_url?(url)
  begin
    uri = URI.parse(url)
    if uri.scheme != 'http' && uri.scheme != 'https'
      return false
    end
  rescue URI::InvalidURIError
    return false
  end
  return true
end

#validate_props(props) ⇒ Object



21
22
23
24
25
26
# File 'lib/patriot/command/post_processor/slack_notification.rb', line 21

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