feat: 优化端口被占用时的处理逻辑

This commit is contained in:
helloplhm-qwq
2024-01-05 17:56:35 +08:00
parent 30e1e6cf22
commit 8c4d3c4c07

30
main.py
View File

@ -146,13 +146,22 @@ app.router.add_route('*', '/{tail:.*}', handle_404)
async def run_app(): async def run_app():
host = config.read_config('common.host') while True:
port = int(config.read_config('common.port')) try:
runner = aiohttp.web.AppRunner(app) host = config.read_config('common.host')
await runner.setup() port = int(config.read_config('common.port'))
site = aiohttp.web.TCPSite(runner, host, port) runner = aiohttp.web.AppRunner(app)
await site.start() await runner.setup()
logger.info(f"监听 -> http://{host}:{port}") site = aiohttp.web.TCPSite(runner, host, port)
await site.start()
logger.info(f"监听 -> http://{host}:{port}")
return
except OSError as e:
if str(e).startswith("[Errno 98]"):
logger.error("端口已被占用,请检查\n" + str(e))
logger.info('服务器将在10s后再次尝试启动...')
await asyncio.sleep(10)
logger.info('重新尝试启动...')
async def initMain(): async def initMain():
await scheduler.run() await scheduler.run()
@ -164,11 +173,8 @@ async def initMain():
except (KeyboardInterrupt, stopEvent): except (KeyboardInterrupt, stopEvent):
pass pass
except OSError as e: except OSError as e:
if str(e).startswith("[Errno 98]"): logger.error("遇到未知错误,请查看日志")
logger.error("端口已被占用,请检查\n" + str(e)) logger.error(traceback.format_exc())
else:
logger.error("遇到未知错误,请查看日志")
logger.error(traceback.format_exc())
except: except:
logger.error("遇到未知错误,请查看日志") logger.error("遇到未知错误,请查看日志")
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())