Commit 2fee3425 authored by Amos Latteier's avatar Amos Latteier

Fixed event log logging. Made installation of the service set a sane (hopefully)

value for the ZServer start command. Changed naming conventions for the service.
parent 52b91eaf
...@@ -177,22 +177,23 @@ Usage: ...@@ -177,22 +177,23 @@ Usage:
TODO: TODO:
* Integrate it into the Windows installer. * Integrate it into the Windows installer.
* Fix event logging, probably switch to new Zope logging framework * Add ZLOG logging in addition to event log logging.
* Make it easier to run multiple Zope services with one Zope install * Make it easier to run multiple Zope services with one Zope install
This script does for NT the same sort of thing zdeamon.py does for UNIX. This script does for NT the same sort of thing zdaemon.py does for UNIX.
Requires Python win32api extensions. Requires Python win32api extensions.
""" """
import win32api, win32serviceutil, win32service, win32event, win32process import win32api, win32serviceutil, win32service, win32event, win32process
import win32evtlog, win32evtlogutil try: import servicemanager
except: pass
import time, imp, sys import time, imp, sys
try: try:
import App.version_txt import App.version_txt
ZOPE_VERSION=App.version_txt.version_txt() ZOPE_VERSION=App.version_txt.version_txt()
except: except:
ZOPE_VERSION='1.10' ZOPE_VERSION='1.10.x'
# pythoncom and pywintypes are special, and require these hacks when # pythoncom and pywintypes are special, and require these hacks when
# we dont have a standard Python installation around. # we dont have a standard Python installation around.
...@@ -218,8 +219,8 @@ def magic_import(modulename, filename): ...@@ -218,8 +219,8 @@ def magic_import(modulename, filename):
magic_import('pywintypes','pywintypes15.dll') magic_import('pywintypes','pywintypes15.dll')
class ZServerService(win32serviceutil.ServiceFramework): class ZServerService(win32serviceutil.ServiceFramework):
_svc_name_ = "ZServerService" _svc_name_ = "Zope%s" % ZOPE_VERSION
_svc_display_name_ = "Zope (%s) %s" % (ZOPE_VERSION, _svc_name_) _svc_display_name_ = "Zope (%s)" % ZOPE_VERSION
restart_min_time=5 # if ZServer restarts before this many restart_min_time=5 # if ZServer restarts before this many
# seconds then we have a problem, and # seconds then we have a problem, and
...@@ -228,9 +229,6 @@ class ZServerService(win32serviceutil.ServiceFramework): ...@@ -228,9 +229,6 @@ class ZServerService(win32serviceutil.ServiceFramework):
def __init__(self, args): def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args) win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
# win32evtlog.pyd doesn't seem to be a good event source, hmm
dll = win32api.GetModuleFileName(win32api.GetModuleHandle("win32evtlog.pyd"))
win32evtlogutil.AddSourceToRegistry(self._svc_name_, dll)
def SvcDoRun(self): def SvcDoRun(self):
self.start_zserver() self.start_zserver()
...@@ -244,12 +242,9 @@ class ZServerService(win32serviceutil.ServiceFramework): ...@@ -244,12 +242,9 @@ class ZServerService(win32serviceutil.ServiceFramework):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000) self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000)
def SvcStop(self): def SvcStop(self):
servicemanager.LogInfoMsg('Stopping Zope.')
try: try:
self.stop_zserver() self.stop_zserver()
win32evtlogutil.ReportEvent(self._svc_name_, 2,
eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE,
strings=["Stopping Zope."]
)
except: except:
pass pass
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
...@@ -260,20 +255,14 @@ class ZServerService(win32serviceutil.ServiceFramework): ...@@ -260,20 +255,14 @@ class ZServerService(win32serviceutil.ServiceFramework):
None, None, 0, 0, None, None, win32process.STARTUPINFO()) None, None, 0, 0, None, None, win32process.STARTUPINFO())
self.hZServer=result[0] self.hZServer=result[0]
self.last_start_time=time.time() self.last_start_time=time.time()
win32evtlogutil.ReportEvent(self._svc_name_, 1, servicemanager.LogInfoMsg('Starting Zope.')
eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE,
strings=["Starting Zope."]
)
def stop_zserver(self): def stop_zserver(self):
win32process.TerminateProcess(self.hZServer,0) win32process.TerminateProcess(self.hZServer,0)
def restart_zserver(self): def restart_zserver(self):
if time.time() - self.last_start_time < self.restart_min_time: if time.time() - self.last_start_time < self.restart_min_time:
win32evtlogutil.ReportEvent(self._svc_name_, 4, servicemanager.LogErrorMsg('Zope died and could not be restarted.')
eventType=win32evtlog.EVENTLOG_ERROR_TYPE,
strings=["Zope died and could not be restarted."]
)
self.SvcStop() self.SvcStop()
code=win32process.GetExitCodeProcess(self.hZServer) code=win32process.GetExitCodeProcess(self.hZServer)
if code == 0: if code == 0:
...@@ -281,23 +270,24 @@ class ZServerService(win32serviceutil.ServiceFramework): ...@@ -281,23 +270,24 @@ class ZServerService(win32serviceutil.ServiceFramework):
# assume that shutdown is intentional. # assume that shutdown is intentional.
self.SvcStop() self.SvcStop()
else: else:
win32evtlogutil.ReportEvent(self._svc_name_, 3, servicemanager.LogWarningMsg('Restarting Zope.')
eventType=win32evtlog.EVENTLOG_WARNING_TYPE,
strings=["Restarting Zope."]
)
self.start_zserver() self.start_zserver()
def get_start_command(self): def get_start_command(self):
return win32serviceutil.GetServiceCustomOption(self,'start') return win32serviceutil.GetServiceCustomOption(self,'start')
def option_handler(options): def set_start_command(value):
for opt, value in options: "sets the ZServer start command"
if opt == '-z':
win32serviceutil.SetServiceCustomOption(ZServerService,'start',value) win32serviceutil.SetServiceCustomOption(ZServerService,'start',value)
print "Setting ZServer start command"
if __name__=='__main__':
win32serviceutil.HandleCommandLine(ZServerService,
customInstallOptions='z:', customOptionHandler=option_handler)
if __name__=='__main__':
win32serviceutil.HandleCommandLine(ZServerService)
if sys.argv[1]=='install':
if win32serviceutil.GetServiceCustomOption(ZServerService,'start') is None:
import string, os.path
home=string.split(sys.argv[0],'ZServer')[0]
command='"%s" "%s"' % (sys.executable, os.path.join(home,'z2.py'))
set_start_command(command)
print "Setting ZServer start command to:", command
...@@ -177,22 +177,23 @@ Usage: ...@@ -177,22 +177,23 @@ Usage:
TODO: TODO:
* Integrate it into the Windows installer. * Integrate it into the Windows installer.
* Fix event logging, probably switch to new Zope logging framework * Add ZLOG logging in addition to event log logging.
* Make it easier to run multiple Zope services with one Zope install * Make it easier to run multiple Zope services with one Zope install
This script does for NT the same sort of thing zdeamon.py does for UNIX. This script does for NT the same sort of thing zdaemon.py does for UNIX.
Requires Python win32api extensions. Requires Python win32api extensions.
""" """
import win32api, win32serviceutil, win32service, win32event, win32process import win32api, win32serviceutil, win32service, win32event, win32process
import win32evtlog, win32evtlogutil try: import servicemanager
except: pass
import time, imp, sys import time, imp, sys
try: try:
import App.version_txt import App.version_txt
ZOPE_VERSION=App.version_txt.version_txt() ZOPE_VERSION=App.version_txt.version_txt()
except: except:
ZOPE_VERSION='1.10' ZOPE_VERSION='1.10.x'
# pythoncom and pywintypes are special, and require these hacks when # pythoncom and pywintypes are special, and require these hacks when
# we dont have a standard Python installation around. # we dont have a standard Python installation around.
...@@ -218,8 +219,8 @@ def magic_import(modulename, filename): ...@@ -218,8 +219,8 @@ def magic_import(modulename, filename):
magic_import('pywintypes','pywintypes15.dll') magic_import('pywintypes','pywintypes15.dll')
class ZServerService(win32serviceutil.ServiceFramework): class ZServerService(win32serviceutil.ServiceFramework):
_svc_name_ = "ZServerService" _svc_name_ = "Zope%s" % ZOPE_VERSION
_svc_display_name_ = "Zope (%s) %s" % (ZOPE_VERSION, _svc_name_) _svc_display_name_ = "Zope (%s)" % ZOPE_VERSION
restart_min_time=5 # if ZServer restarts before this many restart_min_time=5 # if ZServer restarts before this many
# seconds then we have a problem, and # seconds then we have a problem, and
...@@ -228,9 +229,6 @@ class ZServerService(win32serviceutil.ServiceFramework): ...@@ -228,9 +229,6 @@ class ZServerService(win32serviceutil.ServiceFramework):
def __init__(self, args): def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args) win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
# win32evtlog.pyd doesn't seem to be a good event source, hmm
dll = win32api.GetModuleFileName(win32api.GetModuleHandle("win32evtlog.pyd"))
win32evtlogutil.AddSourceToRegistry(self._svc_name_, dll)
def SvcDoRun(self): def SvcDoRun(self):
self.start_zserver() self.start_zserver()
...@@ -244,12 +242,9 @@ class ZServerService(win32serviceutil.ServiceFramework): ...@@ -244,12 +242,9 @@ class ZServerService(win32serviceutil.ServiceFramework):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000) self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000)
def SvcStop(self): def SvcStop(self):
servicemanager.LogInfoMsg('Stopping Zope.')
try: try:
self.stop_zserver() self.stop_zserver()
win32evtlogutil.ReportEvent(self._svc_name_, 2,
eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE,
strings=["Stopping Zope."]
)
except: except:
pass pass
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
...@@ -260,20 +255,14 @@ class ZServerService(win32serviceutil.ServiceFramework): ...@@ -260,20 +255,14 @@ class ZServerService(win32serviceutil.ServiceFramework):
None, None, 0, 0, None, None, win32process.STARTUPINFO()) None, None, 0, 0, None, None, win32process.STARTUPINFO())
self.hZServer=result[0] self.hZServer=result[0]
self.last_start_time=time.time() self.last_start_time=time.time()
win32evtlogutil.ReportEvent(self._svc_name_, 1, servicemanager.LogInfoMsg('Starting Zope.')
eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE,
strings=["Starting Zope."]
)
def stop_zserver(self): def stop_zserver(self):
win32process.TerminateProcess(self.hZServer,0) win32process.TerminateProcess(self.hZServer,0)
def restart_zserver(self): def restart_zserver(self):
if time.time() - self.last_start_time < self.restart_min_time: if time.time() - self.last_start_time < self.restart_min_time:
win32evtlogutil.ReportEvent(self._svc_name_, 4, servicemanager.LogErrorMsg('Zope died and could not be restarted.')
eventType=win32evtlog.EVENTLOG_ERROR_TYPE,
strings=["Zope died and could not be restarted."]
)
self.SvcStop() self.SvcStop()
code=win32process.GetExitCodeProcess(self.hZServer) code=win32process.GetExitCodeProcess(self.hZServer)
if code == 0: if code == 0:
...@@ -281,23 +270,24 @@ class ZServerService(win32serviceutil.ServiceFramework): ...@@ -281,23 +270,24 @@ class ZServerService(win32serviceutil.ServiceFramework):
# assume that shutdown is intentional. # assume that shutdown is intentional.
self.SvcStop() self.SvcStop()
else: else:
win32evtlogutil.ReportEvent(self._svc_name_, 3, servicemanager.LogWarningMsg('Restarting Zope.')
eventType=win32evtlog.EVENTLOG_WARNING_TYPE,
strings=["Restarting Zope."]
)
self.start_zserver() self.start_zserver()
def get_start_command(self): def get_start_command(self):
return win32serviceutil.GetServiceCustomOption(self,'start') return win32serviceutil.GetServiceCustomOption(self,'start')
def option_handler(options): def set_start_command(value):
for opt, value in options: "sets the ZServer start command"
if opt == '-z':
win32serviceutil.SetServiceCustomOption(ZServerService,'start',value) win32serviceutil.SetServiceCustomOption(ZServerService,'start',value)
print "Setting ZServer start command"
if __name__=='__main__':
win32serviceutil.HandleCommandLine(ZServerService,
customInstallOptions='z:', customOptionHandler=option_handler)
if __name__=='__main__':
win32serviceutil.HandleCommandLine(ZServerService)
if sys.argv[1]=='install':
if win32serviceutil.GetServiceCustomOption(ZServerService,'start') is None:
import string, os.path
home=string.split(sys.argv[0],'ZServer')[0]
command='"%s" "%s"' % (sys.executable, os.path.join(home,'z2.py'))
set_start_command(command)
print "Setting ZServer start command to:", command
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment