Skip to content

Instantly share code, notes, and snippets.

@pmoran
Created January 20, 2011 11:25
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 pmoran/787763 to your computer and use it in GitHub Desktop.
Save pmoran/787763 to your computer and use it in GitHub Desktop.
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