def do_start(self):
     if not self.log:
         self.log = RotatingFile(self.logfile,
             maxBytes=self.logfile_maxbytes, 
             backupCount=self.logfile_backups)
         
     if self.is_ok():
         msg = self.name + ' has already been started'
     else:
         self.log.info('start '+self.name)
         n = 0
         while n < self.startretries:
             self.process = Popen(self.command, stdout=self.log, stderr=self.log, shell=True, cwd=self.cwd)
             gevent.sleep(self.starting_time)
             if self.process.poll() is not None:
                 #error
                 self.log.info('start '+self.name+' failed!, times=%d'%n)
                 n += 1
             else:
                 self.start_time = datetime.datetime.now()
                 break
         #if stopped then status is Fatal
         if n == self.startretries:
             self.stop = FATAL
             msg = self.name + ' started failed'
         else:
             self.stop = RUNNING
             msg = self.name + ' started'
     return msg
class Command(object):
    def __init__(self, name, command, **kwargs):
        self.name = name
        self.command = command
        self.log = None
        self.stop = False
        self.process = None
        for k, v in kwargs.items():
            setattr(self, k, v)
        
    def is_ok(self):
        return self.process and self.process.poll() is None
    
    def do_start(self):
        if not self.log:
            self.log = RotatingFile(self.logfile,
                maxBytes=self.logfile_maxbytes, 
                backupCount=self.logfile_backups)
            
        if self.is_ok():
            msg = self.name + ' has already been started'
        else:
            self.log.info('start '+self.name)
            self.process = Popen(self.command, stdout=self.log, stderr=self.log, shell=True, cwd=self.cwd)
            self.stop = False
            msg = self.name + ' started'
        return msg
    
    def do_stop(self):
        if self.is_ok():
            self.log.info('stop '+self.name)
            self.stop = True
            call(['taskkill', '/F', '/T', '/PID', str(self.process.pid)])
            self.process = None
            msg = self.name + ' stopped'
        else:
            msg = self.name + ' has already stopped'
        return msg
    
    def do_status(self):
        if self.is_ok():
            status = 'RUNNING'
        else:
            status = 'STOPPED'
        msg = '%-20s %s' % (self.name, status)
        return msg
    def _getLogger(self):
        from logfile import RotatingFile

        logger = RotatingFile(
            os.path.join(self.startpath,
                         self.ini.server.get('log_to', 'pywatcher.log')),
            maxBytes=self.ini.server.get('logfile_maxbytes', 50 * 1024 * 1024),
            backupCount=self.ini.server.get('logfile_backups', 10))

        return logger
 def do_start(self):
     if not self.log:
         self.log = RotatingFile(self.logfile,
             maxBytes=self.logfile_maxbytes, 
             backupCount=self.logfile_backups)
         
     if self.is_ok():
         msg = self.name + ' has already been started'
     else:
         self.log.info('start '+self.name)
         self.process = Popen(self.command, stdout=self.log, stderr=self.log, shell=True, cwd=self.cwd)
         self.stop = False
         msg = self.name + ' started'
     return msg
class Command(object):
    def __init__(self, name, command, **kwargs):
        self.name = name
        self.command = command
        self.log = None
        self.stop = STOPPED   #0 normal stopped 1 running -1 fatal stopped
        self.process = None
        for k, v in kwargs.items():
            setattr(self, k, v)
        self.start_time = None
        self.stop_time = None
        
    def is_ok(self):
        return self.process and self.process.poll() is None
    
    def do_start(self):
        if not self.log:
            self.log = RotatingFile(self.logfile,
                maxBytes=self.logfile_maxbytes, 
                backupCount=self.logfile_backups)
            
        if self.is_ok():
            msg = self.name + ' has already been started'
        else:
            self.log.info('start '+self.name)
            n = 0
            while n < self.startretries:
                self.process = Popen(self.command, stdout=self.log, stderr=self.log, shell=True, cwd=self.cwd)
                gevent.sleep(self.starting_time)
                if self.process.poll() is not None:
                    #error
                    self.log.info('start '+self.name+' failed!, times=%d'%n)
                    n += 1
                else:
                    self.start_time = datetime.datetime.now()
                    break
            #if stopped then status is Fatal
            if n == self.startretries:
                self.stop = FATAL
                msg = self.name + ' started failed'
            else:
                self.stop = RUNNING
                msg = self.name + ' started'
        return msg
    
    def do_stop(self):
        if self.is_ok():
            self.log.info('stop '+self.name)
            self.stop = STOPPED
            self.stop_time = datetime.datetime.now()
            call(['taskkill', '/F', '/T', '/PID', str(self.process.pid)])
            self.process = None
            msg = self.name + ' stopped'
        else:
            msg = self.name + ' has already stopped'
        return msg
    
    def do_status(self):
        if self.is_ok():
            status = 'RUNNING'
            info = 'pid %d, uptime %s' % (self.process.pid, self.start_time.strftime('%H:%M:%S'))
        else:
            if self.stop == STOPPED:
                status = 'STOPPED'
                info = self.stop_time.ctime()
            else:
                status = 'FATAL'
                info = 'Exited too quickly'
        msg = '%-20s %-10s %s' % (self.name, status, info)
        return msg