Skip to content

Commit

Permalink
fix on_stop
Browse files Browse the repository at this point in the history
  • Loading branch information
Mips2648 committed May 10, 2024
1 parent 35fe432 commit 854be8c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
14 changes: 3 additions & 11 deletions jeedomdaemon/base_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def run(self):
asyncio.run(self.__run())
except Exception as e: # pylint: disable=broad-exception-caught
self._logger.error('Fatal error: %s', e)
self.stop()
finally:
self._logger.info("Shutdown")
try:
Expand Down Expand Up @@ -89,20 +88,13 @@ async def __run(self):

await self.__listen_task

def stop(self):
async def stop(self):
""" Stop your daemon if need be"""

if self.__on_stop_cb is not None:
self._logger.debug("create on stop callback task")
stop_task = asyncio.create_task(self.__on_stop_cb())
try:
asyncio.gather(stop_task, return_exceptions=True)
except BaseException as e: # pylint: disable=broad-exception-caught
self._logger.warning("Some exception occured during cancellation: %s", e)
self._logger.debug("on stop callback task done")
await self.__on_stop_cb()

tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
tasks = asyncio.all_tasks()
self._logger.info("Cancelling %i outstanding tasks", len(tasks))
for task in tasks:
task.cancel()
Expand All @@ -113,7 +105,7 @@ def stop(self):

def __ask_exit(self, sig):
self._logger.info("Signal %i caught, exiting...", sig)
self.stop()
asyncio.run_coroutine_threadsafe(self.stop(), self._loop)

async def __add_signal_handler(self):
self._loop.add_signal_handler(signal.SIGINT, functools.partial(self.__ask_exit, signal.SIGINT))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# Needed for dependencies
install_requires=['aiohttp'],
# *strongly* suggested for sharing
version='0.8.3',
version='0.8.4',
# The license can be anything you like
license='MIT',
description='A base to implement Jeedom daemon in python',
Expand Down

0 comments on commit 854be8c

Please sign in to comment.