-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
55 lines (40 loc) · 1.53 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
55
import threading
import uvicorn
from fastapi import FastAPI
from lib import log, redis
from lib.triggers import api
from lib.triggers.subscriber import Subscriber
from src.config import config
from src.forwarder.batch import BatchForwarder
from src.system.manager import SystemManager
cfg = config.Config()
logger = log.Logger.new()
cache = redis.Client.connect(cfg.cache.URL)
pubsub = redis.Client.connect(cfg.pubsub.URL).pubsub()
app = FastAPI()
if __name__ == "__main__":
done = threading.Event()
manager = SystemManager(logger, cache, cfg.manager)
forwarder = BatchForwarder(logger, manager, cache, cfg.forwarder, done)
subscriber = Subscriber(manager)
topic = api.device(cfg.version, cfg.manager.DEVICE_NAME)
try:
pubsub.subscribe(**{topic: subscriber.receive})
# TODO: Handle exception in thread using https://github.com/andymccurdy/redis-py/pull/1395
pubsub_thread = pubsub.run_in_thread()
logger.info("manager.subscribe.started", topic=topic)
forwarder.start()
logger.error("data.forwarder.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()
pubsub_thread.stop()
logger.info("manager.subscribe.stopped")
forwarder.join()
logger.error("data.forwarder.stopped")