-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
54 lines (40 loc) · 1.36 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import threading
from queue import Queue
import uvicorn
from fastapi import FastAPI
from lib import log, redis
from plugins import registry
from src.triggers.publisher import TriggerPublisher
from src.config import config
from src.routers import triggers
cfg = config.Config()
logger = log.Logger.new()
cache = redis.Client.connect(cfg.cache.URL)
pubsub = redis.Client.connect(cfg.pubsub.URL)
queue = Queue()
app = FastAPI()
app.include_router(triggers.router)
if __name__ == "__main__":
done = threading.Event()
publisher = TriggerPublisher(logger, cache, pubsub, cfg.publisher, queue, done)
watchers = registry.load(logger, cache, queue, done)
try:
publisher.start()
logger.info("trigger.publisher.started")
for plugin, watcher in watchers.items():
watcher.start()
logger.info(f"{plugin}.started")
logger.info("server.started", port=cfg.server.PORT)
uvicorn.run(app, host=cfg.server.HOST, port=cfg.server.PORT)
logger.info("server.stopped")
except Exception as err:
logger.error("service.fatal", error=err)
except KeyboardInterrupt:
pass
finally:
done.set()
publisher.join()
logger.info("trigger.publisher.stopped")
for plugin, watcher in watchers.items():
watcher.join()
logger.info(f"{plugin}.stopped")