def CheckAlreadyRunning( self ): while HydrusData.IsAlreadyRunning( self.db_dir, 'client' ): self.pub( 'splash_set_status_text', 'client already running' ) def wx_code(): message = 'It looks like another instance of this client is already running, so this instance cannot start.' message += os.linesep * 2 message += 'If the old instance is closing and does not quit for a _very_ long time, it is usually safe to force-close it from task manager.' with ClientGUIDialogs.DialogYesNo( self._splash, message, 'The client is already running.', yes_label = 'wait a bit, then try again', no_label = 'forget it' ) as dlg: if dlg.ShowModal() != wx.ID_YES: raise HydrusExceptions.PermissionException() self.CallBlockingToWx( wx_code ) for i in range( 10, 0, -1 ): if not HydrusData.IsAlreadyRunning( self.db_dir, 'client' ): break self.pub( 'splash_set_status_text', 'waiting ' + str( i ) + ' seconds' ) time.sleep( 1 )
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, or e[x]it?' ) answer = raw_input() if len( answer ) > 0: answer = answer[0] if answer == 's': return 'stop' elif answer == 'r': return 'restart' raise HydrusExceptions.PermissionException( 'Exiting!' ) else: return action elif action == 'stop': if already_running: return action else: raise HydrusExceptions.PermissionException( 'The server is not running, so it cannot be stopped!' ) elif action == 'restart': if already_running: return action else: return 'start'
def ShutdownSiblingInstance(db_dir): port_found = False ports = HydrusData.GetSiblingProcessPorts(db_dir, 'server') if ports is None: raise HydrusExceptions.PermissionException( 'Could not figure out the existing server\'s ports, so could not shut it down!' ) for port in ports: try: connection = HydrusNetworking.GetLocalConnection(port, https=True) connection.request('GET', '/') response = connection.getresponse() response.read() server_name = response.getheader('Server') except: text = 'Could not contact existing server\'s port ' + str( port) + '!' text += os.linesep text += traceback.format_exc() raise HydrusExceptions.PermissionException(text) if 'server administration' in server_name: port_found = True HydrusData.Print(u'Sending shut down instruction\u2026') connection.request('POST', '/shutdown') response = connection.getresponse() result = response.read() if response.status != 200: text = 'When told to shut down, the existing server gave an error!' text += os.linesep text += result raise HydrusExceptions.PermissionException(text) time_waited = 0 while HydrusData.IsAlreadyRunning(db_dir, 'server'): time.sleep(1) time_waited += 1 if time_waited > 20: raise HydrusExceptions.PermissionException( 'Attempted to shut the existing server down, but it took too long!' ) break if not port_found: raise HydrusExceptions.PermissionException( 'The existing server did not have an administration service!') HydrusData.Print('The existing server is shut down!')
def GetStartingAction(): action = 'help' args = sys.argv[1:] if len(args) > 0: command = args[0] while command.startswith('-'): command = command[1:] if command == 'help': action = 'help' else: already_running = HydrusData.IsAlreadyRunning(HC.DB_DIR, 'server') if command == 'start': if already_running: raise HydrusExceptions.PermissionException( 'The server is already running!') else: action = 'start' elif command == 'stop': if already_running: action = 'stop' else: raise HydrusExceptions.PermissionException( 'The server is not running, so it cannot be stopped!') elif command == 'restart': if already_running: action = 'restart' else: action = 'start' else: already_running = HydrusData.IsAlreadyRunning(HC.DB_DIR, 'server') if not already_running: action = 'start' else: HydrusData.Print( 'The server is already running. Would you like to [s]top it, or [r]estart it?' ) answer = raw_input() if len(answer) > 0: answer = answer[0] if answer == 's': action = 'stop' elif answer == 'r': action = 'restart' return action