Avoid rails and non rails context (script/websocket-server.rb is without rails).

This commit is contained in:
Martin Edenhofer 2015-05-13 15:27:25 +02:00
parent 5cd3ca79bd
commit efd1f5dd15

View file

@ -251,14 +251,14 @@ returns
# if no session dir exists, session got destoried # if no session dir exists, session got destoried
if !File.exist? session_dir if !File.exist? session_dir
destory(client_id) destory(client_id)
Rails.logger.debug "missing session directory for '#{client_id}', remove session." log('debug', "missing session directory for '#{client_id}', remove session.")
return return
end end
# if only session file is missing, then it's an error behavior # if only session file is missing, then it's an error behavior
if !File.exist? session_file if !File.exist? session_file
destory(client_id) destory(client_id)
Rails.logger.errror "missing session file for '#{client_id}', remove session." log('error', "missing session file for '#{client_id}', remove session.")
return return
end end
begin begin
@ -273,9 +273,9 @@ returns
end end
} }
rescue => e rescue => e
Rails.logger.error e.inspect log('error', e.inspect)
destory(client_id) destory(client_id)
Rails.logger.error "error in reading/parsing session file '#{session_file}', remove session." log('error', "error in reading/parsing session file '#{session_file}', remove session.")
return return
end end
data data
@ -458,7 +458,7 @@ returns
begin begin
message_parsed = JSON.parse( spool['msg'] ) message_parsed = JSON.parse( spool['msg'] )
rescue => e rescue => e
Rails.logger.error "can't parse spool message: #{ message }, #{ e.inspect }" log('error', "can't parse spool message: #{ message }, #{ e.inspect }")
next next
end end
@ -535,7 +535,7 @@ returns
@@client_threads[client_id] = Thread.new { @@client_threads[client_id] = Thread.new {
thread_client(client_id) thread_client(client_id)
@@client_threads[client_id] = nil @@client_threads[client_id] = nil
Rails.logger.debug "close client (#{client_id}) thread" log('debug', "close client (#{client_id}) thread")
ActiveRecord::Base.connection.close ActiveRecord::Base.connection.close
} }
sleep 0.5 sleep 0.5
@ -575,17 +575,17 @@ returns
=end =end
def self.thread_client(client_id, try_count = 0, try_run_time = Time.now.utc) def self.thread_client(client_id, try_count = 0, try_run_time = Time.now.utc)
Rails.logger.debug "LOOP #{client_id} - #{try_count}" log('debug', "LOOP #{client_id} - #{try_count}")
begin begin
Sessions::Client.new(client_id) Sessions::Client.new(client_id)
rescue => e rescue => e
Rails.logger.error "thread_client #{client_id} exited with error #{ e.inspect }" log('error', "thread_client #{client_id} exited with error #{ e.inspect }")
Rails.logger.error e.backtrace.join("\n ") log('error', e.backtrace.join("\n ") )
sleep 10 sleep 10
begin begin
ActiveRecord::Base.connection_pool.release_connection ActiveRecord::Base.connection_pool.release_connection
rescue => e rescue => e
Rails.logger.error "Can't reconnect to database #{ e.inspect }" log('error', "Can't reconnect to database #{ e.inspect }")
end end
try_run_max = 10 try_run_max = 10
@ -604,7 +604,7 @@ returns
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
end end
Rails.logger.debug "/LOOP #{client_id} - #{try_count}" log('debug', "/LOOP #{client_id} - #{try_count}")
end end
def self.symbolize_keys(hash) def self.symbolize_keys(hash)
@ -621,4 +621,18 @@ returns
} }
end end
# we use it in rails and non rails context
def self.log(level, message)
if defined?(Rails)
if level == 'debug'
Rails.logger.debug message
elsif level == 'notice'
Rails.logger.notice message
else
Rails.logger.error message
end
return
end
puts "#{Time.now.utc.iso8601}:#{level} #{message}" # rubocop:disable Rails/Output
end
end end