Move EventMachine initialization to Runner::EventLoop

This commit is contained in:
Felix Auringer
2021-07-27 13:40:50 +02:00
committed by Sebastian Serth
parent 9e2cff7558
commit e752df1b3c
2 changed files with 19 additions and 17 deletions

View File

@ -41,7 +41,6 @@ class Runner < ApplicationRecord
end
def attach_to_execution(command, &block)
ensure_event_machine
starting_time = Time.zone.now
begin
# As the EventMachine reactor is probably shared with other threads, we cannot use EventMachine.run with
@ -66,22 +65,6 @@ class Runner < ApplicationRecord
private
# If there are multiple threads trying to connect to the WebSocket of their execution at the same time,
# the Faye WebSocket connections will use the same reactor. We therefore only need to start an EventMachine
# if there isn't a running reactor yet.
# See this StackOverflow answer: https://stackoverflow.com/a/8247947
def ensure_event_machine
unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
event_loop = Runner::EventLoop.new
Thread.new do
EventMachine.run { event_loop.stop }
ensure
ActiveRecord::Base.connection_pool.release_connection
end
event_loop.wait
end
end
def request_id
request_new_id if runner_id.blank?
end