def run():
    import os
    if os.path.exists('webkit.lock'):
        os.remove('webkit.lock')
        
    import time
    from freestationapp import FreeStationApp
    
    app = FreeStationApp(3, True)
    app.setName('FreeStation' + '-' + str(time.time()))
    app.start()
    app.run()
Example #2
0
class Watchdog(object):
    
    MONITOR_TIME = 1
    PROCESS_MONITORED_NAME = 'FreeStation'
    
    def __init__(self):
        ''' 
            Init the watchdog 
        '''
        
        LOG.debug('Parent PID: {0}'.format(getppid()))
        LOG.debug('Watchdog PID: {0}'.format(getpid()))
        
        signal(SIGTERM, self.handler)
        
        self.play = True
        
        self.threads = []
    
    def handler(self, signal, object): #@ReservedAssignment
        '''
            Catch SIGTERM signal and destroy the app if alive
        '''
        if hasattr(self, 'p'):
            if self.p and self.p.is_alive():
                self.p.on_destroy(None, None)
        
        LOG.debug('Exiting by interrupt. Bye')
        # This shows: Exception SystemExit: 0 in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
        # But it is only closing with nice exit. SystemExit when it is not handled, the Python interpreter exits
        sys.exit(0) 
        
    def launch(self, alive_time = None, enable_gui = True):
        '''
            Launch the threaded application 
            @param alive_time msec for live app, normally used by test. Default None for ignore
            @param enable_gui launch the program loading the GUI if True
        '''
        if hasattr(self, 'p'):
            self.p.destroy()
            del self.p
            
        # Cleanup any state set by the FreeStationApp.
        #
        import signal #@Reimport
        #if signal.__dict__.has_key('SIGHUP'):
        #    signal.signal(signal.SIGHUP, signal.SIG_DFL)
        #if signal.__dict__.has_key('SIGBREAK'):
        #    signal.signal(signal.SIGBREAK, signal.SIG_DFL) #@UndefinedVariable
        signal.signal(signal.SIGINT, signal.SIG_DFL)
        signal.signal(signal.SIGTERM, signal.SIG_DFL)
        
        import os
        if os.path.exists('webkit.lock'):
            os.remove('webkit.lock')
        self.p = None
        self.p = FreeStationApp(alive_time, enable_gui)
        self.p.setName(self.PROCESS_MONITORED_NAME + '-' + str(time()))
        self.threads.append(self.p)
        self.p.start()

        
    def monitor(self, alive_time = None, enable_gui = True, executions = None):
        ''' 
            Check if the app is alive, and restart if the app dead 
        '''
        while(self.play):
            LOG.debug('Sleeping {0} seconds'.format(str(self.MONITOR_TIME)))
            sleep(self.MONITOR_TIME)
            
            if not hasattr(self, 'p'):
                self.launch(alive_time, enable_gui)
                
            LOG.debug('Checking if alive...')
            
            #print (self.threads)
            
            if(self.p.is_alive() == False):
                self.p.on_destroy(None, None)
                
                # Join all threads using a timeout so it doesn't block
                # Filter out threads which have been joined or are None
                self.threads = [t.join(1) for t in self.threads if t is not None and t.isAlive()]
            
                LOG.error('Restarting process ...')
                self.launch(alive_time, enable_gui)
                
                if executions:
                    executions -= 1
                    if executions <= 0:
                        self.exit()
            else:
                LOG.debug('Process still running: {0}'.format(self.p.getName()))

                self.p.join(timeout=1.0)
    
    def stop(self):
        self.play = False
    
    def exit(self): #@ReservedAssignment
        '''
            Send a SIGTERM himself
        '''
        kill(getpid(), SIGTERM)