def handle(self, *args, **options): """ This is the entry point for the management command. It will handle daemonizing the script as needed. """ """ Before starting this daemon, this will check for another version of this process by checking for a file in the temporary folder, and then if another version exists it will close that process. """ if (self.process_exists()): # TODO : refactor into utils/utils.stop_process for DRY try: files = os.listdir(utils._get_tmp_directory()) except OSError: logger.warning("OSError encountered when attempting to get " + "temporary files.") for filename in files: # check for file containing pid matches = re.match(r"^([0-9]+).pid$", filename) # if it exists, then stop the process if matches: pid = matches.group(1) verbose = options["verbose"] logger.info("Stopping an existing labstats_daemon") utils.stop_process(pid, verbose) atexit.register(self.remove_pid_file) daemon = options["daemon"] # get the flag if daemon: logger.info("Starting the updater as a daemon") pid = os.fork() if pid == 0: os.setsid() pid = os.fork() if pid != 0: os._exit(0) else: os._exit(0) self.create_pid_file() try: self.controller(options["update_delay"], options["run_once"]) except Exception as ex: logger.error("Uncaught error running the controller", exc_info=1) else: logger.info("Starting the updater as an interactive process") self.create_pid_file() self.controller(options["update_delay"], options["run_once"])
def process_exists(self): try: files = os.listdir(utils._get_tmp_directory()) except OSError: sys.exit(0) for filename in files: matches = re.match(r"^([0-9]+).pid$", filename) if matches: return True return False
def kill_process(self, pid): """ Kills the process with the passed PID and removes its PID file. """ try: os.kill(int(pid), signal.SIGTERM) time.sleep(1) try: os.getpgid(int(pid)) os.kill(int(pid), signal.SIGKILL) except: pass if os.path.isfile(_get_tmp_directory() + "%s.stop" % pid): os.remove(_get_tmp_directory() + "%s.stop" % pid) if os.path.isfile(_get_tmp_directory() + "%s.pid" % pid): os.remove(_get_tmp_directory() + "%s.pid" % pid) except: pass
def handle(self, *args, **options): force = options["force"] try: files = os.listdir(_get_tmp_directory()) except OSError: logger.warning("OSError encountered when attempting to get " + "temporary files. Daemon could not be stopped") sys.exit(0) # get the file with the PID of the other labstats daemons for filename in files: matches = re.match(r"^([0-9]+).pid$", filename) if matches: pid = matches.group(1) if force: self.kill_process(pid) else: verbose = options["verbose"] if stop_process(pid, verbose): sys.exit(0) else: sys.exit(1)
def handle(self, *args, **options): force = options["force"] try: files = os.listdir(_get_tmp_directory()) except OSError: logger.warning( "OSError encountered when attempting to get " + "temporary files. Daemon could not be stopped" ) sys.exit(0) # get the file with the PID of the other labstats daemons for filename in files: matches = re.match(r"^([0-9]+).pid$", filename) if matches: pid = matches.group(1) if force: self.kill_process(pid) else: verbose = options["verbose"] if stop_process(pid, verbose): sys.exit(0) else: sys.exit(1)
def _get_stopfile_path(self): return utils._get_tmp_directory() + "%s.stop" % (str(os.getpid()))
def _get_pid_file_path(self): return utils._get_tmp_directory() + "%s.pid" % (str(os.getpid()))