added scheduling (#133)

This commit is contained in:
Simone Robutti 2022-01-27 20:54:14 +01:00 committed by GitHub
parent 1f1ff2e5c2
commit 8c16812ed0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

27
docker/scheduler.py Normal file
View File

@ -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

View File

@ -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)