Skip to content

Instantly share code, notes, and snippets.

@raggi
Forked from lifo/gist:26399
Created November 19, 2008 03:28
Show Gist options
  • Save raggi/26404 to your computer and use it in GitHub Desktop.
Save raggi/26404 to your computer and use it in GitHub Desktop.
#!/usr/bin/env thin -e production -R config.ru -V start
# config.ru
# N.B. Rack::Lint doesn't yet support the api, use production mode rack.
require 'eventmachine'
class AsyncApp
class DeferrableBody
include EventMachine::Deferrable
def call(body)
body.each do |chunk|
@body_callback.call(chunk)
end
end
def each &blk
@body_callback = blk
end
end
# This is a template async response. N.B. Can't use string for body on 1.9
AsyncResponse = [-1, {}, []].freeze
def call(env)
body = DeferrableBody.new
EventMachine::next_tick { env['async.callback'].call [200, {'Content-Type' => 'text/plain'}, body] }
EventMachine::add_periodic_timer(3) do
body.call ["Cheers then!\n"]
# body.succeed
end
AsyncResponse
end
end
run AsyncApp.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment