Improved error handling.

This commit is contained in:
Martin Edenhofer 2014-04-24 18:43:24 +02:00
parent ff74c847e9
commit 52d4643f6c

View file

@ -217,7 +217,7 @@ module Sessions
if !@@user_threads[user.id] if !@@user_threads[user.id]
@@user_threads[user.id] = true @@user_threads[user.id] = true
@@user_threads[user.id] = Thread.new { @@user_threads[user.id] = Thread.new {
thread_worker(user.id, 0) thread_worker(user.id)
@@user_threads[user.id] = nil @@user_threads[user.id] = nil
puts "close user (#{user.id}) thread" puts "close user (#{user.id}) thread"
} }
@ -233,7 +233,7 @@ module Sessions
if !@@client_threads[client_id] if !@@client_threads[client_id]
@@client_threads[client_id] = true @@client_threads[client_id] = true
@@client_threads[client_id] = Thread.new { @@client_threads[client_id] = Thread.new {
thread_client(client_id, 0) thread_client(client_id)
@@client_threads[client_id] = nil @@client_threads[client_id] = nil
puts "close client (#{client_id}) thread" puts "close client (#{client_id}) thread"
} }
@ -245,31 +245,40 @@ module Sessions
end end
end end
def self.thread_worker(user_id, count) def self.thread_worker(user_id, try_count = 0, try_run_time = Time.now)
puts "LOOP WORKER #{user_id} - #{count}" puts "LOOP WORKER #{user_id} - #{try_count}"
begin begin
Sessions::Worker.new(user_id) Sessions::Worker.new(user_id)
rescue => e rescue => e
puts "thread_client exited with error #{ e.inspect }" puts "thread_worker exited with error #{ e.inspect }"
sleep 10 sleep 10
begin begin
ActiveRecord::Base.connection.reconnect! ActiveRecord::Base.connection.reconnect!
rescue => e rescue => e
puts "Can't reconnect to database #{ e.inspect }" puts "Can't reconnect to database #{ e.inspect }"
end end
ct = count++1
if ct < 10 try_run_max = 10
thread_worker(user_id, ct) try_count += 1
# reset error counter if to old
if try_run_time + ( 60 * 5 ) < Time.now
try_count = 0
end
try_run_time = Time.now
# restart worker again
if try_run_max > try_count
thread_worker(user_id, try_count, try_run_time)
else else
raise "STOP thread_worker for user #{user_id} after 10 tries" raise "STOP thread_worker for user #{user_id} after #{try_run_max} tries"
end end
end end
puts "/LOOP WORKER #{user_id} - #{count}" puts "/LOOP WORKER #{user_id} - #{try_count}"
end end
def self.thread_client(client_id, count, try_count = 0, try_run_time = Time.now) def self.thread_client(client_id, try_count = 0, try_run_time = Time.now)
puts "LOOP #{client_id} - #{count}" puts "LOOP #{client_id} - #{try_count}"
begin begin
Sessions::Client.new(client_id) Sessions::Client.new(client_id)
rescue => e rescue => e
@ -292,7 +301,7 @@ module Sessions
# restart job again # restart job again
if try_run_max > try_count if try_run_max > try_count
thread_client(client_id, ct, try_count, try_run_time) thread_client(client_id, try_count, try_run_time)
else else
raise "STOP thread_client for client #{client_id} after #{try_run_max} tries" raise "STOP thread_client for client #{client_id} after #{try_run_max} tries"
end end