[Checkins] SVN: z3c.recipe.winservice/ Added windows service recipe

Roger Ineichen roger at projekt01.ch
Sat Apr 12 14:00:57 EDT 2008


Log message for revision 85277:
  Added windows service recipe

Changed:
  A   z3c.recipe.winservice/branches/
  A   z3c.recipe.winservice/tags/
  A   z3c.recipe.winservice/trunk/
  A   z3c.recipe.winservice/trunk/CHANGES.txt
  A   z3c.recipe.winservice/trunk/README.txt
  A   z3c.recipe.winservice/trunk/bootstrap.py
  A   z3c.recipe.winservice/trunk/buildout.cfg
  A   z3c.recipe.winservice/trunk/setup.py
  A   z3c.recipe.winservice/trunk/src/
  A   z3c.recipe.winservice/trunk/src/z3c/
  A   z3c.recipe.winservice/trunk/src/z3c/__init__.py
  A   z3c.recipe.winservice/trunk/src/z3c/recipe/
  A   z3c.recipe.winservice/trunk/src/z3c/recipe/__init__.py
  A   z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/
  A   z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/README.txt
  A   z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/__init__.py
  A   z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/service.py
  A   z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/tests.py
  A   z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/winservice.in

-=-

Property changes on: z3c.recipe.winservice/trunk
___________________________________________________________________
Name: svn:ignore
   + bin
.installed.cfg
develop-eggs
parts


Added: z3c.recipe.winservice/trunk/CHANGES.txt
===================================================================
--- z3c.recipe.winservice/trunk/CHANGES.txt	                        (rev 0)
+++ z3c.recipe.winservice/trunk/CHANGES.txt	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,8 @@
+=======
+CHANGES
+=======
+
+0.5.0 (unreleased)
+------------------
+
+- Initial Release


Property changes on: z3c.recipe.winservice/trunk/CHANGES.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/README.txt
===================================================================
--- z3c.recipe.winservice/trunk/README.txt	                        (rev 0)
+++ z3c.recipe.winservice/trunk/README.txt	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,2 @@
+The ``z3c.recipe.winservice:service`` generates a service scripts and 
+configuration files for starting a egg based Zope 3 setup as a windows service.


Property changes on: z3c.recipe.winservice/trunk/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/bootstrap.py
===================================================================
--- z3c.recipe.winservice/trunk/bootstrap.py	                        (rev 0)
+++ z3c.recipe.winservice/trunk/bootstrap.py	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,56 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Bootstrap a buildout-based project
+
+Simply run this script in a directory containing a buildout.cfg.
+The script accepts buildout command-line options, so you can
+use the -c option to specify an alternate configuration file.
+
+$Id: bootstrap.py 77225 2007-06-29 09:20:13Z dobe $
+"""
+
+import os, shutil, sys, tempfile, urllib2
+
+tmpeggs = tempfile.mkdtemp()
+
+try:
+    import pkg_resources
+except ImportError:
+    ez = {}
+    exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
+                         ).read() in ez
+    ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
+
+    import pkg_resources
+
+cmd = 'from setuptools.command.easy_install import main; main()'
+if sys.platform == 'win32':
+    cmd = '"%s"' % cmd # work around spawn lamosity on windows
+
+ws = pkg_resources.working_set
+assert os.spawnle(
+    os.P_WAIT, sys.executable, sys.executable,
+    '-c', cmd, '-mqNxd', tmpeggs, 'zc.buildout',
+    dict(os.environ,
+         PYTHONPATH=
+         ws.find(pkg_resources.Requirement.parse('setuptools')).location
+         ),
+    ) == 0
+
+ws.add_entry(tmpeggs)
+ws.require('zc.buildout')
+import zc.buildout.buildout
+zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
+shutil.rmtree(tmpeggs)
+


Property changes on: z3c.recipe.winservice/trunk/bootstrap.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/buildout.cfg
===================================================================
--- z3c.recipe.winservice/trunk/buildout.cfg	                        (rev 0)
+++ z3c.recipe.winservice/trunk/buildout.cfg	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,7 @@
+[buildout]
+parts = test
+develop = .
+
+[test]
+recipe = zc.recipe.testrunner
+eggs = z3c.recipe.winservice [test]

Added: z3c.recipe.winservice/trunk/setup.py
===================================================================
--- z3c.recipe.winservice/trunk/setup.py	                        (rev 0)
+++ z3c.recipe.winservice/trunk/setup.py	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,73 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Setup
+
+$Id: setup.py 82497 2007-12-28 14:59:22Z rogerineichen $
+"""
+import os
+import xml.sax.saxutils
+from setuptools import setup, find_packages
+
+
+def read(*rnames):
+    text = open(os.path.join(os.path.dirname(__file__), *rnames)).read()
+    return xml.sax.saxutils.escape(text)
+
+setup(
+    name = 'z3c.recipe.winservice',
+    version = '0.5.0dev',
+    author = 'Roger Ineichen and the Zope Community',
+    author_email = 'zope-dev at zope.org',
+    description = 'Zope3 windows service installer',
+    long_description=(
+        read('README.txt')
+        + '\n\n' +
+        read('CHANGES.txt')
+        ),
+    license = 'ZPL 2.1',
+    keywords = 'zope zope3 z3c deploy recipe windows service',
+    classifiers = [
+        'Development Status :: 3 - Alpha',
+        'Environment :: Web Environment',
+        'Intended Audience :: Developers',
+        'License :: OSI Approved :: Zope Public License',
+        'Programming Language :: Python',
+        'Natural Language :: English',
+        'Operating System :: OS Independent',
+        'Topic :: Internet :: WWW/HTTP',
+        'Framework :: Zope3'],
+    url = 'http://pypi.python.org/pypi/z3c.recipe.winservice',
+    packages = find_packages('src'),
+    include_package_data = True,
+    package_dir = {'':'src'},
+    namespace_packages = ['z3c', 'z3c.recipe'],
+    extras_require = dict(
+        test = [
+            'zope.testing',
+            'zc.recipe.egg',
+            'ZConfig >=2.4a5',
+            ],
+        ),
+    install_requires = [
+        'ZConfig >=2.4a5',
+        'setuptools',
+        'zc.buildout',
+        'zc.recipe.egg',
+        ],
+    entry_points = {
+        'zc.buildout': [
+             'service = z3c.recipe.winservice.service:ServiceSetup',
+         ]
+    },
+)


Property changes on: z3c.recipe.winservice/trunk/setup.py
___________________________________________________________________
Name: svn:eol-style
   + native


Property changes on: z3c.recipe.winservice/trunk/src
___________________________________________________________________
Name: svn:ignore
   + z3c.recipe.winservice.egg-info


Added: z3c.recipe.winservice/trunk/src/z3c/__init__.py
===================================================================
--- z3c.recipe.winservice/trunk/src/z3c/__init__.py	                        (rev 0)
+++ z3c.recipe.winservice/trunk/src/z3c/__init__.py	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,7 @@
+# this is a namespace package
+try:
+    import pkg_resources
+    pkg_resources.declare_namespace(__name__)
+except ImportError:
+    import pkgutil
+    __path__ = pkgutil.extend_path(__path__, __name__)


Property changes on: z3c.recipe.winservice/trunk/src/z3c/__init__.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/src/z3c/recipe/__init__.py
===================================================================
--- z3c.recipe.winservice/trunk/src/z3c/recipe/__init__.py	                        (rev 0)
+++ z3c.recipe.winservice/trunk/src/z3c/recipe/__init__.py	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,6 @@
+# namespace package boilerplate
+try:
+    __import__('pkg_resources').declare_namespace(__name__)
+except ImportError, e:
+    from pkgutil import extend_path
+    __path__ = extend_path(__path__, __name__)


