TwitterStream bugfixes

* do not run disabled `TwitterStreamAgents`
* calling `sleep` within the EM loop blocks the EM thread and thereby
  blocks the reload callback and proper shutdown of the threaded worker
* stopping the EM loop from outside of the `EM.run` blocks does not seem
  to work reliably, using a timer and checking if shutdown was requested
  works as expected
* removed unnecessary sleep at the end of the EM loop which would delay
  the shutdown of the thread
This commit is contained in:
Dominik Sander 2014-08-17 15:22:49 +02:00
parent 11d788f1ad
commit d529872e37
2 changed files with 10 additions and 8 deletions

View file

@ -55,6 +55,8 @@ class Agent < ActiveRecord::Base
has_many :scenario_memberships, :dependent => :destroy, :inverse_of => :agent
has_many :scenarios, :through => :scenario_memberships, :inverse_of => :agents
scope :active, -> { where(disabled: false) }
scope :of_type, lambda { |type|
type = case type
when String, Symbol, Class

View file

@ -11,7 +11,6 @@ class TwitterStream
def stop
@running = false
EventMachine::stop_event_loop if EventMachine.reactor_running?
end
def stream!(filters, agent, &block)
@ -91,9 +90,13 @@ class TwitterStream
def run
while @running
begin
agents = Agents::TwitterStreamAgent.all
agents = Agents::TwitterStreamAgent.active.all
EventMachine::run do
EventMachine.add_periodic_timer(1) {
EventMachine::stop_event_loop if !@running
}
EventMachine.add_periodic_timer(RELOAD_TIMEOUT) {
puts "Reloading EventMachine and all Agents..."
EventMachine::stop_event_loop
@ -101,17 +104,14 @@ class TwitterStream
if agents.length == 0
puts "No agents found. Will look again in a minute."
sleep 60
EventMachine::stop_event_loop
EventMachine.add_timer(60) {
EventMachine::stop_event_loop
}
else
puts "Found #{agents.length} agent(s). Loading them now..."
load_and_run agents
end
end
print "Pausing..."; STDOUT.flush
sleep 1
puts "done."
rescue SignalException, SystemExit
@running = false
EventMachine::stop_event_loop if EventMachine.reactor_running?