Просмотр исходного кода

Improved error handling of streaming channels.

Martin Edenhofer 9 лет назад
Родитель
Сommit
b0af4f85ee
2 измененных файлов с 28 добавлено и 13 удалено
  1. 17 6
      app/models/channel.rb
  2. 11 7
      test/integration/twitter_browser_test.rb

+ 17 - 6
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

+ 11 - 7
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)