Class: Patriot::Command::PostProcessor::HttpNotification

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

Constant Summary collapse

CALLBACK_URL =
:callback_url
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, #process_failure, #process_success

Constructor Details

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

Instance Method Details

#process(cmd, worker, job_ticket) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/patriot/command/post_processor/http_notification.rb', line 20

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 unless on.include?(exit_code)

  callback_url = @props[CALLBACK_URL]
  case exit_code
  when Patriot::Command::ExitCode::SUCCEEDED then send_callback(job_ticket.job_id, callback_url, "SUCCEEDED")
  when Patriot::Command::ExitCode::FAILED then send_callback(job_ticket.job_id, callback_url, "FAILED")
  end
  return true
end

#send_callback(job_id, callback_url, state) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/patriot/command/post_processor/http_notification.rb', line 35

def send_callback(job_id, callback_url, state)
  retries = 0
  begin
    return RestClient.post(
      callback_url,
      {'job_id' => job_id, 'state' => state }.to_json,
      :content_type => 'application/json'
    )
  rescue RestClient::Exception => error
    retries += 1
    if retries < 3
      retry
    else
      raise error
    end
  end
end

#valid_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/patriot/command/post_processor/http_notification.rb', line 53

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



14
15
16
17
18
# File 'lib/patriot/command/post_processor/http_notification.rb', line 14

def validate_props(props)
  raise "#{CALLBACK_URL} is not specified" unless props.has_key?(CALLBACK_URL)
  raise "#{CALLBACK_URL} is not a correct URL" unless valid_url?(props.fetch(CALLBACK_URL, ""))
  raise "#{ON_PROP_KEY} is not specified" unless props.has_key?(ON_PROP_KEY)
end