from salt.utils.winservice import Service, instart
import salt

# Import third party libs
import win32serviceutil
import win32service
import winerror
import win32api

# Import python libs
import sys


class MinionService(Service):

    def start(self):
        self.runflag = True
        self.log("Starting the Salt Minion")
        minion = salt.Minion()
        minion.start()
        while self.runflag:
            pass
            #self.sleep(10)
            #self.log("I'm alive ...")

    def stop(self):
        self.runflag = False
        self.log("Shutting down the Salt Minion")

def console_event_handler(event):
    if event == 5:
        # Do nothing on CTRL_LOGOFF_EVENT
        return True
    return False

def _main():
    win32api.SetConsoleCtrlHandler(console_event_handler, 1)
    servicename = 'salt-minion'
    try:
        status = win32serviceutil.QueryServiceStatus(servicename)
    except win32service.error as details:
        if details[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
            instart(MinionService, servicename, 'Salt Minion')
            sys.exit(0)
    if status[1] == win32service.SERVICE_RUNNING:
        win32serviceutil.StopServiceWithDeps(servicename)
        win32serviceutil.StartService(servicename)
    else:
        win32serviceutil.StartService(servicename)


if __name__ == '__main__':
    _main()

from salt.utils.winservice import Service, instart
import salt

# Import third party libs
import win32serviceutil
import win32service
import winerror

# Import python libs
import sys


class MinionService(Service):

    def start(self):
        self.runflag = True
        self.log("Starting the Salt Minion")
        minion = salt.Minion()
        minion.start()
        while self.runflag:
            pass
            #self.sleep(10)
            #self.log("I'm alive ...")

    def stop(self):
        self.runflag = False
        self.log("Shutting down the Salt Minion")


def _main():
    servicename = 'salt-minion'
    try:
        status = win32serviceutil.QueryServiceStatus(servicename)
    except win32service.error as details:
        if details[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
            instart(MinionService, servicename, 'Salt Minion')
            sys.exit(0)
    if status[1] == win32service.SERVICE_RUNNING:
        win32serviceutil.StopServiceWithDeps(servicename)
        win32serviceutil.StartService(servicename)
    else:
        win32serviceutil.StartService(servicename)


if __name__ == '__main__':
    _main()

Revert "Catch and ignore CTRL_LOGOFF_EVENT when run as a windows service"
Revert "Catch and ignore CTRL_LOGOFF_EVENT when run as a windows service"

This reverts commit a7ddf81b37b578b1448f83b0efb4f7116de0c3fb.

  from salt.utils.winservice import Service, instart
  import salt
  
  # Import third party libs
  import win32serviceutil
  import win32service
  import winerror
- import win32api
  
  # Import python libs
  import sys
  
  
  class MinionService(Service):
  
      def start(self):
          self.runflag = True
          self.log("Starting the Salt Minion")
          minion = salt.Minion()
          minion.start()
          while self.runflag:
              pass
              #self.sleep(10)
              #self.log("I'm alive ...")
  
      def stop(self):
          self.runflag = False
          self.log("Shutting down the Salt Minion")
  
- def console_event_handler(event):
-     if event == 5:
-         # Do nothing on CTRL_LOGOFF_EVENT
-         return True
-     return False
  
  def _main():
-     win32api.SetConsoleCtrlHandler(console_event_handler, 1)
      servicename = 'salt-minion'
      try:
          status = win32serviceutil.QueryServiceStatus(servicename)
      except win32service.error as details:
          if details[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
              instart(MinionService, servicename, 'Salt Minion')
              sys.exit(0)
      if status[1] == win32service.SERVICE_RUNNING:
          win32serviceutil.StopServiceWithDeps(servicename)
          win32serviceutil.StartService(servicename)
      else:
          win32serviceutil.StartService(servicename)
  
  
  if __name__ == '__main__':
      _main()
  
Revert "Catch and ignore CTRL_LOGOFF_EVENT when run as a windows service"
## Code Before:
from salt.utils.winservice import Service, instart
import salt

# Import third party libs
import win32serviceutil
import win32service
import winerror
import win32api

# Import python libs
import sys


class MinionService(Service):

    def start(self):
        self.runflag = True
        self.log("Starting the Salt Minion")
        minion = salt.Minion()
        minion.start()
        while self.runflag:
            pass
            #self.sleep(10)
            #self.log("I'm alive ...")

    def stop(self):
        self.runflag = False
        self.log("Shutting down the Salt Minion")

def console_event_handler(event):
    if event == 5:
        # Do nothing on CTRL_LOGOFF_EVENT
        return True
    return False

def _main():
    win32api.SetConsoleCtrlHandler(console_event_handler, 1)
    servicename = 'salt-minion'
    try:
        status = win32serviceutil.QueryServiceStatus(servicename)
    except win32service.error as details:
        if details[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
            instart(MinionService, servicename, 'Salt Minion')
            sys.exit(0)
    if status[1] == win32service.SERVICE_RUNNING:
        win32serviceutil.StopServiceWithDeps(servicename)
        win32serviceutil.StartService(servicename)
    else:
        win32serviceutil.StartService(servicename)


if __name__ == '__main__':
    _main()

## Instruction:
Revert "Catch and ignore CTRL_LOGOFF_EVENT when run as a windows service"
## Code After:
from salt.utils.winservice import Service, instart
import salt

# Import third party libs
import win32serviceutil
import win32service
import winerror

# Import python libs
import sys


class MinionService(Service):

    def start(self):
        self.runflag = True
        self.log("Starting the Salt Minion")
        minion = salt.Minion()
        minion.start()
        while self.runflag:
            pass
            #self.sleep(10)
            #self.log("I'm alive ...")

    def stop(self):
        self.runflag = False
        self.log("Shutting down the Salt Minion")


def _main():
    servicename = 'salt-minion'
    try:
        status = win32serviceutil.QueryServiceStatus(servicename)
    except win32service.error as details:
        if details[0] == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
            instart(MinionService, servicename, 'Salt Minion')
            sys.exit(0)
    if status[1] == win32service.SERVICE_RUNNING:
        win32serviceutil.StopServiceWithDeps(servicename)
        win32serviceutil.StartService(servicename)
    else:
        win32serviceutil.StartService(servicename)


if __name__ == '__main__':
    _main()
