1+ import asyncio
12from core .cluster import Cluster
23from core .config import Config
3- import asyncio
4- import signal
4+ from core .logger import logger
55
66cluster = Cluster ()
77
8-
98async def main ():
10- await cluster .token .fetchToken ()
11- await cluster .getConfiguration ()
12- await cluster .fetchFileList ()
13- await cluster .init ()
14- await cluster .checkStorages ()
15- missing_filelist = await cluster .getMissingFiles ()
16- delay = Config .get ("advanced.delay" )
17- retry = Config .get ("advanced.retry" )
18- await cluster .syncFiles (missing_filelist , retry , delay )
19- await cluster .connect ()
20- protocol = "http" if Config .get ("cluster.byoc" ) else "https"
21- if protocol == "https" :
22- await cluster .socket .requestCertificate ()
23- await cluster .setupRouter ()
24- await cluster .listen (protocol == "https" , Config .get ("cluster.port" ))
25- await cluster .enable ()
26- while True :
27- await asyncio .sleep (3600 )
9+ try :
10+ await cluster .token .fetchToken ()
11+ await cluster .getConfiguration ()
12+ await cluster .fetchFileList ()
13+ await cluster .init ()
14+ await cluster .checkStorages ()
15+ missing_filelist = await cluster .getMissingFiles ()
16+ await cluster .syncFiles (missing_filelist , Config .get ("advanced.retry" ), Config .get ("advanced.delay" ))
17+ await cluster .connect ()
18+
19+ protocol = "http" if Config .get ("cluster.byoc" ) else "https"
20+ if protocol == "https" :
21+ await cluster .socket .requestCertificate ()
22+
23+ await cluster .setupRouter ()
24+ await cluster .listen (protocol == "https" , Config .get ("cluster.port" ))
25+ await cluster .enable ()
26+
27+ while True :
28+ await asyncio .sleep (3600 )
2829
30+ except asyncio .CancelledError :
31+ logger .tinfo ('main.info.stopping' )
32+ if cluster .enabled :
33+ await cluster .disable ()
34+ if cluster .socket :
35+ await cluster .socket .socket .disconnect ()
36+ if cluster .site :
37+ await cluster .site .stop ()
38+ logger .tsuccess ("main.success.stopped" )
2939
3040def init ():
31- asyncio .run (main ())
41+ loop = asyncio .get_event_loop ()
42+ main_task = loop .create_task (main ())
43+ try :
44+ loop .run_until_complete (main_task )
45+ except KeyboardInterrupt :
46+ main_task .cancel ()
47+ loop .run_until_complete (asyncio .shield (main_task ))
48+ finally :
49+ loop .close ()
0 commit comments