Property changes on: z3c.recipe.winservice/trunk/src/z3c/recipe/__init__.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/README.txt
===================================================================
--- z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/README.txt	                        (rev 0)
+++ z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/README.txt	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,416 @@
+=====================
+Z3 development recipe
+=====================
+
+z3c.recipe.winservice
+---------------------
+
+This Zope 3 recipes offers windows service installation support.
+
+The 'service' recipe installes the required scripts and files whihc can be 
+used for install a windwos service.
+
+
+Options
+*******
+
+The 'service' recipe accepts the following options:
+
+name
+  The windows service name option.
+
+description
+  The windows service description option.
+
+runzope
+  The script name which get used by the winservice. This script must exist in 
+  the bin folder and before we run this recipe exist. This script can get setup 
+  with the z3c.recipe.dev.app recipe.
+
+
+Test
+****
+
+Lets define some (bogus) eggs that we can use in our application:
+
+  >>> mkdir('demo1')
+  >>> write('demo1', 'setup.py',
+  ... '''
+  ... from setuptools import setup
+  ... setup(name = 'demo1')
+  ... ''')
+
+  >>> mkdir('demo2')
+  >>> write('demo2', 'setup.py',
+  ... '''
+  ... from setuptools import setup
+  ... setup(name = 'demo2', install_requires='demo1')
+  ... ''')
+
+We also need to setup an application. This is normaly done with the app recipe
+defined in z3c.recipe.dev:
+
+  >>> write('bin', 'app-script.py',
+  ... '''
+  ... dummy start script
+  ... ''')
+
+
+Now check if the setup was correct. This is true if we have already the
+buildout files and our fake app-script.py installed:
+
+  >>> ls('bin')
+  -  app-script.py
+  -  buildout-script.py
+  -  buildout.exe
+
+We'll create a `buildout.cfg` file that defines our winservice configuration:
+
+  >>> write('buildout.cfg',
+  ... '''
+  ... [buildout]
+  ... develop = demo1 demo2
+  ... parts = winservice
+  ...
+  ... [winservice]
+  ... recipe = z3c.recipe.winservice:service
+  ... name = Zope 3 Windows Service
+  ... description = Zope 3 Windows Service description
+  ... runzope = app
+  ...
+  ... ''' % globals())
+
+Now, Let's run the buildout and see what we get:
+
+  >>> print system(join('bin', 'buildout')),
+  Develop: '/sample-buildout/demo1'
+  Develop: '/sample-buildout/demo2'
+  Installing winservice.
+
+
+The bin folder contains the windows service installer script:
+
+  >>> ls('bin')
+  -  app-script.py
+  -  buildout-script.py
+  -  buildout.exe
+  -  winservice.py
+
+The winservice-scrip.py contains the service setup for our zope windows service:
+
+  >>> cat('bin', 'winservice.py')
+  ##############################################################################
+  #
+  # Copyright (c) 2008 Zope Foundation and Contributors.
+  # All Rights Reserved.
+  #
+  # This software is subject to the provisions of the Zope Public License,
+  # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+  # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+  # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+  # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+  # FOR A PARTICULAR PURPOSE.
+  #
+  ##############################################################################
+  """A Zope Windows NT service frontend.
+  <BLANKLINE>
+  Usage:
+  <BLANKLINE>
+    Installation
+  <BLANKLINE>
+      The Zope service should be installed by the Zope Windows
+      installer. You can manually install, uninstall the service from
+      the commandline.
+  <BLANKLINE>
+        zopeservice.py [options] install|update|remove|start [...]
+             |stop|restart [...]|debug [...]
+  <BLANKLINE>
+      Options for 'install' and 'update' commands only:
+  <BLANKLINE>
+       --username domain\username : The Username the service is to run
+                                    under
+  <BLANKLINE>
+       --password password : The password for the username
+  <BLANKLINE>
+       --startup [manual|auto|disabled] : How the service starts,
+                                          default = manual
+  <BLANKLINE>
+      Commands
+  <BLANKLINE>
+        install : Installs the service
+  <BLANKLINE>
+        update : Updates the service, use this when you change
+                 the service class implementation
+  <BLANKLINE>
+        remove : Removes the service
+  <BLANKLINE>
+        start : Starts the service, this can also be done from the
+                services control panel
+  <BLANKLINE>
+        stop : Stops the service, this can also be done from the
+               services control panel
+  <BLANKLINE>
+        restart : Restarts the service
+  <BLANKLINE>
+        debug : Runs the service in debug mode
+  <BLANKLINE>
+      You can view the usage options by running ntservice.py without any
+      arguments.
+  <BLANKLINE>
+      Note: you may have to register the Python service program first,
+  <BLANKLINE>
+        win32\PythonService.exe /register
+  <BLANKLINE>
+    Starting Zope
+  <BLANKLINE>
+      Start Zope by clicking the 'start' button in the services control
+      panel. You can set Zope to automatically start at boot time by
+      choosing 'Auto' startup by clicking the 'statup' button.
+  <BLANKLINE>
+    Stopping Zope
+  <BLANKLINE>
+      Stop Zope by clicking the 'stop' button in the services control
+      panel. You can also stop Zope through the web by going to the
+      Zope control panel and by clicking 'Shutdown'.
+  <BLANKLINE>
+    Event logging
+  <BLANKLINE>
+      Zope events are logged to the NT application event log. Use the
+      event viewer to keep track of Zope events.
+  <BLANKLINE>
+  """
+  <BLANKLINE>
+  import sys, os, time
+  import pywintypes
+  import win32serviceutil
+  import win32service
+  import win32event
+  import win32process
+  <BLANKLINE>
+  <BLANKLINE>
+  # the max seconds we're allowed to spend backing off
+  BACKOFF_MAX = 300
+  # if the process runs successfully for more than BACKOFF_CLEAR_TIME
+  # seconds, we reset the backoff stats to their initial values
+  BACKOFF_CLEAR_TIME = 30
+  # the initial backoff interval (the amount of time we wait to restart
+  # a dead process)
+  BACKOFF_INITIAL_INTERVAL = 5
+  <BLANKLINE>
+  <BLANKLINE>
+  class NullOutput:
+      """A stdout / stderr replacement that discards everything."""
+  <BLANKLINE>
+      def noop(self, *args, **kw):
+          pass
+  <BLANKLINE>
+      write = writelines = close = seek = flush = truncate = noop
+  <BLANKLINE>
+      def __iter__(self):
+          return self
+  <BLANKLINE>
+      def next(self):
+          raise StopIteration
+  <BLANKLINE>
+      def isatty(self):
+          return False
+  <BLANKLINE>
+      def tell(self):
+          return 0
+  <BLANKLINE>
+      def read(self, *args, **kw):
+          return ''
+  <BLANKLINE>
+      readline = read
+  <BLANKLINE>
+      def readlines(self, *args, **kw):
+          return []
+  <BLANKLINE>
+  <BLANKLINE>
+  class Service(win32serviceutil.ServiceFramework):
+      """ A class representing a Windows NT service that can manage an
+      instance-home-based Zope/ZEO/ZRS processes """
+  <BLANKLINE>
+      # The PythonService model requires that an actual on-disk class declaration
+      # represent a single service.  Thus, the below definition of start_cmd,
+      # must be overridden in a subclass in a file within the instance home for
+      # each instance.  The below-defined start_cmd (and _svc_display_name_
+      # and _svc_name_) are just examples.
+  <BLANKLINE>
+      _svc_name_ = r'Zope-Instance'
+      _svc_display_name_ = r'Zope instance at C:\Zope-Instance'
+  <BLANKLINE>
+      start_cmd = (
+          r'"C:\Program Files\Zope-2.7.0-a1\bin\python.exe" '
+          r'"C:\Program Files\Zope-2.7.0-a1\lib\python\Zope\Startup\run.py" '
+          r'-C "C:\Zope-Instance\etc\zope.conf"'
+          )
+  <BLANKLINE>
+      def __init__(self, args):
+          win32serviceutil.ServiceFramework.__init__(self, args)
+          # Create an event which we will use to wait on.
+          # The "service stop" request will set this event.
+          self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
+          self.redirectOutput()
+  <BLANKLINE>
+      def redirectOutput(self):
+          sys.stdout.close()
+          sys.stderr.close()
+          sys.stdout = NullOutput()
+          sys.stderr = NullOutput()
+  <BLANKLINE>
+      def SvcStop(self):
+          # Before we do anything, tell the SCM we are starting the stop process.
+          self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
+  <BLANKLINE>
+          # TODO:  This TerminateProcess call doesn't make much sense:  it's
+          # doing a hard kill _first_, never giving the process a chance to
+          # shut down cleanly.  Compare to current Zope2 service code, which
+          # uses Windows events to give the process a chance to shut down
+          # cleanly, doing a hard kill only if that doesn't succeed.
+  <BLANKLINE>
+          # stop the process if necessary
+          try:
+              win32process.TerminateProcess(self.hZope, 0)
+          except pywintypes.error:
+              # the process may already have been terminated
+              pass
+          # And set my event.
+          win32event.SetEvent(self.hWaitStop)
+  <BLANKLINE>
+      # SvcStop only gets triggered when the user explictly stops (or restarts)
+      # the service.  To shut the service down cleanly when Windows is shutting
+      # down, we also need to hook SvcShutdown.
+      SvcShutdown = SvcStop
+  <BLANKLINE>
+      def createProcess(self, cmd):
+          return win32process.CreateProcess(
+              None, cmd, None, None, 0, 0, None, None,
+              win32process.STARTUPINFO())
+  <BLANKLINE>
+      def SvcDoRun(self):
+          # indicate to Zope that the process is daemon managed (restartable)
+          os.environ['ZMANAGED'] = '1'
+  <BLANKLINE>
+          # daemon behavior:  we want to to restart the process if it
+          # dies, but if it dies too many times, we need to give up.
+  <BLANKLINE>
+          # we use a simple backoff algorithm to determine whether
+          # we should try to restart a dead process:  for each
+          # time the process dies unexpectedly, we wait some number of
+          # seconds to restart it, as determined by the backoff interval,
+          # which doubles each time the process dies.  if we exceed
+          # BACKOFF_MAX seconds in cumulative backoff time, we give up.
+          # at any time if we successfully run the process for more thab
+          # BACKOFF_CLEAR_TIME seconds, the backoff stats are reset.
+  <BLANKLINE>
+          # the initial number of seconds between process start attempts
+          backoff_interval = BACKOFF_INITIAL_INTERVAL
+          # the cumulative backoff seconds counter
+          backoff_cumulative = 0
+  <BLANKLINE>
+          import servicemanager
+  <BLANKLINE>
+          # log a service started message
+          servicemanager.LogMsg(
+              servicemanager.EVENTLOG_INFORMATION_TYPE,
+              servicemanager.PYS_SERVICE_STARTED,
+              (self._svc_name_, ' (%s)' % self._svc_display_name_))
+  <BLANKLINE>
+          while 1:
+              start_time = time.time()
+              info = self.createProcess(self.start_cmd)
+              self.hZope = info[0] # the pid
+              if backoff_interval > BACKOFF_INITIAL_INTERVAL:
+                  # if we're in a backoff state, log a message about
+                  # starting a new process
+                  servicemanager.LogInfoMsg(
+                      '%s (%s): recovering from died process, new process '
+                      'started' % (self._svc_name_, self._svc_display_name_)
+                      )
+              rc = win32event.WaitForMultipleObjects(
+                  (self.hWaitStop, self.hZope), 0, win32event.INFINITE)
+              if rc == win32event.WAIT_OBJECT_0:
+                  # user sent a stop service request
+                  self.SvcStop()
+                  break
+              else:
+                  # user did not send a service stop request, but
+                  # the process died; this may be an error condition
+                  status = win32process.GetExitCodeProcess(self.hZope)
+                  if status == 0:
+                      # the user shut the process down from the web
+                      # interface (or it otherwise exited cleanly)
+                      break
+                  else:
+                      # this was an abormal shutdown.  if we can, we want to
+                      # restart the process but if it seems hopeless,
+                      # don't restart an infinite number of times.
+                      if backoff_cumulative > BACKOFF_MAX:
+                          # it's hopeless
+                          servicemanager.LogErrorMsg(
+                            '%s (%s): process could not be restarted due to max '
+                            'restart attempts exceeded' % (
+                              self._svc_display_name_, self._svc_name_
+                            ))
+                          self.SvcStop()
+                          break
+                      servicemanager.LogWarningMsg(
+                         '%s (%s): process died unexpectedly.  Will attempt '
+                         'restart after %s seconds.' % (
+                              self._svc_name_, self._svc_display_name_,
+                              backoff_interval
+                              )
+                         )
+                      # if BACKOFF_CLEAR_TIME seconds have elapsed since we last
+                      # started the process, reset the backoff interval
+                      # and the cumulative backoff time to their original
+                      # states
+                      if time.time() - start_time > BACKOFF_CLEAR_TIME:
+                          backoff_interval = BACKOFF_INITIAL_INTERVAL
+                          backoff_cumulative = 0
+                      # we sleep for the backoff interval.  since this is async
+                      # code, it would be better done by sending and
+                      # catching a timed event (a service
+                      # stop request will need to wait for us to stop sleeping),
+                      # but this works well enough for me.
+                      time.sleep(backoff_interval)
+                      # update backoff_cumulative with the time we spent
+                      # backing off.
+                      backoff_cumulative = backoff_cumulative + backoff_interval
+                      # bump the backoff interval up by 2* the last interval
+                      backoff_interval = backoff_interval * 2
+  <BLANKLINE>
+                      # loop and try to restart the process
+  <BLANKLINE>
+          # log a service stopped message
+          servicemanager.LogMsg(
+              servicemanager.EVENTLOG_INFORMATION_TYPE,
+              servicemanager.PYS_SERVICE_STOPPED,
+              (self._svc_name_, ' (%s) ' % self._svc_display_name_))
+  <BLANKLINE>
+  # these are replacements from winservice recipe
+  PYTHON = r'...\python.exe'
+  PYTHONDIR = os.path.split(PYTHON)[0]
+  PYTHONW = os.path.join(PYTHONDIR, 'pythonw.exe')
+  PYTHONSERVICE_EXE = r'%s\Lib\site-packages\win32\pythonservice.exe' % PYTHONDIR
+  RUNZOPE = r'/sample-buildout/bin/app-script.py'
+  SERVICE_NAME = str(hash(SERVICE_DISPLAY_NAME))
+  SERVICE_DISPLAY_NAME = 'Zope 3 Windows Service'
+  SERVICE_DESCRIPTION =  'Zope 3 Windows Service description'
+  <BLANKLINE>
+  <BLANKLINE>
+  class InstanceService(Service):
+      start_cmd = '"%s" "%s"' % (PYTHONW, RUNZOPE)
+      _svc_name_ = SERVICE_NAME
+      _svc_display_name_ = SERVICE_DISPLAY_NAME
+      _svc_description_ = SERVICE_DESCRIPTION
+      _exe_name_ = PYTHONSERVICE_EXE
+  <BLANKLINE>
+  <BLANKLINE>
+  if __name__ == '__main__':
+      import win32serviceutil
+      if os.path.exists(PYTHONSERVICE_EXE):
+          # This ensures that pythonservice.exe is registered...
+          os.system('"%s" -register' % PYTHONSERVICE_EXE)
+      win32serviceutil.HandleCommandLine(InstanceService)


