Closed garyfoster closed 7 years ago
Create a standalone script (e.g. daemon.rb), something like this:
# Or change to where you could find your Rails environment
require_relative 'config/environment'
thread = Thread.new do
Thread.current.abort_on_exception = true
# Of course you should write it more formally, here's just the idea
$start = true
# Use your event stream setup somewhere
event_stream.start
sleep(1) while $start # sleep forever while we're running
event_stream.close
event_stream.wait # shutdown cleanly
end
rd, wr = IO.pipe
%w[INT TERM].each do |sig|
Signal.trap(sig) do
wr.puts # unblock the main thread
end
end
rd.gets # block main thread until INT or TERM received
$start = false # shutting down
thread.join # wait for the thread to stop cleanly
Or maybe you could just do it simple if you don't need to do the other stuffs.
# Or change to where you could find your Rails environment
require_relative 'config/environment'
# Use your event stream setup somewhere
event_stream.start
rd, wr = IO.pipe
%w[INT TERM].each do |sig|
Signal.trap(sig) do
wr.puts # unblock the main thread
end
end
rd.gets # block main thread until INT or TERM received
event_stream.close
event_stream.wait # shutdown cleanly
And finally put it in your Procfile
like:
firebase_daemon: ruby -Ilib daemon.rb
Worked great, thanks!
I implemented a routine to listen via the event streaming and it works fine if I run it on the rails console, or if I add the code to a rails initializer. Any hints on how to set this up to run as a daemon when deploying to heroku, preferably on a separate dyno from the main web dyno?