Last active
October 7, 2021 09:41
-
-
Save troex/31790323fb4a8a29c8b8cd84e50ad1e8 to your computer and use it in GitHub Desktop.
Sinatra + EventMachine + Puma – Heroku workaround for 30 seconds timeout
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
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 |
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
greetz!)
ze_d0g was searching u
CiN!