def CallBlockingToQt(self, win, func, *args, **kwargs): def qt_code(win: QW.QWidget, job_key: ClientThreading.JobKey): try: if win is not None and not QP.isValid(win): raise HydrusExceptions.QtDeadWindowException( 'Parent Window was destroyed before Qt command was called!' ) result = func(*args, **kwargs) job_key.SetVariable('result', result) except (HydrusExceptions.QtDeadWindowException, HydrusExceptions.DBCredentialsException, HydrusExceptions.ShutdownException) as e: job_key.SetErrorException(e) except Exception as e: job_key.SetErrorException(e) HydrusData.Print('CallBlockingToQt just caught this error:') HydrusData.DebugPrint(traceback.format_exc()) finally: job_key.Finish() job_key = ClientThreading.JobKey() QP.CallAfter(qt_code, win, job_key) while not job_key.IsDone(): if HG.model_shutdown: raise HydrusExceptions.ShutdownException( 'Application is shutting down!') time.sleep(0.05) if job_key.HasVariable('result'): # result can be None, for qt_code that has no return variable result = job_key.GetIfHasVariable('result') return result if job_key.HadError(): e = job_key.GetErrorException() raise e raise HydrusExceptions.ShutdownException()
def GetResult(self): time.sleep( 0.00001 ) # this one neat trick can save hassle on superquick jobs as event.wait can be laggy while True: if self._result_ready.wait(2) == True: break elif HG.model_shutdown: raise HydrusExceptions.ShutdownException( 'Application quit before db could serve result!') self._DoDelayedResultRelief() if isinstance(self._result, Exception): e = self._result raise e else: return self._result
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 WaitUntilPubSubsEmpty( self ): while self.CurrentlyPubSubbing(): if HG.model_shutdown: raise HydrusExceptions.ShutdownException( 'Application shutting down!' ) else: time.sleep( 0.00001 )
def do_test(): if HG.model_shutdown: try: process.kill() except: pass raise HydrusExceptions.ShutdownException( 'Application is shutting down!')
def Write( self, action, synchronous, *args, **kwargs ): job_type = 'write' job = self._GenerateDBJob( job_type, synchronous, action, *args, **kwargs ) if HG.model_shutdown: raise HydrusExceptions.ShutdownException( 'Application has shut down!' ) self._jobs.put( job ) if synchronous: return job.GetResult()
def WaitUntilFree(self): while True: if HG.view_shutdown: raise HydrusExceptions.ShutdownException( 'Application shutting down!') queue_is_empty = self._waterfall_queue_empty_event.wait(1) if queue_is_empty: return
def WaitUntilDBEmpty( self ) -> None: while True: if HG.model_shutdown: raise HydrusExceptions.ShutdownException( 'Application shutting down!' ) elif self.db.JobsQueueEmpty() and not self.db.CurrentlyDoingJob(): return else: time.sleep( 0.00001 )
def Read( self, action, *args, **kwargs ): if action in self.READ_WRITE_ACTIONS: job_type = 'read_write' else: job_type = 'read' synchronous = True job = self._GenerateDBJob( job_type, synchronous, action, *args, **kwargs ) if HG.model_shutdown: raise HydrusExceptions.ShutdownException( 'Application has shut down!' ) self._jobs.put( job ) return job.GetResult()
def CheckIfThreadShuttingDown(): if IsThreadShuttingDown(): raise HydrusExceptions.ShutdownException('Thread is shutting down!')
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!')
def CheckProgramIsNotShuttingDown(): if HG.model_shutdown: raise HydrusExceptions.ShutdownException( 'Application is shutting down!')