Allow running scheduled jobs once

This commit is contained in:
Jake Howard 2019-08-30 22:20:32 +01:00
parent 9fd854dfe4
commit 2ef14e8d9e
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -1,25 +1,40 @@
import argparse
import logging import logging
from multiprocessing import Process from multiprocessing import Process
import sentry_sdk import sentry_sdk
from apscheduler.util import get_callable_name
from sentry_sdk.integrations.aiohttp import AioHttpIntegration from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from actioner.scheduler import create_scheduler from actioner.scheduler import create_scheduler
from actioner.settings import LOGGING_LEVEL, SENTRY_DSN from actioner.settings import LOGGING_LEVEL, SENTRY_DSN
from actioner.web import get_server, run_server 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(): def main():
logging.basicConfig(level=LOGGING_LEVEL) logging.basicConfig(level=LOGGING_LEVEL)
sentry_sdk.init(dsn=SENTRY_DSN, integrations=[AioHttpIntegration()]) sentry_sdk.init(dsn=SENTRY_DSN, integrations=[AioHttpIntegration()])
server = get_server() args = parse_args()
scheduler = create_scheduler()
# HACK: APScheduler doesn't like running in an external event loop. https://github.com/agronholm/apscheduler/issues/360 if args.once:
Process(target=run_server, args=(server,)).start() scheduler = create_scheduler()
Process(target=scheduler.start).start() 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__": if __name__ == "__main__":