Browse Source

Revert "Revert "Merge pull request #6089 from maxkoryukov/fix/address-in-use""

This reverts commit 4633adcead.
pull/6133/head
Ruud 9 years ago
parent
commit
ac545f9c72
  1. 52
      couchpotato/runner.py

52
couchpotato/runner.py

@ -24,6 +24,8 @@ from requests.packages.urllib3 import disable_warnings
from tornado.httpserver import HTTPServer from tornado.httpserver import HTTPServer
from tornado.web import Application, StaticFileHandler, RedirectHandler from tornado.web import Application, StaticFileHandler, RedirectHandler
from couchpotato.core.softchroot import SoftChrootInitError from couchpotato.core.softchroot import SoftChrootInitError
from socket import error as SocketError
import errno
def getOptions(args): def getOptions(args):
@ -340,10 +342,11 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En
server = HTTPServer(application, no_keep_alive = True, ssl_options = ssl_options) server = HTTPServer(application, no_keep_alive = True, ssl_options = ssl_options)
try_restart = True run_tries = 5
restart_tries = 5 assert run_tries > 0
while try_restart: while run_tries > 0:
run_tries -= 1
try: try:
server.listen(config['port'], config['host']) server.listen(config['port'], config['host'])
@ -352,27 +355,36 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En
except: log.info2('Tried to bind to IPV6 but failed') except: log.info2('Tried to bind to IPV6 but failed')
loop.start() loop.start()
# on shutting down without exception
server.close_all_connections() server.close_all_connections()
server.stop() server.stop()
loop.close(all_fds = True) loop.close(all_fds = True)
except Exception as e:
log.error('Failed starting: %s', traceback.format_exc()) except SocketError as e:
try:
nr, msg = e # here we will handle just two errors :
if nr == 48: # errno.EADDRINUSE = 98 - address in use
log.info('Port (%s) needed for CouchPotato is already in use, try %s more time after few seconds', (config.get('port'), restart_tries)) # errno.ELNRNG = 48 - port in use (strange, but I took it from old code)
time.sleep(1) # TODO : check, that value 48 still actual
restart_tries -= 1 if e.errno in [ errno.EADDRINUSE, errno.ELNRNG ]:
if run_tries > 0:
if restart_tries > 0: log.warning('Port (%s) needed for CouchPotato is already in use, try %s more time after few seconds',
continue (config.get('port'), run_tries))
else: time.sleep(2)
return continue
except ValueError:
# not ugly error message
log.error('Failed starting: Port (%s) needed for CouchPotato is already in use',
(config['port']))
# we will not raise this exception again, because no additional actions are required:
return return
except:
pass
log.error('Failed starting: %s', traceback.format_exc())
raise raise
try_restart = False except Exception as e:
log.error('Failed starting: %s', traceback.format_exc())
raise
pass # while run_tries>0

Loading…
Cancel
Save