All pastes #968998 Raw Edit

Stuff

public python v1 · immutable
·published 2008-04-03 13:01 UTC
rendered paste body
import init_agentimport agentimport sysimport timeimport win32serviceutil, win32evtlogutil, win32event, win32service, pywintypesimport AgentThreadq=init_agent.qclass QAgent(win32serviceutil.ServiceFramework):            _svc_name_ = "QAgent"    _svc_display_name_ = "Q-layer agent"    _svc_description_ = "Q-layer agent"    def __init__(self, args):        win32serviceutil.ServiceFramework.__init__(self, args)        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)        self.redirectOutput()        self.overlapped = pywintypes.OVERLAPPED()        self.overlapped.hEvent = win32event.CreateEvent(None,0,0,None)        def redirectOutput(self):        sys.stdout.close()        sys.stderr.close()        sys.stdout = NullOutput()        sys.stderr = NullOutput()        def SvcDoRun(self):        import servicemanager                servicemanager.LogMsg(            servicemanager.EVENTLOG_INFORMATION_TYPE,            servicemanager.PYS_SERVICE_STARTED,            (self._svc_name_, 'Jochen (%s)' % self._svc_display_name_))        interval = 1000                # Start the agent thread        stoppy = AgentThread.StopObject()        at = AgentThread.AgentThread(q)        at.setAgentInifile(q.vars.getVar('agentinifile'))        at.setStopObject(stoppy)        at.start()        while True:            # Listen for incoming events             rc = win32event.WaitForMultipleObjects((self.hWaitStop, self.overlapped.hEvent), 0, interval)              if rc == win32event.WAIT_OBJECT_0:                stoppy.stopNow()                # Wait for agent thread to die                while at.isAlive() == True:                    time.sleep(2)                break            else:                time.sleep(2)        def SvcStop(self):        # Tell the SCM that we are closing        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)        # set the stop event        win32event.SetEvent(self.hWaitStop)        class NullOutput:    """A stdout / stderr replacement that discards everything."""    def noop(self, *args, **kw):        pass    write = writelines = close = seek = flush = truncate = noop    def __iter__(self):        return self    def next(self):        raise StopIteration    def isatty(self):        return False    def tell(self):        return 0    def read(self, *args, **kw):        return ''    readline = read    def readlines(self, *args, **kw):        return []   if __name__ == '__main__':     win32serviceutil.HandleCommandLine(QAgent)    q.application.stop(0)