Skip to content

Commit

Permalink
TwitterStreamAgent prevent connection in use and restart issues
Browse files Browse the repository at this point in the history
Ensures the database connection is checked back in to the pool before restarting.
Restart TwitterStreamAgent outside of the Rufus::Scheduler work thread.
  • Loading branch information
dsander committed Mar 12, 2016
1 parent 718dabd commit 6ee094a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app/concerns/long_runnable.rb
Expand Up @@ -98,13 +98,15 @@ def stop!

def terminate_thread!
if thread
thread.terminate
thread.instance_eval { ActiveRecord::Base.connection_pool.release_connection }
thread.wakeup if thread.status == 'sleep'
thread.terminate
end
end

def restart!
without_alive_check do
puts "--> Restarting #{id} at #{Time.now} <--"
stop!
setup!(scheduler, mutex)
run!
Expand Down
11 changes: 6 additions & 5 deletions app/models/agents/twitter_stream_agent.rb
Expand Up @@ -157,16 +157,14 @@ class Worker < LongRunnable::Worker
def setup
require 'twitter/json_stream'
@filter_to_agent_map = @config[:filter_to_agent_map]

schedule_in RELOAD_TIMEOUT do
puts "--> Restarting TwitterStream #{id} at #{Time.now} <--"
restart!
end
end

def run
@recent_tweets = []
EventMachine.run do
EventMachine.add_periodic_timer(RELOAD_TIMEOUT) do
restart!
end
stream!(@filter_to_agent_map.keys, @agent) do |status|
handle_status(status)
end
Expand Down Expand Up @@ -200,6 +198,9 @@ def stream!(filters, agent, &block)

stream.on_error do |message|
STDERR.puts " --> Twitter error: #{message} at #{Time.now} <--"
STDERR.puts " --> Sleeping for 15 seconds"
sleep 15
restart!
end

stream.on_no_data do |message|
Expand Down

0 comments on commit 6ee094a

Please sign in to comment.