def stop( self, cmd = None ): """ Stop the child. If 'cmd' is given, this stop command will send to the app to stop itself. If this is not working, kill -15 and kill -9 will be used to kill the app. Returns an InProgress which finishes when the process stops. """ if self.stopping: return inprogress(self.signals['completed']) if not is_mainthread(): return MainThreadCallback(self.stop, cmd)() self.stopping = True cmd = cmd or self._stop_cmd if self.is_alive() and not self.__kill_timer: if cmd: log.info('sending exit command to app') if callable(cmd): cmd() else: self.write(cmd) cb = Callback( self.__kill, 15 ) self.__kill_timer = notifier.timer_add( 3000, cb ) else: cb = Callback( self.__kill, 15 ) self.__kill_timer = notifier.timer_add( 0, cb ) return inprogress(self.signals['completed'])
def start(self, interval): """ Start the timer, invoking the callback every *interval* seconds. If the timer is already running, it is stopped and restarted with the given interval. The timer's precision is at the mercy of other tasks running in the main loop. For example, if another task (a different timer, or I/O callback) blocks the mainloop for longer than the given timer interval, the callback will be invoked late. :param interval: interval between invocations of the callback, in seconds """ if self.active: if not self.restart_when_active: return self.unregister() self._id = notifier.timer_add(int(interval * 1000), self) self.__interval = interval
def __kill( self, signal ): """ Internal kill helper function """ if not self.is_alive(): self.__dead = True self.stopping = False return False # child needs some assistance with dying ... try: os.kill( self.child.pid, signal ) except OSError: pass if signal == 15: cb = Callback( self.__kill, 9 ) else: cb = Callback( self.__killall, 15 ) self.__kill_timer = notifier.timer_add( 3000, cb ) return False
def append( self, proc, cb ): self.__processes[ proc ] = cb if not self.__timer: log.info('start process watching') # FIXME: there is no reason to reap 20 times a second. self.__timer = notifier.timer_add(50, self.check)
# Found one, kill it pid = int(cmdline_filename.split('/')[2]) try: os.kill(pid, signal) except (KeyboardInterrupt, SystemExit), e: os.kill(pid, signal) sys.exit(0) except Exception: pass except OSError: pass log.info('kill -%d %s' % ( signal, self.binary )) if signal == 15: cb = Callback( self.__killall, 9 ) self.__kill_timer = notifier.timer_add( 2000, cb ) else: log.critical('PANIC %s' % self.binary) return False def __child_died( self, pid, status ): """ Callback from watcher when the child died. """ if pid != self.get_pid(): # We received notification from the watcher concerning another # process, so ignore. return