Streaming on Ruby Thin server - ruby

I tried the following ruby code...
self.response.headers["Cache-Control"] ||= "no-cache"
self.response.headers["Transfer-Encoding"] = "chunked"
self.response.headers['Last-Modified'] = Time.now.ctime.to_s
self.response_body = Rack::Chunked::Body.new(Enumerator.new do |y|
10.times do
sleep 1
y << "Hello World\n"
end
end)
This works great in Unicron server but can't stream using Thin server. I tried 1.5.0 and 2.0.0.pre too, this is not working in thin.
I tried the following rack code,
class DeferredBody
def each(block)
#server_block = block
end
def send(data)
#server_block.call data
end
end
class RackStreamApp
def self.call(env)
Thread.new do
sleep 2 # simulate waiting for some event
body = DeferredBody.new
response = [200, {'Content-Type' => 'text/plain'}, body]
env['async.callback'].call response
body.send 'Hello, '
sleep 2
body.send 'World'
end
[-1, {}, []] # or throw :async
end
end
The above code streams "Hello, World" if we use Unicorn Server, but the code doesn't stream using Thin server 1.5.0 ( I tried 2.0.0-pre too)
Is there anything I can do to stream data using the thin server?

Related

Running Sinatra server and subprocess asynchronously

I am trying to run a process that processes flight tracking data and actively turns it into JSON strings (continuous looping process) alongside a Sinatra server that responds to GET requests with these JSON strings. I am trying to use threading to handle this but have had no success. How can I run these two processes side by side? Here are some more specifics:
I have a class Aircraft with an array of Aircraft objects called Aircraft::All. I have a method that continually updates this array that I want to run alongside a Sinatra server that responds to GET requests with the list of aircraft in JSON format.
Here is the code:
# starting the data stream from external process
IO.popen("./dump1090") do |data|
block = ""
# created sinatra server thread
t1 = Thread.new do
set :port, 8080
set :environment, :production
get '/aircrafts' do
return_message = {}
if !Aircraft::All.first.nil?
return_message[:status] == 'success'
return_message[:aircrafts] = message_maker
else
return_message[:status] = 'sorry - something went wrong'
return_message[:aircrafts] = []
end
return_message.to_json
end
end
# parsing the data in main thread -- the process
# I want to run alongside the server (parse_block updates Aircraft::All)
while line = data.gets
if line.to_s.split('').first == '*'
parse_block(block)
puts block
Aircraft::All.reject { |aircraft| Time.now.to_f - aircraft.contact_time > 30 }
block = ""
end
block += line.to_s
end
end
Here the main thread is the Sinatra app and the additional thread loads the data, which is more usual to me.
class Aircraft
#aircrafts = {}
def self.all
#aircrafts
end
end
Thread.new do
no = 1
while true
Aircraft.all[no] = 'Boing'
no += 1
sleep(3)
end
end
get '/aircrafts' do
Aircraft.all.to_json
end

Sinatra + Fibers + EventMachine

I would like to know to to pause a Root Fiber in ruby (if possible).
I have this Sinatra app, and I am making async calls to an external API with EventMachine.
I don't want to respond to the client until the api responds me.
For example, sleeping the Root Fiber in Sinatra until the EventMachine callback wake it up.
Thanks.
get '/some/route/' do
fib = Fiber.current
req = EM::SomeNonBlokingLib.request
req.callback do |response|
fib.resume(response)
end
req.errback do |err|
fib.resume(err)
end
Fiber.yield
end
EDIT
In your case you should spawn a Fiber for each request. So. Firstly create Rack config file and add some magick:
# config.ru
BOOT_PATH = File.expand_path('../http.rb', __FILE__)
require BOOT_PATH
class FiberSpawn
def initialize(app)
#app = app
end
def call(env)
fib = Fiber.new do
res = #app.call(env)
env['async.callback'].call(res)
end
EM.next_tick{ fib.resume }
throw :async
end
end
use FiberSpawn
run Http
Then your http Sinatra application:
# http.rb
require 'sinatra'
require 'fiber'
class Http < Sinatra::Base
get '/' do
f = Fiber.current
EM.add_timer(1) do
f.resume("Hello World")
end
Fiber.yield
end
end
Now you could run it under thin for example:
> thin start -R config.ru
Then if you will visit locakhost:3000 you'll see your Hello World message

Ctrl+C not killing Sinatra + EM::WebSocket servers

