def signal(self, sig): """Send a signal to the subprocess, without intending to kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ options = self.config.options processname = as_string(self.config.name) if not self.pid: msg = ("attempted to send %s sig %s but it wasn't running" % (processname, signame(sig))) options.logger.debug(msg) return msg options.logger.debug('sending %s (pid %s) sig %s' % (processname, self.pid, signame(sig))) self._assertInState(ProcessStates.RUNNING, ProcessStates.STARTING, ProcessStates.STOPPING) try: options.kill(self.pid, sig) except: tb = traceback.format_exc() msg = 'unknown problem sending sig %s (%s):%s' % (processname, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 return msg return None
def handle_signal(self): sig = self.options.get_signal() if sig: if sig in (signal.SIGTERM, signal.SIGINT, signal.SIGQUIT): self.options.logger.warn( 'received %s indicating exit request' % signame(sig)) self.options.mood = SupervisorStates.SHUTDOWN elif sig == signal.SIGHUP: if self.options.mood == SupervisorStates.SHUTDOWN: self.options.logger.warn( 'ignored %s indicating restart request (shutdown in progress)' % signame(sig)) else: self.options.logger.warn( 'received %s indicating restart request' % signame(sig)) self.options.mood = SupervisorStates.RESTARTING elif sig == signal.SIGCHLD: self.options.logger.debug( 'received %s indicating a child quit' % signame(sig)) elif sig == signal.SIGUSR2: self.options.logger.info( 'received %s indicating log reopen request' % signame(sig)) self.options.reopenlogs() for group in self.process_groups.values(): group.reopenlogs() else: self.options.logger.blather( 'received %s indicating nothing' % signame(sig))
def signal(self, sig): """Send a signal to the subprocess, without intending to kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ options = self.config.options if not self.pid: msg = ("attempted to send %s sig %s but it wasn't running" % (self.config.name, signame(sig))) options.logger.debug(msg) return msg options.logger.debug('sending %s (pid %s) sig %s' % (self.config.name, self.pid, signame(sig)) ) self._assertInState(ProcessStates.RUNNING,ProcessStates.STARTING, ProcessStates.STOPPING) try: options.kill(self.pid, sig) except: tb = traceback.format_exc() msg = 'unknown problem sending sig %s (%s):%s' % ( self.config.name, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 return msg return None
def kill(self, sig): """Send a signal to the subprocess. This may or may not kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ now = time.time() options = self.config.options if not self.pid: msg = ("attempted to kill %s with sig %s but it wasn't running" % (self.config.name, signame(sig))) options.logger.debug(msg) return msg #If we're in the stopping state, then we've already sent the stop #signal and this is the kill signal if self.state == ProcessStates.STOPPING: killasgroup = self.config.killasgroup else: killasgroup = self.config.stopasgroup as_group = "" if killasgroup: as_group = "process group " options.logger.debug( 'killing %s (pid %s) %swith signal %s' % (self.config.name, self.pid, as_group, signame(sig))) # RUNNING/STARTING/STOPPING -> STOPPING self.killing = 1 self.delay = now + self.config.stopwaitsecs # we will already be in the STOPPING state if we're doing a # SIGKILL as a result of overrunning stopwaitsecs self._assertInState(ProcessStates.RUNNING, ProcessStates.STARTING, ProcessStates.STOPPING) self.change_state(ProcessStates.STOPPING) pid = self.pid if killasgroup: # send to the whole process group instead pid = -self.pid try: options.kill(pid, sig) except: io = io.StringIO() traceback.print_exc(file=io) tb = io.getvalue() msg = 'unknown problem killing %s (%s):%s' % (self.config.name, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 self.killing = 0 self.delay = 0 return msg return None
def kill(self, sig): """Send a signal to the subprocess. This may or may not kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ now = time.time() options = self.config.options if not self.pid: msg = ("attempted to kill %s with sig %s but it wasn't running" % (self.config.name, signame(sig))) options.logger.debug(msg) return msg killasgroup = self.config.killasgroup and sig == signal.SIGKILL as_group = "" if killasgroup: as_group = "process group " options.logger.debug('killing %s (pid %s) %swith signal %s' % (self.config.name, self.pid, as_group, signame(sig)) ) # RUNNING/STARTING/STOPPING -> STOPPING self.killing = 1 self.delay = now + self.config.stopwaitsecs # we will already be in the STOPPING state if we're doing a # SIGKILL as a result of overrunning stopwaitsecs self._assertInState(ProcessStates.RUNNING,ProcessStates.STARTING, ProcessStates.STOPPING) self.change_state(ProcessStates.STOPPING) pid = self.pid if killasgroup: # send to the whole process group instead pid = -self.pid try: options.kill(pid, sig) except: io = StringIO.StringIO() traceback.print_exc(file=io) tb = io.getvalue() msg = 'unknown problem killing %s (%s):%s' % (self.config.name, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 self.killing = 0 self.delay = 0 return msg return None
def handle_signal(self): sig = self.options.get_signal() if sig: if sig in (signal.SIGTERM, signal.SIGINT): self.options.logger.warn('received %s indicating exit request' % signame(sig)) self.options.mood = SupervisorStates.SHUTDOWN elif sig == signal.SIGABRT: self.options.logger.warn('received %s indicating restart request' % signame(sig)) self.options.mood = SupervisorStates.RESTARTING # elif sig == signal.SIGCHLD: # self.options.logger.debug('received %s indicating a child quit' % signame(sig)) # elif sig == signal.SIGUSR2: # self.options.logger.info('received %s indicating log reopen request' % signame(sig)) # self.options.reopenlogs() # for group in self.process_groups.values(): # group.reopenlogs() else: self.options.logger.blather('received %s indicating nothing' % signame(sig))
def signal(self, sig): """Send a signal to the subprocess, without intending to kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ options = self.config.options processname = as_string(self.config.name) if not self.pid: msg = ("attempted to send %s sig %s but it wasn't running" % (processname, signame(sig))) options.logger.debug(msg) return msg options.logger.debug('sending %s (pid %s) sig %s' % (processname, self.pid, signame(sig))) self._assertInState(ProcessStates.RUNNING, ProcessStates.STARTING, ProcessStates.STOPPING) try: try: options.kill(self.pid, sig) except OSError as exc: if exc.errno == errno.ESRCH: msg = ( "unable to signal %s (pid %s), it probably just now exited " "on its own: %s" % (processname, self.pid, str(exc))) options.logger.debug(msg) # we could change the state here but we intentionally do # not. we will do it during normal SIGCHLD processing. return None raise except: tb = traceback.format_exc() msg = 'unknown problem sending sig %s (%s):%s' % (processname, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) return msg return None
def handle_signal(self): sig = self.options.get_signal() if sig: if sig in (signal.SIGTERM, signal.SIGINT): self.options.logger.warn( 'received %s indicating exit request' % signame(sig)) self.options.mood = SupervisorStates.SHUTDOWN elif sig == signal.SIGABRT: self.options.logger.warn( 'received %s indicating restart request' % signame(sig)) self.options.mood = SupervisorStates.RESTARTING # elif sig == signal.SIGCHLD: # self.options.logger.debug('received %s indicating a child quit' % signame(sig)) # elif sig == signal.SIGUSR2: # self.options.logger.info('received %s indicating log reopen request' % signame(sig)) # self.options.reopenlogs() # for group in self.process_groups.values(): # group.reopenlogs() else: self.options.logger.blather('received %s indicating nothing' % signame(sig))
def signal(self, sig): """Send a signal to the subprocess, without intending to kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ options = self.config.options if not self.pid: msg = ("attempted to send %s sig %s but it wasn't running" % (self.config.name, signame(sig))) options.logger.debug(msg) return msg options.logger.debug('sending %s (pid %s) sig %s' % (self.config.name, self.pid, signame(sig))) self._assertInState(ProcessStates.RUNNING, ProcessStates.STARTING, ProcessStates.STOPPING) try: options.kill(self.pid, sig) except ValueError as e: # signal not supported msg = 'problem sending sig %s (%s): %s' % (self.config.name, self.pid, str(e)) io = StringIO() traceback.print_exc(file=io) tb = io.getvalue() options.logger.error(tb) return msg except: io = StringIO() traceback.print_exc(file=io) tb = io.getvalue() msg = 'unknown problem sending sig %s (%s):%s' % (self.config.name, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 return msg
def handle_signal(self): sig = self.options.get_signal() if sig: # 请求中止进程,kill命令缺省发送 # 由Interrupt Key产生,通常是CTRL+C或者DELETE。发送给所有ForeGround Group的进程 # 输入Quit Key的时候(CTRL+\)发送给所有Foreground Group的进程 # 关闭 if sig in (signal.SIGTERM, signal.SIGINT, signal.SIGQUIT): self.options.logger.warn( 'received %s indicating exit request' % signame(sig)) self.options.mood = SupervisorStates.SHUTDOWN # 发送给具有Terminal的Controlling Process,当terminal被disconnect时候发送 # 重启 elif sig == signal.SIGHUP: if self.options.mood == SupervisorStates.SHUTDOWN: self.options.logger.warn( 'ignored %s indicating restart request (shutdown in progress)' % signame(sig)) else: self.options.logger.warn( 'received %s indicating restart request' % signame(sig)) self.options.mood = SupervisorStates.RESTARTING # 进程Terminate或Stop的时候,SIGCHLD会发送给它的父进程。缺省情况下该Signal会被忽略 elif sig == signal.SIGCHLD: self.options.logger.debug( 'received %s indicating a child quit' % signame(sig)) # 用户自定义signal 2 elif sig == signal.SIGUSR2: self.options.logger.info( 'received %s indicating log reopen request' % signame(sig)) self.options.reopenlogs() for group in self.process_groups.values(): group.reopenlogs() else: self.options.logger.blather('received %s indicating nothing' % signame(sig))
def kill(self, sig): """Send a signal to the subprocess. This may or may not kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ now = time.time() options = self.config.options processname = as_string(self.config.name) # If the process is in BACKOFF and we want to stop or kill it, then # BACKOFF -> STOPPED. This is needed because if startretries is a # large number and the process isn't starting successfully, the stop # request would be blocked for a long time waiting for the retries. if self.state == ProcessStates.BACKOFF: msg = ("Attempted to kill %s, which is in BACKOFF state." % processname) options.logger.debug(msg) self.change_state(ProcessStates.STOPPED) return None if not self.pid: msg = ("attempted to kill %s with sig %s but it wasn't running" % (processname, signame(sig))) options.logger.debug(msg) return msg # If we're in the stopping state, then we've already sent the stop # signal and this is the kill signal if self.state == ProcessStates.STOPPING: killasgroup = self.config.killasgroup else: killasgroup = self.config.stopasgroup as_group = "" if killasgroup: as_group = "process group " options.logger.debug('killing %s (pid %s) %swith signal %s' % (processname, self.pid, as_group, signame(sig))) # RUNNING/STARTING/STOPPING -> STOPPING self.killing = True self.delay = now + self.config.stopwaitsecs # we will already be in the STOPPING state if we're doing a # SIGKILL as a result of overrunning stopwaitsecs self._assertInState(ProcessStates.RUNNING, ProcessStates.STARTING, ProcessStates.STOPPING) self.change_state(ProcessStates.STOPPING) pid = self.pid if killasgroup: # send to the whole process group instead pid = -self.pid try: options.kill(pid, sig) except: tb = traceback.format_exc() msg = 'unknown problem killing %s (%s):%s' % (processname, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 self.killing = False self.delay = 0 return msg return None
def kill(self, sig): """Send a signal to the subprocess. This may or may not kill it. Return None if the signal was sent, or an error message string if an error occurred or if the subprocess is not running. """ now = time.time() options = self.config.options # Properly stop processes in BACKOFF state. if self.state == ProcessStates.BACKOFF: msg = ("Attempted to kill %s, which is in BACKOFF state." % (self.config.name)) options.logger.debug(msg) self.change_state(ProcessStates.STOPPED) return None if not self.pid: msg = ("attempted to kill %s with sig %s but it wasn't running" % (self.config.name, signame(sig))) options.logger.debug(msg) return msg #If we're in the stopping state, then we've already sent the stop #signal and this is the kill signal if self.state == ProcessStates.STOPPING: killasgroup = self.config.killasgroup else: killasgroup = self.config.stopasgroup as_group = "" if killasgroup: as_group = "process group " options.logger.debug('killing %s (pid %s) %swith signal %s' % (self.config.name, self.pid, as_group, signame(sig)) ) # RUNNING/STARTING/STOPPING -> STOPPING self.killing = True self.delay = now + self.config.stopwaitsecs # we will already be in the STOPPING state if we're doing a # SIGKILL as a result of overrunning stopwaitsecs self._assertInState(ProcessStates.RUNNING,ProcessStates.STARTING, ProcessStates.STOPPING) self.change_state(ProcessStates.STOPPING) pid = self.pid if killasgroup: # send to the whole process group instead pid = -self.pid try: options.kill(pid, sig) except: tb = traceback.format_exc() msg = 'unknown problem killing %s (%s):%s' % (self.config.name, self.pid, tb) options.logger.critical(msg) self.change_state(ProcessStates.UNKNOWN) self.pid = 0 self.killing = False self.delay = 0 return msg return None
def restart(self, sig): """ Restart with signal """ # DIRTY killing process, need to rewrite completely (according to kill method) options = self.config.options if not self.pid: msg = "attempted to kill %s with sig %s but it wasn't running" % (self.config.name, signame(sig)) options.logger.debug(msg) return msg pid = self.pid options.kill(pid, sig) return None