diff --git a/docker/scheduler.py b/docker/scheduler.py new file mode 100644 index 0000000..9012f37 --- /dev/null +++ b/docker/scheduler.py @@ -0,0 +1,27 @@ +# This module allows to customize the scheduling logic for mobilizon-reshare. It's not intended to be part of the +# supported code base but just an example on how to schedule the commands of mobilizon-reshare +import asyncio +from functools import partial + +from apscheduler.schedulers.asyncio import AsyncIOScheduler + +from mobilizon_reshare.cli import _safe_execution +from mobilizon_reshare.cli.commands.start.main import start + +sched = AsyncIOScheduler() + +# Runs "start" from Monday to Friday every 15 mins +sched.add_job( + partial(_safe_execution, start), + "cron", + day_of_week="mon-fri", + minute="*/15", + hour="10-18", +) +# Runs "recap" once a week +sched.add_job(partial(_safe_execution, start), "cron", day_of_week="mon", hour="11") +sched.start() +try: + asyncio.get_event_loop().run_forever() +except (KeyboardInterrupt, SystemExit): + pass diff --git a/mobilizon_reshare/cli/__init__.py b/mobilizon_reshare/cli/__init__.py index 8afa2ad..bd78ca2 100644 --- a/mobilizon_reshare/cli/__init__.py +++ b/mobilizon_reshare/cli/__init__.py @@ -10,9 +10,8 @@ from mobilizon_reshare.storage.db import tear_down, MoReDB logger = logging.getLogger(__name__) -async def graceful_exit(code): +async def graceful_exit(): await tear_down() - exit(code) async def init(): @@ -33,8 +32,10 @@ async def _safe_execution(f): traceback.print_exc() finally: logger.debug("Closing") - await graceful_exit(return_code) + await graceful_exit() + return return_code def safe_execution(f): - asyncio.run(_safe_execution(f)) + code = asyncio.run(_safe_execution(f)) + exit(code)