Skip to content

Commit 6359aa4

Browse files
committed
feat: 支持退出时下线操作
1 parent ac8307f commit 6359aa4

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

core/__init__.py

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
1+
import asyncio
12
from core.cluster import Cluster
23
from core.config import Config
3-
import asyncio
4-
import signal
4+
from core.logger import logger
55

66
cluster = Cluster()
77

8-
98
async 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

3040
def 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()

core/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ async def _() -> None:
2828
logger.tsuccess("client.success.connected")
2929

3030
@self.socket.on("disconnect")
31-
async def _(reason: str) -> None:
32-
logger.twarning("client.warn.disconnected", reason=reason)
31+
async def _() -> None:
32+
logger.twarning("client.warn.disconnected")
3333

3434
@self.socket.on("message")
3535
async def _(message: str) -> None:

core/cluster.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ async def syncFiles(
192192
for file in missing_filelist.files
193193
]
194194
await asyncio.gather(*tasks)
195-
196195
if not self.failed_filelist.files:
197196
logger.tsuccess("cluster.success.sync_files.downloaded")
198197
elif retry > 1:
@@ -315,12 +314,13 @@ async def callback(data: List[Any]):
315314
return
316315

317316
self.enabled = True
317+
logger.tsuccess("cluster.success.enable.enabled")
318318

319319
except Exception as e:
320320
logger.terror("cluster.error.enable.exception", e=e)
321321

322322
async def disable(self) -> None:
323-
if not self.socket:
323+
if not self.socket or not self.enabled:
324324
return
325325
logger.tinfo("cluster.info.disabling")
326326
future = asyncio.Future()
@@ -344,6 +344,7 @@ async def callback(data: List[Any]):
344344
if ack is not True:
345345
logger.terror("cluster.error.disable.failed")
346346

347+
logger.tsuccess("cluster.success.disable.disabled")
347348
except Exception as e:
348349
logger.terror("cluster.error.enable.exception", e=e)
349350

i18n/zh_cn.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"main.info.start": "启动中……",
3+
"main.info.stopping": "停止程序中……",
4+
"main.success.stopped": "成功终止程序,再见!",
35
"token.info.fetching": "正在获取 Token……",
46
"token.success.fetched": "成功获取 Token!有效期:${ttl} 小时。",
57
"storage.info.check": "储存测试中……",
@@ -36,8 +38,10 @@
3638
"cluster.tqdm.desc.sync_files": "同步文件中",
3739
"cluster.tqdm.unit.files": " 个文件",
3840
"client.success.connected": "成功与主控建立连接!",
41+
"cluster.success.enable.enabled": "成功启用节点!",
42+
"cluster.success.disable.disabled": "成功禁用节点!",
3943
"client.info.message": "[主控] ${message}",
40-
"client.warn.disconnected": "与主控断开连接:${reason}",
44+
"client.warn.disconnected": "与主控断开连接",
4145
"cluster.error.disconnected": "未连接到服务器。",
4246
"client.error.exception": "[主控] ${error}",
4347
"client.error.reconnect": "在尝试重连至主控时发生错误:${e}",

0 commit comments

Comments
 (0)