@@ -146,13 +146,22 @@ async def handle_404(request):
146
146
147
147
148
148
async def run_app ():
149
- host = config .read_config ('common.host' )
150
- port = int (config .read_config ('common.port' ))
151
- runner = aiohttp .web .AppRunner (app )
152
- await runner .setup ()
153
- site = aiohttp .web .TCPSite (runner , host , port )
154
- await site .start ()
155
- logger .info (f"监听 -> http://{ host } :{ port } " )
149
+ while True :
150
+ try :
151
+ host = config .read_config ('common.host' )
152
+ port = int (config .read_config ('common.port' ))
153
+ runner = aiohttp .web .AppRunner (app )
154
+ await runner .setup ()
155
+ site = aiohttp .web .TCPSite (runner , host , port )
156
+ await site .start ()
157
+ logger .info (f"监听 -> http://{ host } :{ port } " )
158
+ return
159
+ except OSError as e :
160
+ if str (e ).startswith ("[Errno 98]" ):
161
+ logger .error ("端口已被占用,请检查\n " + str (e ))
162
+ logger .info ('服务器将在10s后再次尝试启动...' )
163
+ await asyncio .sleep (10 )
164
+ logger .info ('重新尝试启动...' )
156
165
157
166
async def initMain ():
158
167
await scheduler .run ()
@@ -164,11 +173,8 @@ async def initMain():
164
173
except (KeyboardInterrupt , stopEvent ):
165
174
pass
166
175
except OSError as e :
167
- if str (e ).startswith ("[Errno 98]" ):
168
- logger .error ("端口已被占用,请检查\n " + str (e ))
169
- else :
170
- logger .error ("遇到未知错误,请查看日志" )
171
- logger .error (traceback .format_exc ())
176
+ logger .error ("遇到未知错误,请查看日志" )
177
+ logger .error (traceback .format_exc ())
172
178
except :
173
179
logger .error ("遇到未知错误,请查看日志" )
174
180
logger .error (traceback .format_exc ())
0 commit comments