-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
42 lines (32 loc) · 1.2 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
import uvicorn
from fastapi import FastAPI
from lib import log, redis
from lib.triggers.subscriber import Subscriber
from src.config import config
from src.protocols.builder import ProtocolBuilder
from src.routers import builds
cfg = config.Config()
logger = log.Logger.new()
cache = redis.Client.connect(cfg.cache.URL)
pubsub = redis.Client.connect(cfg.pubsub.URL).pubsub()
app = FastAPI()
app.include_router(builds.router)
if __name__ == "__main__":
builder = ProtocolBuilder(logger, cache, cfg.builder)
subscriber = Subscriber(builder)
topic = cfg.version
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("builder.subscribe.started", topic=topic)
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:
pubsub_thread.stop()
logger.info("builder.subscribe.stopped")