From 1ef4a3bc150d78761194078397255a33b6165679 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Sat, 19 May 2018 17:28:03 +0200 Subject: [PATCH] Handle connected clients on unexpected shutdown caused, for example, by a server crash. Could be triggered by an incorrectly named logfile as well because it is assumed once there are connected clients in a logfile which isn't the last one to be parsed. The fix is realized by taking the timestamp of the last event from the currently parsed logfile and disconnecting all still connected clients on that timestamp. --- tsstats/log.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/tsstats/log.py b/tsstats/log.py index 7dabf33..7a322f5 100644 --- a/tsstats/log.py +++ b/tsstats/log.py @@ -140,8 +140,9 @@ def parse_logs(log_glob, ident_map=None, online_dc=True): logger.debug('Started parsing of %s', f.name) # parse logfile line by line and filter lines without events events = filter(None, map(_parse_line, f)) + all_events = list(itertools.chain.from_iterable(events)) # chain apply events to Client-obj - clients.apply_events(itertools.chain.from_iterable(events)) + clients.apply_events(all_events) # find connected clients online_clients = list( @@ -152,12 +153,32 @@ def parse_logs(log_glob, ident_map=None, online_dc=True): logger.debug( 'Some clients are still connected: %s', online_clients ) - if index == len(logs) - 1 and online_dc: - logger.debug('Last log => disconnecting online clients') - # last iteration => disconnect online clients if desired - for online_client in online_clients: - online_client.disconnect(pendulum.utcnow()) - online_client.connected += 1 + if index == len(logs) - 1: + if online_dc: + logger.debug( + 'Last log => disconnecting online clients' + ) + # last iteration + # => disconnect online clients if desired + for online_client in online_clients: + online_client.disconnect(pendulum.utcnow()) + online_client.connected += 1 + else: + logger.warn( + 'Server didn\'t disconnect all clients on shutdown' + ' or logfile is incorrectly named/corrupted (%s).' + ' Check debuglog for details', + f.name + ) + logger.debug( + 'Will handle this by disconnecting all clients on' + ' last event timestamp' + ) + last_event_timestamp = all_events[-1].timestamp + logger.debug( + 'Last event timestamp: %s', last_event_timestamp) + for online_client in online_clients: + online_client.disconnect(last_event_timestamp) logger.debug('Finished parsing of %s', f.name) if len(clients) >= 1: