Beispiel #1
0
 def Start(self):
     self.CheckCurrentState()
     if self.started:
         print("Ignoring start request - server already running")
         return
     if self.use_service:
         if verbose: print("Doing 'Start' via service")
         if self.GetServiceStatus() in stoppedStatus:
             self.StartService()
         else:
             print("Service was already running - ignoring!")
     else:
         if verbose: print("Doing 'Start' internally")
         if not self.have_prepared_state:
             try:
                 sb_server.prepare()
                 self.have_prepared_state = True
             except sb_server.AlreadyRunningException:
                 msg = "The proxy is already running on this " \
                       "machine - please\r\n stop the existing " \
                       "proxy, and try again."
                 self.ShowMessage(msg)
                 return
         self.StartProxyThread()
     self.started = True
     self.UpdateUIState()
Beispiel #2
0
    def SvcDoRun(self):
        import servicemanager

        try:
            sb_server.prepare(can_stop=False)
        except sb_server.AlreadyRunningException:
            msg = (
                "The SpamBayes proxy service could not be started, as "
                "another SpamBayes server is already running on this machine"
            )
            servicemanager.LogErrorMsg(msg)
            errCode = winerror.ERROR_SERVICE_SPECIFIC_ERROR
            self.ReportServiceStatus(win32service.SERVICE_STOPPED, win32ExitCode=errCode, svcExitCode=1)
            return
        assert not sb_server.state.launchUI, "Service can't launch a UI"
        thread = threading.Thread(target=self.ServerThread)
        thread.start()
        from spambayes.Options import optionsPathname

        extra = " as user '%s', using config file '%s'" % (win32api.GetUserName(), optionsPathname)
        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, extra)
        )
        try:
            self.event_stopping.wait()
            for i in range(60):
                self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
                self.event_stopped.wait(1)
                if self.event_stopped.isSet():
                    break
                print "The service is still shutting down..."
            else:
                print "The worker failed to stop - aborting it anyway"
        except KeyboardInterrupt:
            pass
        s = sb_server.state
        status = " after %d sessions (%d ham, %d spam, %d unsure)" % (
            s.totalSessions,
            s.numHams,
            s.numSpams,
            s.numUnsure,
        )
        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, status)
        )
    def SvcDoRun(self):
        import servicemanager
        # Setup our state etc
        try:
            sb_server.prepare(can_stop=False)
        except sb_server.AlreadyRunningException:
            msg = "The SpamBayes proxy service could not be started, as "\
                  "another SpamBayes server is already running on this machine"
            servicemanager.LogErrorMsg(msg)
            errCode = winerror.ERROR_SERVICE_SPECIFIC_ERROR
            self.ReportServiceStatus(win32service.SERVICE_STOPPED,
                                     win32ExitCode=errCode, svcExitCode=1)
            return
        assert not sb_server.state.launchUI, "Service can't launch a UI"

        # Start the thread running the server.
        thread = threading.Thread(target=self.ServerThread)
        thread.start()

        # Write an event log record - in debug mode we will also
        # see this message printed.
        from spambayes.Options import optionsPathname
        extra = " as user '%s', using config file '%s'" \
                % (win32api.GetUserName(),
                   optionsPathname)
        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE,
            servicemanager.PYS_SERVICE_STARTED,
            (self._svc_name_, extra)
            )

        try:
            # Thread running - wait for the stopping event.
            self.event_stopping.wait()
            # Either user requested stop, or thread done - wait for it
            # to actually stop, but reporting we are still alive.
            # Wait up to 60 seconds for shutdown before giving up and
            # exiting uncleanly - we wait for current proxy connections
            # to close, but you have to draw the line somewhere.
            for i in range(60):
                self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
                self.event_stopped.wait(1)
                if self.event_stopped.isSet():
                    break
                print "The service is still shutting down..."
            else:
                # eeek - we timed out - give up in disgust.
                print "The worker failed to stop - aborting it anyway"
        except KeyboardInterrupt:
            pass

        # Write another event log record.
        s = sb_server.state
        status = " after %d sessions (%d ham, %d spam, %d unsure)" % \
                (s.totalSessions, s.numHams, s.numSpams, s.numUnsure)

        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE,
            servicemanager.PYS_SERVICE_STOPPED,
            (self._svc_name_, status)
            )