class Monitor: """ Internal monitor class for handling the process state. """ def __init__(self, home, id=None, init=None): self.home = home; # if id is specified, this is a client monitor. self.session = Session( home, id ); self.init = init; self.sp = None; self.started = time.time(); self.setproctitle(); #self.log_fd = sys.stdout; self.log_fd = self.open_log(); #sys.stdout = LogWrapper("STDOUT", self); #sys.stderr = LogWrapper("STDERR", self); def open_log(self): bestname = self.session.id; return self.home.open_log( "%s.log"%( bestname ) ) def log(self, *items): d_now = str( datetime.datetime.now() ); self.log_fd.write( "%s %s: %s\n"%( self.session.id, d_now, " ".join([str(i) for i in items]) ) ); self.log_fd.flush(); def run(self): """ Wait for all child processes to die. """ self.running = True; self.session.create(); self.log("Monitor Running"); while self.running: try: c = MonitorCommand.from_json( self.session.recv() ); if not c: self.log( "Got bad command:", repr(s) ); else: self._handle_reception(c); except Exception, e: import traceback self.log( "Exc:", traceback.format_exc() ); if not self._check_process(): self.log("Child exited with returncode:", self.sp.returncode); self.log("Initiating shutdown"); self.stop(); self.session.destroy(); self.log("Monitor Shutting Down");
def __init__(self, home, id=None, init=None): self.home = home; # if id is specified, this is a client monitor. self.session = Session( home, id ); self.init = init; self.sp = None; self.started = time.time(); self.setproctitle(); #self.log_fd = sys.stdout; self.log_fd = self.open_log();