def ProcessStartingAction(db_dir, action): already_running = HydrusData.IsAlreadyRunning(db_dir, 'server') if action == 'start': if already_running: HydrusData.Print( 'The server is already running. Would you like to [s]top it, [r]estart it here, or e[x]it?' ) answer = input() if len(answer) > 0: answer = answer[0] if answer == 's': return 'stop' elif answer == 'r': return 'restart' return 'exit' else: return action elif action == 'stop': if already_running: return action else: raise HydrusExceptions.ShutdownException( 'The server is not running, so it cannot be stopped!') elif action == 'restart': if already_running: return action else: HydrusData.Print( 'Did not find an already running instance of the server--changing boot command from \'restart\' to \'start\'.' ) return 'start'
def ShutdownSiblingInstance(db_dir): port_found = False ports = HydrusData.GetSiblingProcessPorts(db_dir, 'server') if ports is None: raise HydrusExceptions.ShutdownException( 'Could not figure out the existing server\'s ports, so could not shut it down!' ) session = requests.Session() session.verify = False for port in ports: try: r = session.get('https://127.0.0.1:' + str(port) + '/') server_name = r.headers['Server'] except: text = 'Could not contact existing server\'s port ' + str( port) + '!' text += os.linesep text += traceback.format_exc() raise HydrusExceptions.ShutdownException(text) if 'server administration' in server_name: port_found = True HydrusData.Print('Sending shut down instruction\u2026') r = session.post('https://127.0.0.1:' + str(port) + '/shutdown') if not r.ok: text = 'When told to shut down, the existing server gave an error!' text += os.linesep text += r.text raise HydrusExceptions.ShutdownException(text) time_waited = 0 while HydrusData.IsAlreadyRunning(db_dir, 'server'): time.sleep(1) time_waited += 1 if time_waited > 20: raise HydrusExceptions.ShutdownException( 'Attempted to shut the existing server down, but it took too long!' ) break if not port_found: raise HydrusExceptions.ShutdownException( 'The existing server did not have an administration service!') HydrusData.Print('The existing server is shut down!')