Skip to content

Commit

Permalink
Send sessions synchronously in atexit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed Jan 24, 2024
1 parent e02b68f commit d0c0c92
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
12 changes: 7 additions & 5 deletions bugsnag/delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def deliver(self, config, payload: Any, options={}):
"""
pass

def deliver_sessions(self, config, payload: Any):
def deliver_sessions(self, config, payload: Any, options=None):
"""
Sends sessions to Bugsnag
"""
Expand All @@ -72,10 +72,12 @@ def deliver_sessions(self, config, payload: Any):
'No sessions will be sent to Bugsnag.')
self.sent_session_warning = True
else:
options = {
'endpoint': config.session_endpoint,
'success': 202,
}
if options is None:
options = {}

options['endpoint'] = config.session_endpoint
options['success'] = 202

self.deliver(config, payload, options)

def queue_request(self, request: Callable, config, options: Dict):
Expand Down
24 changes: 19 additions & 5 deletions bugsnag/sessiontracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def start_session(self):
_session_info.set(new_session)
self.__queue_session(start_time)

def send_sessions(self):
def send_sessions(self, asynchronous=True):
self.mutex.acquire()
try:
sessions = []
Expand All @@ -66,7 +66,8 @@ def send_sessions(self):
self.session_counts = {}
finally:
self.mutex.release()
self.__deliver(sessions)

self.__deliver(sessions, asynchronous)

def __start_delivery(self):
if self.delivery_thread is None:
Expand All @@ -83,7 +84,8 @@ def deliver():
def cleanup():
if self.delivery_thread is not None:
self.delivery_thread.cancel()
self.send_sessions()

self.send_sessions(asynchronous=False)

atexit.register(cleanup)

Expand All @@ -96,7 +98,7 @@ def __queue_session(self, start_time: str):
finally:
self.mutex.release()

def __deliver(self, sessions: List[Dict]):
def __deliver(self, sessions: List[Dict], asynchronous=True):
if not sessions:
self.config.logger.debug("No sessions to deliver")
return
Expand Down Expand Up @@ -132,7 +134,19 @@ def __deliver(self, sessions: List[Dict]):
)

encoded_payload = encoder.encode(payload)
self.config.delivery.deliver_sessions(self.config, encoded_payload)

deliver = self.config.delivery.deliver_sessions

if 'options' in deliver.__code__.co_varnames:
deliver(
self.config,
encoded_payload,
options={'asynchronous': asynchronous}
)
else:
deliver(self.config, encoded_payload)


except Exception as e:
self.config.logger.exception('Sending sessions failed %s', e)

Expand Down

0 comments on commit d0c0c92

Please sign in to comment.