forked from lAmeR1/kaspa-socket-server
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
56 lines (42 loc) · 1.39 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
56
# encoding: utf-8
import asyncio
import os
from asyncio import Task, InvalidStateError
from fastapi_utils.tasks import repeat_every
from starlette.responses import RedirectResponse
import sockets
from server import app, karlsend_client
from sockets import blocks
from sockets.blockdag import periodical_blockdag
from sockets.bluescore import periodical_blue_score
from sockets.coinsupply import periodic_coin_supply
print(
f"Loaded: {sockets.join_room}"
f"{periodic_coin_supply} {periodical_blockdag} {periodical_blue_score}")
BLOCKS_TASK = None # type: Task
@app.on_event("startup")
async def startup():
global BLOCKS_TASK
# find karlsend before staring webserver
await karlsend_client.initialize_all()
BLOCKS_TASK = asyncio.create_task(blocks.config())
@app.on_event("startup")
@repeat_every(seconds=5)
async def watchdog():
global BLOCKS_TASK
try:
exception = BLOCKS_TASK.exception()
except InvalidStateError:
pass
else:
print(f"Watch found an error! {exception}\n"
f"Reinitialize karlsends and start task again")
await karlsend_client.initialize_all()
BLOCKS_TASK = asyncio.create_task(blocks.config())
@app.get("/", include_in_schema=False)
async def docs_redirect():
return RedirectResponse(url='/docs')
if __name__ == '__main__':
if os.getenv("DEBUG"):
import uvicorn
uvicorn.run(app)