From 2ef14e8d9e290c40935ca3cf2a9be2714373dfbc Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 30 Aug 2019 22:20:32 +0100 Subject: [PATCH] Allow running scheduled jobs once --- actioner/main.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/actioner/main.py b/actioner/main.py index da7c83d..fd93e5e 100644 --- a/actioner/main.py +++ b/actioner/main.py @@ -1,25 +1,40 @@ +import argparse import logging from multiprocessing import Process import sentry_sdk +from apscheduler.util import get_callable_name from sentry_sdk.integrations.aiohttp import AioHttpIntegration from actioner.scheduler import create_scheduler from actioner.settings import LOGGING_LEVEL, SENTRY_DSN from actioner.web import get_server, run_server +logger = logging.getLogger(__name__) + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument("--once", action="store_true") + return parser.parse_args() + def main(): logging.basicConfig(level=LOGGING_LEVEL) sentry_sdk.init(dsn=SENTRY_DSN, integrations=[AioHttpIntegration()]) - server = get_server() - scheduler = create_scheduler() + args = parse_args() - # HACK: APScheduler doesn't like running in an external event loop. https://github.com/agronholm/apscheduler/issues/360 - Process(target=run_server, args=(server,)).start() - Process(target=scheduler.start).start() + if args.once: + scheduler = create_scheduler() + jobs = {job.func for job in scheduler.get_jobs()} + for job in jobs: + logger.info("Executing '{}'".format(get_callable_name(job))) + job() + else: + Process(target=run_server, args=(get_server(),)).start() + Process(target=create_scheduler().start).start() if __name__ == "__main__":