Skip to content

Instantly share code, notes, and snippets.

@iHiD
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iHiD/10136255 to your computer and use it in GitHub Desktop.
Save iHiD/10136255 to your computer and use it in GitHub Desktop.
require 'net/http'
module Indicare
class NewsFeedItemProcessor < Larva::Processor
def news_feed_item_created
publish_to_flumen
end
private
def publish_to_flumen
nfi = MeducationSDK::NewsFeedItem.find(id)
message = {
type: :news_feed_item,
payload: NewsFeedItemSerializer.serialize(nfi).to_json
}
Indicare.config.logger.info "Posting message to flumen"
uri = URI('http://flow.meducation.net:3004')
unless retry_post(uri, message)
Indicare.config.logger.error "Message POST failed: #{res.code}: #{res.message}"
raise IndicareError.new("Message failed to post to flumen")
end
end
def retry_post(uri, message)
max_posts = Indicare.config.max_post_retries
delay = Indicare.config.initial_post_retry_delay
max_posts.times do |post_count|
begin
Indicare.config.logger.info "Attempting POST #{post_count}"
res = Net::HTTP.post_form(uri, message)
if res.code == '200'
Indicare.config.logger.info "POST #{post_count} successful"
return true
else
Indicare.config.logger.error "POST #{post_count} failed with response: #{res.code} - sleeping for #{delay} seconds"
end
rescue => e
Indicare.config.logger.error "POST #{post_count} failed with exception: #{e.message} - sleeping for #{delay} seconds"
raise if (post_count - 1) == max_posts
end
sleep delay
delay = delay * 2
end
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment