Skip to content

Instantly share code, notes, and snippets.

@pmoran
Created Jan 20, 2011
Embed
What would you like to do?
def handle_notification(notification)
message = JSON.parse(notification)['doc']
Fiber.new {
result = despatch(message) # non-blocking
@db.save_doc(message.merge(status: "forwarded", result: result)) # blocking
}.resume
end
def despatch(message)
http = http_request({id: message['_id'], broadcast: message['text']})
http.response_header.status == 200 ? "success" : "failed"
end
def http_request(data = {})
f = Fiber.current
http = EventMachine::HttpRequest.new(TARGET_URL).post :body => data
http.callback { f.resume(http) }
http.errback { f.resume(http) }
return Fiber.yield
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment