"""
Windows service handler for the Sqmediumlite back-end process,
invoked by the service() method of the dbpai2 module.
This depends on the pywin32 project. Note that the source
distribtion of pywin32 further depends on the Windows SDK,
whereas the binary distribution (.exe) does not.
"""
import os, sys
if sys.version < '3':
import urllib as parse
else:
from urllib import parse
import win32serviceutil, win32service, servicemanager
surname="Sqmedium "
def main (argv=None):
if os.path.abspath (__file__).startswith (os.path.abspath ('.')):
raise Exception ("Service requires sqmediumlite imported as a site-package")
try:
win32serviceutil.HandleCommandLine (
Service,
argv=argv,
)
except SystemExit:
if argv is None: raise
if len (argv) <= 1: raise Exception ("missing service parameter")
elif len (argv) == 2: raise Exception ("wrong parameter " + argv [1])
else: raise Exception (
"parameter error: " + ' '.join (argv[1:]) +
" (NOTE: options may NOT be as documented!)")
class Service (win32serviceutil.ServiceFramework):
_svc_display_name_ = surname + os.path.abspath ('.')
_svc_name_ = parse.quote (_svc_display_name_)
_svc_description_ = "Back-end for sqmediumlite"
_svc_reg_class_ = "%s.%s" % (
os.path.splitext(os.path.abspath(__file__))[0], "Service")
def __init__(self, args):
self._svc_name_ = args [0]
self._svc_display_name_ = parse.unquote (self._svc_name_)
assert self._svc_display_name_.startswith (surname)
homedir = self._svc_display_name_ [len (surname):]
os.chdir (homedir)
sys.path.insert (0, homedir)
global sqlite
import sqmedium as sqlite
win32serviceutil.ServiceFramework.__init__(self, args)
def SvcStop(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_display_name_, "")
)
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
sqlite.stop ()
def SvcDoRun(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_display_name_, "")
)
sqlite.start (foreground=True)
if __name__ == "__main__":
main ()