def _check_process_resources(self): """Check if the process is still is abusing resources & should continue This will check the processes memory usage and gracefully exit if it exceeds the maximum value in settings. :return: True if still operational, otherwise it will exit the process. :rtype: bool """ ru_maxrss_mb = utils.ru_maxrss_mb() if ru_maxrss_mb < settings.DIE_ON_RESIDENT_SET_SIZE_MB: if self._dirty: # We only log when the worker has been infected by tasks. logger.debug('Worker process data.') return True # Allow the client of this library to do any setup before # shutting down the worker. settings.ON_WORKER_SHUTDOWN() self._on_exceeding_memory_limit(ru_maxrss_mb) # Use non-zero exit code. sys.exit(1)
def _cleanup_worker(self, signum, frame): """Handle cleanup when the process is sent a signal. This will handle releasing tasks in flight and deleting tasks that have been completed. """ logger.info('Process sent signal %d. Cleaning up tasks...' % signum) num_completed, num_incomplete = self._release_batch() # When the process is suspended we release tasks and then return to the # main loop. if signum == signal.SIGTSTP: self._on_sigtstp(num_completed, num_incomplete) return else: # Allow the client of this library to do any setup before # shutting down the worker. settings.ON_WORKER_SHUTDOWN() self._on_shutdown(num_completed, num_incomplete) sys.exit(0)