-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprestart.py
75 lines (61 loc) · 1.67 KB
/
prestart.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import logging
import asyncio
import subprocess
from tenacity import (
after_log,
before_log,
retry,
stop_after_attempt,
wait_fixed,
)
from tortoise import Tortoise
from app.api import Addino, Request
from app import config
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
max_tries = 60 * 5 # 5 minutes
wait_seconds = 1
@retry(
stop=stop_after_attempt(max_tries),
wait=wait_fixed(wait_seconds),
before=before_log(logger, logging.INFO),
after=after_log(logger, logging.WARN),
)
async def db_pre_init():
try:
db = Tortoise()
await db.init(config=config.TORTOISE_ORM)
except Exception as e:
logger.error(e)
raise e
finally:
await db.close_connections()
@retry(
stop=stop_after_attempt(max_tries),
wait=wait_fixed(wait_seconds),
before=before_log(logger, logging.INFO),
after=after_log(logger, logging.WARN),
)
async def api_pre_init():
try:
api = Addino(Request())
result = await api.status()
if result.status == "online":
logger.info("API status check: ok")
except Exception as e:
logger.error(e)
raise e
finally:
await api.request.session.close()
def main() -> None:
loop = asyncio.get_event_loop()
logger.info("Initializing service: DB")
loop.run_until_complete(db_pre_init())
logger.info("Initializing service: api")
loop.run_until_complete(api_pre_init())
logger.info("Services finished initializing")
logger.info("Initializing migration")
subprocess.run(["aerich", "upgrade"])
logger.info("Migration finished")
if __name__ == "__main__":
main()