def run(self): """ Coordinate the job. """ self.logger.info("%s started" % (self.prog,)) self._running = True action = None while self._running: self.supervise() self.save_status() if self._config.get("command") == "single": self.logger.debug("single mode, exiting") return wake_time = self.sleep_interval() + time.time() self.logger.debug("sleeping for %d seconds" % self.sleep_interval()) while wake_time >= time.time(): if self._config.get("pidfile"): pid_touch(self._config["pidfile"]) action = pid_check(self._config["pidfile"]) if action in ["quit", "stop_supervisor"]: self.logger.info("asked to %s" % action) self._running = False break elif action == "stop_children": self.logger.info("stopping all the children") self._child.stop() pid_write(self._config["pidfile"], os.getpid()) elif action != "": self.logger.warning("unknown action: %s" % action) if not self._running: break time.sleep(0.2) if action != "stop_supervisor": self.logger.info("stopping all the children") self._child.stop() self.logger.info("stopping %s" % (self.prog,)) self.save_status()
def initialize(self): """ Initialize. """ self._initialize_log() self._initialize_callback() self._initialize_daemon() if self._config.get("pidfile"): pid_write(self._config["pidfile"], os.getpid(), excl=True)
def stop(self, action="quit"): """ Quit the process. """ if not self._config.get("pidfile"): raise SimplevisorError("%s requires a pidfile" % action) pid = pid_read(self._config["pidfile"]) timeout = 10 if pid and timeout is not None: print("%s (pid %d) is being told to %s..." % (self.prog, pid, action)) pid_write(self._config["pidfile"], pid, action) while timeout >= 0: try: os.kill(pid, 0) except OSError: break timeout -= 1 time.sleep(0.5) try: os.kill(pid, 0) except OSError: print("%s (pid %d) does not seem to be running anymore" % (self.prog, pid)) sys.exit(0) pid_quit(self._config["pidfile"], self.prog) sys.exit(0)
def start(self): """ Do start action. """ signal.signal(signal.SIGINT, self.on_signal) signal.signal(signal.SIGTERM, self.on_signal) signal.signal(signal.SIGHUP, self.on_signal) self.pre_run() if self._config.get("daemon"): daemonize() run_function = log.log_exceptions( logger_name=self.prog, re_raise=False)(self.run) else: run_function = log.log_exceptions( logger_name=self.prog, re_raise=True)(self.run) if self._config.get("pidfile"): pid_write(self._config["pidfile"], os.getpid(), excl=True) try: run_function() except: self.save_status() if self._config.get("pidfile"): pid_remove(self._config.get("pidfile")) raise sys.exc_info()[1] if self._config.get("pidfile"): pid_remove(self._config.get("pidfile"))
def single(self): """ Do single action. """ if self._config.get("pidfile"): pid_write(self._config["pidfile"], os.getpid(), excl=True) self.pre_run() self.run() if self._config.get("pidfile"): pid_remove(self._config.get("pidfile"))
def send_action(self, action="stop_children"): """ Tell the supervisor to execute an action. """ if not self._config.get("pidfile"): raise SimplevisorError("%s requires a pidfile" % action) pid = pid_read(self._config["pidfile"]) if pid: print("%s (pid %d) is being told to %s..." % (self.prog, pid, action)) pid_write(self._config["pidfile"], pid, action) elif pid is not None: print("%s does not seem to be running anymore" % (self.prog,))
def run(self): """ Coordinate the job. """ self.logger.info("started") self._running = True action = None while self._running: self.supervise() self.save_status() wake_time = self.sleep_interval() + time.time() self.logger.debug( "sleeping for %d seconds", self.sleep_interval()) while wake_time >= time.time(): if self._config.get("pidfile"): pid_touch(self._config["pidfile"]) action = pid_check(self._config["pidfile"]) if action != "": self.logger.info("asked to %s", action) pid_write(self._config["pidfile"], os.getpid()) if action in ["quit", "stop_supervisor"]: self._running = False elif action == "stop_children": self._child.stop() elif action == "wake_up": break elif action[:14] == "restart_child ": target = self.get_child(action[14:]) target.restart() elif action != "": self.logger.warning("unknown action: %s", action) if not self._running: break time.sleep(0.5) if action != "stop_supervisor": self.logger.info("stopping all the children") self._child.stop() self.logger.info("stopping") self.save_status() self.logger.info("stopped")