I'm building a Ruby app that runs both an EM::WebSocket server as well as a Sinatra server. Individually, I believe both of these are equipped to handle a SIGINT. However, when running both in the same app, the app continues when I press Ctrl+C. My assumption is that one of them is capturing the SIGINT, preventing the other from capturing it as well. I'm not sure how to go about fixing it, though.
Here's the code in a nutshell:
require 'thin'
require 'sinatra/base'
require 'em-websocket'
EventMachine.run do
class Web::Server < Sinatra::Base
get('/') { erb :index }
run!(port: 3000)
end
EM::WebSocket.start(port: 3001) do |ws|
# connect/disconnect handlers
end
end
I had the same issue. The key for me seemed to be to start Thin in the reactor loop with signals: false:
Thin::Server.start(
App, '0.0.0.0', 3000,
signals: false
)
This is complete code for a simple chat server:
require 'thin'
require 'sinatra/base'
require 'em-websocket'
class App < Sinatra::Base
# threaded - False: Will take requests on the reactor thread
# True: Will queue request for background thread
configure do
set :threaded, false
end
get '/' do
erb :index
end
end
EventMachine.run do
# hit Control + C to stop
Signal.trap("INT") {
puts "Shutting down"
EventMachine.stop
}
Signal.trap("TERM") {
puts "Shutting down"
EventMachine.stop
}
#clients = []
EM::WebSocket.start(:host => '0.0.0.0', :port => '3001') do |ws|
ws.onopen do |handshake|
#clients << ws
ws.send "Connected to #{handshake.path}."
end
ws.onclose do
ws.send "Closed."
#clients.delete ws
end
ws.onmessage do |msg|
puts "Received message: #{msg}"
#clients.each do |socket|
socket.send msg
end
end
end
Thin::Server.start(
App, '0.0.0.0', 3000,
signals: false
)
end
I downgrade thin to version 1.5.1 and it just works. Wired.

Responding to HTTP Requests in Eventmachine

I have a very simple server for use in integration tests, built using eventmachine:
EM.run do
EM::start_server(server, port, HttpRecipient)
end
I can receive HTTP requests and parse them like so:
class HttpRecipient < EM::Connection
def initialize
##stored = ''
end
# Data is received in chunks, so here we wait until we've got a full web request before
# calling spool.
def receive_data(data)
##stored << data
begin
spool(##stored)
EM.stop
rescue WEBrick::HTTPStatus::BadRequest
#Not received a complete request yet
end
end
def spool(data)
#Parse the request
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(##stored))
#Send a response, e.g. HTTP OK
end
end
The question is, how do I send a response? Eventmachine provides a send_data for sending responses, but that doesn't understand http. Similarly there is the em-http-request
module for sending requests, but it's not obvious that this is capable of generating responses.
I can generate HTTP messages manually and then send them using send_data, but I wonder if there is a clean way to use an existing http library, or the functionality built in to eventmachine?
If you want something easy then use Thin or Rainbows. It uses Eventmachine inside and provides Rack interface support.
# config.ru
http_server = proc do |env|
response = "Hello World!"
[200, {"Connection" => "close", "Content-Length" => response.bytesize.to_s}, [response]]
end
run http_server
And then
>> thin start -R config.ru
UPD.
If you need server to run in parallel you could run it in a Thread
require 'thin'
class ThreadedServer
def initialize(*args)
#server = Thin::Server.new(*args)
end
def start
#thread = Thread.start do
#server.start
end
end
def stop
#server.stop
if #thread
#thread.join
#thread = nil
end
end
end
http_server = proc do |env|
response = "Hello World!"
[200, {"Connection" => "close", "Content-Length" => response.bytesize.to_s}, [response]]
end
server = ThreadedServer.new http_server
server.start
# Some job with server
server.stop
# Server is down

How to stop a background thread in Sinatra once the connection is closed

I'm trying to consume the twitter streaming API with Sinatra and give users real-time updates when they search for a keyword.
require 'sinatra'
require 'eventmachine'
require 'em-http'
require 'json'
STREAMING_URL = 'https://stream.twitter.com/1/statuses/sample.json'
get '/' do
stream(:keep_open) do |out|
http = EM::HttpRequest.new(STREAMING_URL).get :head => { 'Authorization' => [ 'USERNAME', 'PASS' ] }
buffer = ""
http.stream do |chunk|
puts "still chugging"
buffer += chunk
while line = buffer.slice!(/.+\r?\n/)
tweet = JSON.parse(line)
unless tweet.length == 0 or tweet['user'].nil?
out << "<p><b>#{tweet['user']['screen_name']}</b>: #{tweet['text']}</p>"
end
end
end
end
end
I want the processing of the em-http-request stream to stop if the user closes the connection. Does anyone know how to do this?
Eric's answer was close, but what it does is closing the response body (not the client connection, btw) once your twitter stream closes, which normally never happens. This should work:
require 'sinatra/streaming' # gem install sinatra-contrib
# ...
get '/' do
stream(:keep_open) do |out|
# ...
out.callback { http.conn.close_connection }
out.errback { http.conn.close_connection }
end
end
I'm not quite familiar with the Sinatra stream API yet, but did you try this?
http.callback { out.close }

Resources