Created
January 20, 2011 11:25
-
-
Save pmoran/787763 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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