Skip to content

Commit

Permalink
feat: 优化端口被占用时的处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
helloplhm-qwq committed Jan 5, 2024
1 parent 30e1e6c commit 8c4d3c4
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,22 @@ async def handle_404(request):


async def run_app():
host = config.read_config('common.host')
port = int(config.read_config('common.port'))
runner = aiohttp.web.AppRunner(app)
await runner.setup()
site = aiohttp.web.TCPSite(runner, host, port)
await site.start()
logger.info(f"监听 -> http://{host}:{port}")
while True:
try:
host = config.read_config('common.host')
port = int(config.read_config('common.port'))
runner = aiohttp.web.AppRunner(app)
await runner.setup()
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():
await scheduler.run()
Expand All @@ -164,11 +173,8 @@ async def initMain():
except (KeyboardInterrupt, stopEvent):
pass
except OSError as e:
if str(e).startswith("[Errno 98]"):
logger.error("端口已被占用,请检查\n" + str(e))
else:
logger.error("遇到未知错误,请查看日志")
logger.error(traceback.format_exc())
logger.error("遇到未知错误,请查看日志")
logger.error(traceback.format_exc())
except:
logger.error("遇到未知错误,请查看日志")
logger.error(traceback.format_exc())
Expand Down

0 comments on commit 8c4d3c4

Please sign in to comment.