birb-scheduler/birb_scheduler/main.py

69 lines
1.7 KiB
Python
Raw Normal View History

2021-12-12 00:42:00 +01:00
#!/usr/bin/env pyton3
import logging
import sys
2021-12-12 02:07:26 +01:00
from typing import Dict
2021-12-12 00:42:00 +01:00
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
from config import Config
from apscheduler.schedulers.blocking import BlockingScheduler
2021-12-12 02:07:26 +01:00
import redis
import json
import run_scheduler
from functools import cache
@cache
def get_site_url_map() -> Dict[str, str]:
return {p.name: p.baseurl for p in Config.SITE_LIST}
def put_site_url_map_into_redis(redis_client: redis.Redis):
redis_client.set("SITEURLMAP", json.dumps(get_site_url_map()).encode('utf-8'))
def run_wrapper(redis_client: redis.Redis):
put_site_url_map_into_redis(redis_client)
run_scheduler.run(redis_client, get_site_url_map())
2021-12-12 00:42:00 +01:00
def main():
sentry_logging = LoggingIntegration(
level=logging.INFO,
event_level=logging.ERROR
)
if Config.SENTRY_DSN:
sentry_sdk.init(
dsn=Config.SENTRY_DSN,
integrations=[sentry_logging],
traces_sample_rate=0.0,
send_default_pii=True,
release=Config.RELEASE_ID,
environment=Config.RELEASEMODE,
_experiments={"auto_enabling_integrations": True}
)
logging.basicConfig(
stream=sys.stdout,
format="%(asctime)s - %(name)s [%(levelname)s]: %(message)s",
level=logging.DEBUG if Config.DEBUG else logging.INFO
)
2021-12-12 02:07:26 +01:00
redis_client = redis.from_url(Config.REDIS_URL)
put_site_url_map_into_redis(redis_client)
2021-12-12 00:42:00 +01:00
scheduler = BlockingScheduler()
2021-12-12 02:07:26 +01:00
scheduler.add_job(lambda: run_wrapper(redis_client), trigger='interval', seconds=Config.INTERVAL)
2021-12-12 00:59:00 +01:00
try:
scheduler.start()
except KeyboardInterrupt:
pass
scheduler.shutdown()
2021-12-12 00:42:00 +01:00
if __name__ == '__main__':
main()