class Patriot::Command::PostProcessor::SlackNotification

Constants

API_KEY
CHANNEL
COUNT_PROP_KEY

retrial

ON_PROP_KEY
USERNAME

Public Instance Methods

http_request(job_ticket, state, url) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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