Property changes on: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/__init__.py
===================================================================
--- z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/__init__.py	                        (rev 0)
+++ z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/__init__.py	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1 @@
+# make a package


Property changes on: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/__init__.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/service.py
===================================================================
--- z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/service.py	                        (rev 0)
+++ z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/service.py	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,116 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Z3c development recipes
+
+$Id:$
+"""
+
+import os
+import shutil
+import zc.buildout
+
+
+def installScript(srcFile, script, replacements):
+    generated = []
+    
+    # generate the winservice script
+    text = open(srcFile, "rU").read()
+    # perform replacements
+    for var, string in replacements:
+        text = text.replace(var, string)
+
+    # If the file exists, keep the old file.  This is a
+    # hopefully temporary hack to get around distutils
+    # stripping the permissions on the server skeletin files.
+    # We reuse the original default files, which have the
+    # right permissions.
+    old = os.path.exists(script)
+    if old:
+        f = open(script, "r+")
+        f.truncate(0)
+    else:
+        f = open(script, "w")
+
+    f.write(text)
+    f.close()
+
+    if not old:
+        shutil.copymode(srcFile, script)
+        shutil.copystat(srcFile, script)
+    generated.append(script)
+
+    return generated
+
+
+class ServiceSetup:
+
+    def __init__(self, buildout, name, options):
+        self.buildout = buildout
+        self.name = name
+        self.options = options
+
+        # setup executable
+        self.executable = self.buildout['buildout']['executable']
+
+        # setup start script
+        binDir = self.buildout['buildout']['bin-directory']
+        runzope = options.get('runzope')
+
+        # fix script name
+        if not runzope:
+            raise zc.buildout.UserError(
+                'Missing runzope option in winservice recipe.')
+        if not runzope.endswith('-script.py'):
+            if runzope.endswith('.py'):
+                runzope = runzope[:3]
+            runzope = '%s-script.py' % runzope
+
+        self.runScript = os.path.join(binDir, runzope)
+
+        # setup service name
+        defaultName = 'Zope3 %s' % self.name
+        self.winServiceVars = [
+            ("<<PYTHON>>", self.executable),
+            ("<<RUNZOPE>>", self.runScript),
+            ("<<SERVICE_DISPLAY_NAME>>", options.get('name', defaultName)),
+            ("<<SERVICE_DESCRIPTION>>", options.get('description',
+                defaultName)),
+            ]
+        self.runZopeVars = []
+
+    def install(self):
+        options = self.options
+        generated = []
+
+        # raise exeption if the app script is not here now
+        if not os.path.exists(self.runScript):
+            raise zc.buildout.UserError(
+                'App start script %s does not exist in bin folder.'  % 
+                    self.runScript)
+
+        # get templates
+        binDir = self.buildout['buildout']['bin-directory']
+        winServiceTemplate = os.path.join(os.path.dirname(__file__),
+            'winservice.in')
+
+        # setup winservice file paths
+        script = os.path.join(binDir, 'winservice.py')
+
+        generated += installScript(winServiceTemplate, script,
+            self.winServiceVars)
+
+        # return list of generated files
+        return generated
+
+    update = install


Property changes on: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/service.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/tests.py
===================================================================
--- z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/tests.py	                        (rev 0)
+++ z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/tests.py	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,73 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+import re
+import unittest
+from zope.testing import doctest
+from zope.testing import renormalizing
+
+import zc.buildout.testing
+
+
+def test_start_error():
+    """The start script will setup a egg based start hook for a Zope 3 setup.
+    Let's create a buildout that installs it as an ordinary script:
+
+    >>> write('buildout.cfg',
+    ... '''
+    ... [winservice]
+    ... recipe = z3c.recipe.winservice:service
+    ... eggs = z3c.recipe.winservice
+    ... ''')
+
+    >>> print system(join('bin', 'buildout')),
+
+    """
+
+
+def setUp(test):
+    zc.buildout.testing.buildoutSetUp(test)
+    zc.buildout.testing.install_develop('z3c.recipe.winservice', test)
+    zc.buildout.testing.install('zope.testing', test)
+    zc.buildout.testing.install('zc.recipe.egg', test)
+    zc.buildout.testing.install('ZConfig', test)
+
+
+checker = renormalizing.RENormalizing([
+    zc.buildout.testing.normalize_path,
+    (re.compile(
+    "Couldn't find index page for '[a-zA-Z0-9.]+' "
+    "\(maybe misspelled\?\)"
+    "\n"
+    ), ''),
+    (re.compile("""['"][^\n"']+z3c.recipe.dev[^\n"']*['"],"""),
+     "'/z3c.recipe.dev',"),
+    (re.compile('#![^\n]+\n'), ''),
+    (re.compile('-\S+-py\d[.]\d(-\S+)?.egg'),
+     '-pyN.N.egg',
+    ),
+    ])
+
+
+def test_suite():
+    return unittest.TestSuite(
+        doctest.DocFileSuite('README.txt',
+            setUp=setUp, tearDown=zc.buildout.testing.buildoutTearDown,
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+            checker=checker),
+        )
+
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/tests.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/winservice.in
===================================================================
--- z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/winservice.in	                        (rev 0)
+++ z3c.recipe.winservice/trunk/src/z3c/recipe/winservice/winservice.in	2008-04-12 18:00:57 UTC (rev 85277)
@@ -0,0 +1,315 @@
+##############################################################################
+#
+# Copyright (c) 2008 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""A Zope Windows NT service frontend.
+
+Usage:
+
+  Installation
+
+    The Zope service should be installed by the Zope Windows
+    installer. You can manually install, uninstall the service from
+    the commandline.
+
+      zopeservice.py [options] install|update|remove|start [...]
+           |stop|restart [...]|debug [...]
+
+    Options for 'install' and 'update' commands only:
+
+     --username domain\username : The Username the service is to run
+                                  under
+
+     --password password : The password for the username
+
+     --startup [manual|auto|disabled] : How the service starts,
+                                        default = manual
+
+    Commands
+
+      install : Installs the service
+
+      update : Updates the service, use this when you change
+               the service class implementation 
+
+      remove : Removes the service
+
+      start : Starts the service, this can also be done from the
+              services control panel
+
+      stop : Stops the service, this can also be done from the
+             services control panel
+
+      restart : Restarts the service
+
+      debug : Runs the service in debug mode
+
+    You can view the usage options by running ntservice.py without any
+    arguments.
+
+    Note: you may have to register the Python service program first,
+
+      win32\PythonService.exe /register
+
+  Starting Zope
+
+    Start Zope by clicking the 'start' button in the services control
+    panel. You can set Zope to automatically start at boot time by
+    choosing 'Auto' startup by clicking the 'statup' button.
+
+  Stopping Zope
+
+    Stop Zope by clicking the 'stop' button in the services control
+    panel. You can also stop Zope through the web by going to the
+    Zope control panel and by clicking 'Shutdown'.
+
+  Event logging
+
+    Zope events are logged to the NT application event log. Use the
+    event viewer to keep track of Zope events.
+
+"""
+
+import sys, os, time
+import pywintypes
+import win32serviceutil
+import win32service
+import win32event
+import win32process
+
+
+# the max seconds we're allowed to spend backing off
+BACKOFF_MAX = 300
+# if the process runs successfully for more than BACKOFF_CLEAR_TIME
+# seconds, we reset the backoff stats to their initial values
+BACKOFF_CLEAR_TIME = 30
+# the initial backoff interval (the amount of time we wait to restart
+# a dead process)
+BACKOFF_INITIAL_INTERVAL = 5
+
+
+class NullOutput:
+    """A stdout / stderr replacement that discards everything."""
+
+    def noop(self, *args, **kw):
+        pass
+
+    write = writelines = close = seek = flush = truncate = noop
+
+    def __iter__(self):
+        return self
+
+    def next(self):
+        raise StopIteration
+
+    def isatty(self):
+        return False
+
+    def tell(self):
+        return 0
+
+    def read(self, *args, **kw):
+        return ''
+
+    readline = read
+
+    def readlines(self, *args, **kw):
+        return []
+
+
+class Service(win32serviceutil.ServiceFramework):
+    """ A class representing a Windows NT service that can manage an
+    instance-home-based Zope/ZEO/ZRS processes """
+
+    # The PythonService model requires that an actual on-disk class declaration
+    # represent a single service.  Thus, the below definition of start_cmd,
+    # must be overridden in a subclass in a file within the instance home for
+    # each instance.  The below-defined start_cmd (and _svc_display_name_
+    # and _svc_name_) are just examples.
+
+    _svc_name_ = r'Zope-Instance'
+    _svc_display_name_ = r'Zope instance at C:\Zope-Instance'
+
+    start_cmd = (
+        r'"C:\Program Files\Zope-2.7.0-a1\bin\python.exe" '
+        r'"C:\Program Files\Zope-2.7.0-a1\lib\python\Zope\Startup\run.py" '
+        r'-C "C:\Zope-Instance\etc\zope.conf"'
+        )
+
+    def __init__(self, args):
+        win32serviceutil.ServiceFramework.__init__(self, args)
+        # Create an event which we will use to wait on.
+        # The "service stop" request will set this event.
+        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
+        self.redirectOutput()
+
+    def redirectOutput(self):
+        sys.stdout.close()
+        sys.stderr.close()
+        sys.stdout = NullOutput()
+        sys.stderr = NullOutput()
+
+    def SvcStop(self):
+        # Before we do anything, tell the SCM we are starting the stop process.
+        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
+
+        # TODO:  This TerminateProcess call doesn't make much sense:  it's
+        # doing a hard kill _first_, never giving the process a chance to
+        # shut down cleanly.  Compare to current Zope2 service code, which
+        # uses Windows events to give the process a chance to shut down
+        # cleanly, doing a hard kill only if that doesn't succeed.
+
+        # stop the process if necessary
+        try:
+            win32process.TerminateProcess(self.hZope, 0)
+        except pywintypes.error:
+            # the process may already have been terminated
+            pass
+        # And set my event.
+        win32event.SetEvent(self.hWaitStop)
+
+    # SvcStop only gets triggered when the user explictly stops (or restarts)
+    # the service.  To shut the service down cleanly when Windows is shutting
+    # down, we also need to hook SvcShutdown.
+    SvcShutdown = SvcStop
+
+    def createProcess(self, cmd):
+        return win32process.CreateProcess(
+            None, cmd, None, None, 0, 0, None, None,
+            win32process.STARTUPINFO())
+
+    def SvcDoRun(self):
+        # indicate to Zope that the process is daemon managed (restartable)
+        os.environ['ZMANAGED'] = '1'
+
+        # daemon behavior:  we want to to restart the process if it
+        # dies, but if it dies too many times, we need to give up.
+
+        # we use a simple backoff algorithm to determine whether
+        # we should try to restart a dead process:  for each
+        # time the process dies unexpectedly, we wait some number of
+        # seconds to restart it, as determined by the backoff interval,
+        # which doubles each time the process dies.  if we exceed
+        # BACKOFF_MAX seconds in cumulative backoff time, we give up.
+        # at any time if we successfully run the process for more thab
+        # BACKOFF_CLEAR_TIME seconds, the backoff stats are reset.
+
+        # the initial number of seconds between process start attempts
+        backoff_interval = BACKOFF_INITIAL_INTERVAL
+        # the cumulative backoff seconds counter
+        backoff_cumulative = 0
+
+        import servicemanager
+
+        # log a service started message
+        servicemanager.LogMsg(
+            servicemanager.EVENTLOG_INFORMATION_TYPE,
+            servicemanager.PYS_SERVICE_STARTED,
+            (self._svc_name_, ' (%s)' % self._svc_display_name_))
+
+        while 1:
+            start_time = time.time()
+            info = self.createProcess(self.start_cmd)
+            self.hZope = info[0] # the pid
+            if backoff_interval > BACKOFF_INITIAL_INTERVAL:
+                # if we're in a backoff state, log a message about
+                # starting a new process
+                servicemanager.LogInfoMsg(
+                    '%s (%s): recovering from died process, new process '
+                    'started' % (self._svc_name_, self._svc_display_name_)
+                    )
+            rc = win32event.WaitForMultipleObjects(
+                (self.hWaitStop, self.hZope), 0, win32event.INFINITE)
+            if rc == win32event.WAIT_OBJECT_0:
+                # user sent a stop service request
+                self.SvcStop()
+                break
+            else:
+                # user did not send a service stop request, but
+                # the process died; this may be an error condition
+                status = win32process.GetExitCodeProcess(self.hZope)
+                if status == 0:
+                    # the user shut the process down from the web
+                    # interface (or it otherwise exited cleanly)
+                    break
+                else:
+                    # this was an abormal shutdown.  if we can, we want to
+                    # restart the process but if it seems hopeless,
+                    # don't restart an infinite number of times.
+                    if backoff_cumulative > BACKOFF_MAX:
+                        # it's hopeless
+                        servicemanager.LogErrorMsg(
+                          '%s (%s): process could not be restarted due to max '
+                          'restart attempts exceeded' % (
+                            self._svc_display_name_, self._svc_name_
+                          ))
+                        self.SvcStop()
+                        break
+                    servicemanager.LogWarningMsg(
+                       '%s (%s): process died unexpectedly.  Will attempt '
+                       'restart after %s seconds.' % (
+                            self._svc_name_, self._svc_display_name_,
+                            backoff_interval
+                            )
+                       )
+                    # if BACKOFF_CLEAR_TIME seconds have elapsed since we last
+                    # started the process, reset the backoff interval
+                    # and the cumulative backoff time to their original
+                    # states
+                    if time.time() - start_time > BACKOFF_CLEAR_TIME:
+                        backoff_interval = BACKOFF_INITIAL_INTERVAL
+                        backoff_cumulative = 0
+                    # we sleep for the backoff interval.  since this is async
+                    # code, it would be better done by sending and
+                    # catching a timed event (a service
+                    # stop request will need to wait for us to stop sleeping),
+                    # but this works well enough for me.
+                    time.sleep(backoff_interval)
+                    # update backoff_cumulative with the time we spent
+                    # backing off.
+                    backoff_cumulative = backoff_cumulative + backoff_interval
+                    # bump the backoff interval up by 2* the last interval
+                    backoff_interval = backoff_interval * 2
+
+                    # loop and try to restart the process
+
+        # log a service stopped message
+        servicemanager.LogMsg(
+            servicemanager.EVENTLOG_INFORMATION_TYPE,
+            servicemanager.PYS_SERVICE_STOPPED,
+            (self._svc_name_, ' (%s) ' % self._svc_display_name_))
+
+# these are replacements from winservice recipe
+PYTHON = r'<<PYTHON>>'
+PYTHONDIR = os.path.split(PYTHON)[0]
+PYTHONW = os.path.join(PYTHONDIR, 'pythonw.exe')
+PYTHONSERVICE_EXE = r'%s\Lib\site-packages\win32\pythonservice.exe' % PYTHONDIR
+RUNZOPE = r'<<RUNZOPE>>'
+SERVICE_NAME = str(hash(SERVICE_DISPLAY_NAME))
+SERVICE_DISPLAY_NAME = '<<SERVICE_DISPLAY_NAME>>'
+SERVICE_DESCRIPTION =  '<<SERVICE_DESCRIPTION>>'
+
+
+class InstanceService(Service):
+    start_cmd = '"%s" "%s"' % (PYTHONW, RUNZOPE)
+    _svc_name_ = SERVICE_NAME
+    _svc_display_name_ = SERVICE_DISPLAY_NAME
+    _svc_description_ = SERVICE_DESCRIPTION
+    _exe_name_ = PYTHONSERVICE_EXE
+
+
+if __name__ == '__main__':
+    import win32serviceutil
+    if os.path.exists(PYTHONSERVICE_EXE):
+        # This ensures that pythonservice.exe is registered...
+        os.system('"%s" -register' % PYTHONSERVICE_EXE)
+    win32serviceutil.HandleCommandLine(InstanceService)



More information about the Checkins mailing list