From b0af4f85eef4b27237301e5826e82c165fd42686 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 14 Jan 2016 08:54:06 +0100 Subject: [PATCH] Improved error handling of streaming channels. --- app/models/channel.rb | 23 +++++++++++++++++------ test/integration/twitter_browser_test.rb | 18 +++++++++++------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/models/channel.rb b/app/models/channel.rb index 76c44f657..a973cd01d 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -159,17 +159,28 @@ stream all accounts # start threads for each channel @@channel_stream[channel.id][:thread] = Thread.new { - logger.debug "Started stream channel for '#{channel.id}' (#{channel.area})..." - @@channel_stream[channel.id][:stream_instance] = channel.stream_instance - @@channel_stream[channel.id][:stream_instance].stream - @@channel_stream[channel.id][:stream_instance].disconnect - @@channel_stream[channel.id] = false - logger.debug " ...stopped thread for '#{channel.id}'" + begin + logger.debug "Started stream channel for '#{channel.id}' (#{channel.area})..." + @@channel_stream[channel.id][:stream_instance] = channel.stream_instance + @@channel_stream[channel.id][:stream_instance].stream + @@channel_stream[channel.id][:stream_instance].disconnect + @@channel_stream[channel.id] = false + logger.debug " ...stopped thread for '#{channel.id}'" + rescue => e + error = "Can't use channel (#{channel.id}): #{e.inspect}" + logger.error error + logger.error e.backtrace + channel.status_in = 'error' + channel.last_log_in = error + channel.save + @@channel_stream[channel.id] = false + end } } # cleanup deleted channels last_channels.each {|channel_id| + next if !@@channel_stream[channel_id] next if current_channels.include?(channel_id) logger.debug "channel (#{channel_id}) not longer active, stop thread" @@channel_stream[channel_id][:thread].exit diff --git a/test/integration/twitter_browser_test.rb b/test/integration/twitter_browser_test.rb index 404a95c77..e60c7fda9 100644 --- a/test/integration/twitter_browser_test.rb +++ b/test/integration/twitter_browser_test.rb @@ -34,7 +34,7 @@ class TwitterBrowserTest < TestCase end twitter_customer_token_secret = ENV['TWITTER_BT_CUSTOMER_TOKEN_SECRET'] - hash = "#sweetcheck#{rand(99_999)}" + hash = "#sweetcheck#{rand(99_999)}" @browser = browser_instance login( @@ -186,6 +186,9 @@ class TwitterBrowserTest < TestCase css: '#content .main .action:nth-child(2)' ) + # wait till new streaming of channel is active + sleep 35 + # start tweet from customer client = Twitter::REST::Client.new do |config| config.consumer_key = consumer_key @@ -194,7 +197,7 @@ class TwitterBrowserTest < TestCase config.access_token_secret = twitter_customer_token_secret end - text = "Today... ##{hash} #{rand(99_999)}" + text = "Today... #{hash} #{rand(99_999)}" tweet = client.update( text, ) @@ -212,11 +215,12 @@ class TwitterBrowserTest < TestCase watch_for( css: '.content.active', - value: "##{hash}", + value: "#{hash}", + timeout: 20, ) ticket_open_by_title( - title: "##{hash}", + title: "#{hash}", ) # reply via app @@ -246,19 +250,19 @@ class TwitterBrowserTest < TestCase sleep 2 ticket_update( data: { - body: "@dzucker6 reply ##{hash}222 #{rand(99_999)}", + body: "@dzucker6 reply #{hash}222 #{rand(99_999)}", }, ) sleep 20 match( css: '.content.active .ticket-article', - value: "##{hash}222", + value: "#{hash}222", ) # watch till tweet reached customer text = nil - client.search("##{hash}222", result_type: 'mixed').collect { |local_tweet| + client.search("#{hash}222", result_type: 'mixed').collect { |local_tweet| text = local_tweet.text } assert(text)