Use utc timestamps for all.

This commit is contained in:
Martin Edenhofer 2015-05-10 21:47:17 +02:00
parent 08a42e5396
commit c8ae1d38e1
2 changed files with 19 additions and 17 deletions

View file

@ -1,3 +1,5 @@
# rubocop:disable Rails/TimeZone
require 'json' require 'json'
require 'session_helper' require 'session_helper'
@ -33,7 +35,7 @@ returns
session_file = "#{path_tmp}/session" session_file = "#{path_tmp}/session"
# collect session data # collect session data
meta[:last_ping] = Time.zone.now.to_i.to_s meta[:last_ping] = Time.now.utc.to_i.to_s
data = { data = {
user: session, user: session,
meta: meta, meta: meta,
@ -189,7 +191,7 @@ returns
list_of_closed_sessions = [] list_of_closed_sessions = []
clients = Sessions.list clients = Sessions.list
clients.each { |client_id, client| clients.each { |client_id, client|
if !client[:meta] || !client[:meta][:last_ping] || ( client[:meta][:last_ping].to_i + idle_time_in_sec ) < Time.zone.now.to_i if !client[:meta] || !client[:meta][:last_ping] || ( client[:meta][:last_ping].to_i + idle_time_in_sec ) < Time.now.utc.to_i
list_of_closed_sessions.push client_id list_of_closed_sessions.push client_id
Sessions.destory( client_id ) Sessions.destory( client_id )
end end
@ -213,7 +215,7 @@ returns
data = get(client_id) data = get(client_id)
return false if !data return false if !data
path = "#{@path}/#{client_id}" path = "#{@path}/#{client_id}"
data[:meta][:last_ping] = Time.zone.now.to_i.to_s data[:meta][:last_ping] = Time.now.utc.to_i.to_s
content = data.to_json content = data.to_json
File.open( path + '/session', 'wb' ) { |file| File.open( path + '/session', 'wb' ) { |file|
file.write content file.write content
@ -289,7 +291,7 @@ returns
def self.send( client_id, data ) def self.send( client_id, data )
path = "#{@path}/#{client_id}/" path = "#{@path}/#{client_id}/"
filename = "send-#{ Time.zone.now.to_f }" filename = "send-#{ Time.now.utc.to_f }"
check = true check = true
count = 0 count = 0
while check while check
@ -422,11 +424,11 @@ returns
def self.spool_create( msg ) def self.spool_create( msg )
path = "#{@path}/spool/" path = "#{@path}/spool/"
FileUtils.mkpath path FileUtils.mkpath path
file_path = path + "/#{Time.zone.now.to_f}-#{rand(99_999)}" file_path = path + "/#{Time.now.utc.to_f}-#{rand(99_999)}"
File.open( file_path, 'wb' ) { |file| File.open( file_path, 'wb' ) { |file|
data = { data = {
msg: msg, msg: msg,
timestamp: Time.zone.now.to_i, timestamp: Time.now.utc.to_i,
} }
file.write data.to_json file.write data.to_json
} }
@ -457,7 +459,7 @@ returns
end end
# ignore message older then 48h # ignore message older then 48h
if spool['timestamp'] + (2 * 86_400) < Time.zone.now.to_i if spool['timestamp'] + (2 * 86_400) < Time.now.utc.to_i
to_delete.push "#{path}/#{entry}" to_delete.push "#{path}/#{entry}"
next next
end end
@ -568,7 +570,7 @@ returns
=end =end
def self.thread_client(client_id, try_count = 0, try_run_time = Time.zone.now) def self.thread_client(client_id, try_count = 0, try_run_time = Time.now.utc)
Rails.logger.debug "LOOP #{client_id} - #{try_count}" Rails.logger.debug "LOOP #{client_id} - #{try_count}"
begin begin
Sessions::Client.new(client_id) Sessions::Client.new(client_id)
@ -586,10 +588,10 @@ returns
try_count += 1 try_count += 1
# reset error counter if to old # reset error counter if to old
if try_run_time + ( 60 * 5 ) < Time.zone.now if try_run_time + ( 60 * 5 ) < Time.now.utc
try_count = 0 try_count = 0
end end
try_run_time = Time.zone.now try_run_time = Time.now.utc
# restart job again # restart job again
if try_run_max > try_count if try_run_max > try_count

View file

@ -96,7 +96,7 @@ EventMachine.run {
if !@clients.include? client_id if !@clients.include? client_id
@clients[client_id] = { @clients[client_id] = {
websocket: ws, websocket: ws,
last_ping: Time.now, last_ping: Time.now.utc.iso8601,
error_count: 0, error_count: 0,
} }
end end
@ -140,7 +140,7 @@ EventMachine.run {
# error handling # error handling
if data['timestamp'] if data['timestamp']
log 'notice', "request spool data > '#{Time.at(data['timestamp'])}'", client_id log 'notice', "request spool data > '#{Time.at(data['timestamp']).utc.iso8601}'", client_id
else else
log 'notice', 'request spool with init data', client_id log 'notice', 'request spool with init data', client_id
end end
@ -165,7 +165,7 @@ EventMachine.run {
# send spool:sent event to client # send spool:sent event to client
log 'notice', 'send spool:sent event', client_id log 'notice', 'send spool:sent event', client_id
@clients[client_id][:websocket].send( '[{"event":"spool:sent","data":{"timestamp":' + Time.now.to_i.to_s + '}}]' ) @clients[client_id][:websocket].send( '[{"event":"spool:sent","data":{"timestamp":' + Time.now.utc.iso8601.to_i.to_s + '}}]' )
end end
# get session # get session
@ -176,7 +176,7 @@ EventMachine.run {
# remember ping, send pong back # remember ping, send pong back
elsif data['action'] == 'ping' elsif data['action'] == 'ping'
Sessions.touch(client_id) Sessions.touch(client_id)
@clients[client_id][:last_ping] = Time.now @clients[client_id][:last_ping] = Time.now.utc.iso8601
@clients[client_id][:websocket].send( '[{"action":"pong"}]' ) @clients[client_id][:websocket].send( '[{"action":"pong"}]' )
# broadcast # broadcast
@ -299,7 +299,7 @@ EventMachine.run {
# close unused web socket sessions # close unused web socket sessions
@clients.each { |client_id, client| @clients.each { |client_id, client|
next if ( client[:last_ping] + idle_time_in_sec ) >= Time.now next if ( client[:last_ping] + idle_time_in_sec ) >= Time.now.utc.iso8601
log 'notice', 'closing idle websocket connection', client_id log 'notice', 'closing idle websocket connection', client_id
@ -325,8 +325,8 @@ EventMachine.run {
if !@options[:v] if !@options[:v]
return if level == 'debug' return if level == 'debug'
end end
puts "#{Time.now}:client(#{ client_id }) #{ data }" puts "#{Time.now.utc.iso8601}:client(#{ client_id }) #{ data }"
# puts "#{Time.now}:#{ level }:client(#{ client_id }) #{ data }" # puts "#{Time.now.utc.iso8601}:#{ level }:client(#{ client_id }) #{ data }"
end end
} }