Browse Source

Exit more cleanly when ports are occupied

pull/911/head
Safihre 8 years ago
parent
commit
c9f765813c
  1. 91
      SABnzbd.py

91
SABnzbd.py

@ -669,7 +669,11 @@ def is_sabnzbd_running(url):
def find_free_port(host, currentport): def find_free_port(host, currentport):
""" Return a free port, 0 when nothing is free """ """ Return a free port if allowed, 0 when nothing is free """
if sabnzbd.cfg.fixed_ports():
# Port found before, so we bail out
raise IOError
n = 0 n = 0
while n < 10 and currentport <= 49151: while n < 10 and currentport <= 49151:
try: try:
@ -1042,51 +1046,48 @@ def main():
if url and check_for_sabnzbd(url, upload_nzbs, autobrowser): if url and check_for_sabnzbd(url, upload_nzbs, autobrowser):
exit_sab(0) exit_sab(0)
# We only performe the free-port check on first start-up # SSL
# because we want to be sure we can bind to something! if enable_https:
if not sabnzbd.cfg.fixed_ports(): port = https_port or cherryport
# SSL try:
if enable_https: cherrypy.process.servers.check_port(browserhost, port, timeout=0.05)
port = https_port or cherryport except IOError, error:
try: if str(error) == 'Port not bound.':
cherrypy.process.servers.check_port(browserhost, port, timeout=0.05) pass
except IOError, error: else:
if str(error) == 'Port not bound.': if not url:
pass url = 'https://%s:%s/sabnzbd/api?' % (browserhost, port)
else: if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser):
if not url: newport = find_free_port(browserhost, port)
url = 'https://%s:%s/sabnzbd/api?' % (browserhost, port) if newport > 0:
if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser): # Save the new port
newport = find_free_port(browserhost, port) if https_port:
if newport > 0: https_port = newport
# Save the new port sabnzbd.cfg.https_port.set(newport)
if https_port: else:
https_port = newport # In case HTTPS == HTTP port
sabnzbd.cfg.https_port.set(newport) http_port = newport
else: sabnzbd.cfg.port.set(newport)
# In case HTTPS == HTTP port except:
http_port = newport Bail_Out(browserhost, cherryport, '49')
sabnzbd.cfg.port.set(newport)
except:
Bail_Out(browserhost, cherryport, '49')
# NonSSL check if there's no HTTPS or we only use 1 port # NonSSL check if there's no HTTPS or we only use 1 port
if not (enable_https and not https_port): if not (enable_https and not https_port):
try: try:
cherrypy.process.servers.check_port(browserhost, cherryport, timeout=0.05) cherrypy.process.servers.check_port(browserhost, cherryport, timeout=0.05)
except IOError, error: except IOError, error:
if str(error) == 'Port not bound.': if str(error) == 'Port not bound.':
pass pass
else: else:
if not url: if not url:
url = 'http://%s:%s/sabnzbd/api?' % (browserhost, cherryport) url = 'http://%s:%s/sabnzbd/api?' % (browserhost, cherryport)
if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser): if new_instance or not check_for_sabnzbd(url, upload_nzbs, autobrowser):
port = find_free_port(browserhost, cherryport) port = find_free_port(browserhost, cherryport)
if port > 0: if port > 0:
sabnzbd.cfg.cherryport.set(port) sabnzbd.cfg.cherryport.set(port)
cherryport = port cherryport = port
except: except:
Bail_Out(browserhost, cherryport, '49') Bail_Out(browserhost, cherryport, '49')
# We found a port, now we never check again # We found a port, now we never check again
sabnzbd.cfg.fixed_ports.set(True) sabnzbd.cfg.fixed_ports.set(True)

Loading…
Cancel
Save