post_agent.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module Agents
  2. class PostAgent < Agent
  3. cannot_be_scheduled!
  4. description <<-MD
  5. Post Agent receives events from other agents and send those events as the contents of a post request to a specified url. `post_url` field must specify where you would like to receive post requests and do not forget to include URI scheme (`http` or `https`)
  6. MD
  7. event_description "Does not produce events."
  8. def default_options
  9. {
  10. :post_url => "http://www.example.com",
  11. :expected_receive_period_in_days => 1
  12. }
  13. end
  14. def working?
  15. last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
  16. end
  17. def validate_options
  18. unless options[:post_url].present? && options[:expected_receive_period_in_days].present?
  19. errors.add(:base, "post_url and expected_receive_period_in_days are required fields")
  20. end
  21. end
  22. def post_event(uri, event)
  23. req = Net::HTTP::Post.new(uri.request_uri)
  24. req.form_data = event
  25. Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == "https") { |http| http.request(req) }
  26. end
  27. def receive(incoming_events)
  28. incoming_events.each do |event|
  29. uri = URI options[:post_url]
  30. post_event uri, event.payload
  31. end
  32. end
  33. end
  34. end