Skip to content

Commit 1c705e3

Browse files
committed
touch up
1 parent 1bdae40 commit 1c705e3

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

mercury/platform.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def graceful_shutdown(signum, frame):
347347
self.log.info("To stop this application, press Control-C")
348348
while self.running:
349349
time.sleep(0.1)
350-
self.log.info("Bye")
350+
# exit forever loop and ask platform to end event loop
351351
self.stop()
352352
else:
353353
raise ValueError('Unable to register Control-C and KILL signals because this is not the main thread')
@@ -562,29 +562,31 @@ def stop(self):
562562
# to allow user application to invoke the "stop" method from a registered service,
563563
# the system must start a new thread so that the service can finish first.
564564
#
565-
threading.Thread(target=self._bye).start()
566-
567-
def _bye(self):
568565
if not self.stopped:
566+
self.log.info('Bye')
569567
# guarantee this stop function to execute only once
570568
self.stopped = True
571569
# exit the run_forever loop if any
572570
self.running = False
571+
# in case the calling function has just send an event asynchronously
572+
time.sleep(0.5)
573+
threading.Thread(target=self._bye).start()
573574

574-
def stopping():
575-
route_list = []
576-
for route in self.get_routes():
577-
route_list.append(route)
578-
for route in route_list:
579-
self._remove_route(route)
580-
self._loop.create_task(full_stop())
581-
582-
async def full_stop():
583-
# give time for registered services to stop
584-
await asyncio.sleep(1.0)
585-
queue_dir = self.util.normalize_path(self.work_dir + "/queues/" + self.get_origin())
586-
self.util.cleanup_dir(queue_dir)
587-
self._loop.stop()
588-
589-
self._cloud.close_connection(1000, 'bye', stop_engine=True)
590-
self._loop.call_soon_threadsafe(stopping)
575+
def _bye(self):
576+
def stopping():
577+
route_list = []
578+
for route in self.get_routes():
579+
route_list.append(route)
580+
for route in route_list:
581+
self._remove_route(route)
582+
self._loop.create_task(full_stop())
583+
584+
async def full_stop():
585+
# give time for registered services to stop
586+
await asyncio.sleep(1.0)
587+
queue_dir = self.util.normalize_path(self.work_dir + "/queues/" + self.get_origin())
588+
self.util.cleanup_dir(queue_dir)
589+
self._loop.stop()
590+
591+
self._cloud.close_connection(1000, 'bye', stop_engine=True)
592+
self._loop.call_soon_threadsafe(stopping)

0 commit comments

Comments
 (0)