[Zope-dev] NT Service

Toby Dickenson htrd90@zepler.org
Wed, 31 Mar 1999 21:23:49 GMT


I'm suprised noone could suggest an easy way to run Zope as an NT Service, but this is so useful I thought it would be worth writing. The script below uses the service support from the win32 extensions package, at http://www.python.org/windows, although I have not tested it with the binary distributions.

Hopefully this is useful to someone else too,

Toby Dickenson

----

port=80

swhome='D:/Progra~1/Zope'
python_cmd='d:/progra~1/python/python'

import sys
import os
import win32serviceutil
import win32service
import time
import thread
import string

sys.path.insert(0, '%s/lib/python' % swhome)
sys.path.insert(1, '%s' % swhome)

import ZopeHTTPServer
from ZPublisher import Client

try:
 access=string.split(open('%s/access'%swhome).read(),':')
 zope_admin_user_name=string.strip(access[0])
 zope_admin_password=string.strip(access[1])
except:
 zope_admin_user_name=""
 zope_admin_password=""


def run_zope():
 args = ['-p', str(port), 'BOBO_DEBUG_MODE=1', '%s/lib/python/Main.py'%swhome ]
 try: ZopeHTTPServer.main(args)
 except SystemExit: pass

def stop_zope():
  url="http://localhost:%d/Control_Panel/manage_shutdown"%port
  f=Client.Function(url)
  f.username=zope_admin_user_name
  f.password=zope_admin_password
  try:
   f()
  except:
   pass


class ZopeService(win32serviceutil.ServiceFramework):
 _svc_name_ = "OZope"
 _svc_display_name_ = "Zope"

 def SvcStop(self):
  print "Stop request received"
  self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  # need make http rpc to zope in a different thread since we need to return from
  # this stop request sooner than the rpc will complete. This would not
  # be a problem if ZopeHTTPServer would shutdown the socket when an exception escapes
  thread.start_new_thread(stop_zope,())

 SvcShutdown=SvcStop

 def SvcDoRun(self):
  try: sys.stdout=sys.stderr=open("%s/service.log"%swhome,"a",0)
  except: pass
  run_zope()
  print "Service exiting"

if __name__=='__main__':
 win32serviceutil.HandleCommandLine(ZopeService)