7
7
8
8
from asyncio import CancelledError
9
9
10
- #TODO: These imports are duplicated over the abstract class to avoid errors in the Async version
10
+ # TODO: These imports are duplicated over the abstract class to avoid errors in the Async version
11
11
from .exceptions .UnauthorizedException import UnauthorizedException
12
12
from .exceptions .APIClientErrorException import APIClientErrorException
13
13
from .exceptions .DatafeedExpiredException import DatafeedExpiredException
@@ -215,13 +215,13 @@ async def deactivate_datafeed(self, wait_for_handler_completions=True):
215
215
log .debug ('AsyncDataFeedEventService/deactivate_datafeed()' )
216
216
if wait_for_handler_completions :
217
217
log .debug ('AsyncDataFeedEventService/deactivate_datafeed() --> '
218
- 'Waiting for {} events to finish' .format (self .queue .qsize ()))
218
+ 'Waiting for {} events to finish' .format (self .queue .qsize ()))
219
219
await self .queue .join ()
220
220
log .debug ('AsyncDataFeedEventService/deactivate_datafeed() --> '
221
- 'Deactivating' )
221
+ 'Deactivating' )
222
222
else :
223
223
log .debug ('AsyncDataFeedEventService/deactivate_datafeed() --> '
224
- '{} events still being handled, deactivating anyway' .format (self .queue .qsize ()))
224
+ '{} events still being handled, deactivating anyway' .format (self .queue .qsize ()))
225
225
226
226
if not self .stop :
227
227
self .stop = True
@@ -314,25 +314,30 @@ def _process_full_trace(self, id):
314
314
Use with messagedId if available
315
315
"""
316
316
317
- if self .trace_enabled :
318
- try :
319
- intermediates = self .trace_dict [id ]
320
- assert len (intermediates ) == 4
321
- trace = EventTrace (id , intermediates [0 ], intermediates [1 ], intermediates [2 ],
322
- intermediates [3 ])
323
- total_time = intermediates [3 ] - intermediates [0 ]
324
- time_in_bot = intermediates [3 ] - intermediates [1 ]
325
-
326
- # This just writes out total seconds instead of formatting into minutes and hours
327
- # for a typical bot response this seems reasonable
328
- log .debug ("Responded to message in: {:.4g}s. Including {:.4g}s inside the bot"
329
- .format (total_time .total_seconds (), time_in_bot .total_seconds ()))
330
- if self .trace_recorder is not None :
331
- self .trace_recorder .append (trace )
332
- del self .trace_dict [id ]
333
- except Exception as exc :
334
- log .error ("Error while computing trace results for id: " + id )
335
- log .exception (exc )
317
+ if not self .trace_enabled :
318
+ return
319
+
320
+ try :
321
+ intermediates = self .trace_dict [id ]
322
+ if len (intermediates ) != 4 :
323
+ log .error ("Error while computing trace results for id: " + id + ": trace item should have 4 elements" )
324
+ return
325
+
326
+ trace = EventTrace (id , intermediates [0 ], intermediates [1 ], intermediates [2 ],
327
+ intermediates [3 ])
328
+ total_time = intermediates [3 ] - intermediates [0 ]
329
+ time_in_bot = intermediates [3 ] - intermediates [1 ]
330
+
331
+ # This just writes out total seconds instead of formatting into minutes and hours
332
+ # for a typical bot response this seems reasonable
333
+ log .debug ("Responded to message in: {:.4g}s. Including {:.4g}s inside the bot"
334
+ .format (total_time .total_seconds (), time_in_bot .total_seconds ()))
335
+ if self .trace_recorder is not None :
336
+ self .trace_recorder .append (trace )
337
+ del self .trace_dict [id ]
338
+ except Exception as exc :
339
+ log .error ("Error while computing trace results for id: " + id )
340
+ log .exception (exc )
336
341
337
342
@staticmethod
338
343
def _get_event_id (event ):
@@ -348,16 +353,18 @@ def _add_trace(self, e_id, first_timestamp=None):
348
353
349
354
Use with messageId if available
350
355
"""
351
- if self .trace_enabled :
352
- if first_timestamp is not None :
353
- self .trace_dict [e_id ] = [make_datetime (first_timestamp )]
354
- try :
355
- self .trace_dict [e_id ].append (datetime .datetime .utcnow ())
356
- except KeyError :
357
- log .error (
358
- 'Error making traces for {}. Has the same messageId appeared'
359
- 'more than once?' .format (e_id )
360
- )
356
+ if not self .trace_enabled :
357
+ return
358
+
359
+ if first_timestamp is not None :
360
+ self .trace_dict [e_id ] = [make_datetime (first_timestamp )]
361
+ try :
362
+ self .trace_dict [e_id ].append (datetime .datetime .utcnow ())
363
+ except KeyError :
364
+ log .error (
365
+ 'Error making traces for {}. Has the same messageId appeared'
366
+ 'more than once?' .format (e_id )
367
+ )
361
368
362
369
async def handle_events (self ):
363
370
"""For each event resolve its handler and add it to the queue to be processed"""
0 commit comments