Skip to content

Instantly share code, notes, and snippets.

@troex
Last active October 7, 2021 09:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troex/31790323fb4a8a29c8b8cd84e50ad1e8 to your computer and use it in GitHub Desktop.
Save troex/31790323fb4a8a29c8b8cd84e50ad1e8 to your computer and use it in GitHub Desktop.
Sinatra + EventMachine + Puma – Heroku workaround for 30 seconds timeout
module Application
module Controllers
class MyController < Sinatra::Base
helpers Sinatra::Streaming
class << self
def stream(method, path, opts = {}, &block)
send(method, path, opts) do
stream do |out|
timer = EventMachine::PeriodicTimer.new(10) { out << "\0" }
out << instance_eval(&block)
timer.cancel
end
end
end
end
stream :get, '/test_stream' do
# do the long-running stuff here
sleep 40
'done'
end
end
end
end
# puma.rb
require 'eventmachine'
on_worker_boot do
# start EM for each worker
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
end
@el-sol
Copy link

el-sol commented Sep 22, 2021

greetz!)

ze_d0g was searching u

CiN!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment