ednaNTSvc.py :  » Web-Server » edna » edna-0.6 » ntsvc » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Server » edna 
edna » edna 0.6 » ntsvc » ednaNTSvc.py
"""
This is an example of wrapping an arbitary SocketServer.TCPServer derived
server into an NT service with out too much pain and suffering.
"""

# TCPServerService is the generic NT service <-> TCPServer interaction code
import TCPServerService

# Mark's cool helper module...
import win32serviceutil
import win32service
import win32event
import SocketServer
import BaseHTTPServer

# Edna is the TCPServer we will turn into an NT Service
try:
  import edna
except ImportError:
  import sys
  try:
    # in some cases, the above failure leaves a bad module around. torch it.
    del sys.modules['edna']
  except KeyError:
    pass
  sys.path.append('..')
  import edna

class EdnaSvc(TCPServerService.TCPServerService,
              # This following line is optional...
              # You can let edna pick up the normal threading mixin if
              # you want your service to end immediately, and abort
              # any pending requests, instead of making the service
              # hang around until all pending requests are finished.
              TCPServerService.SvcTrackingThreadingMixin,
              edna.Server):
  # Here belong some special attributes pertaining to the installation
  # of this service.
  # _svc_name_, and _svc_display_name_ are required!

  # _svc_name_ is what you use when you want to start/stop the service.
  # e.g.: "net start edna", "net stop edna"
  # This is also the name of the registry key, where your configuration
  # settings are stored.
  # i.e. HKLM\SYSTEM\CurrentControlSet\Services\Edna
  _svc_name_ = "Edna"
  # _svc_display_name_ is the nice name the control panel will display
  # for your service
  _svc_display_name_ = "Edna MP3 Streaming Service"
  # The following are optional
  # _svc_deps_: This should be set to the list of service names
  #             That need to be started before this one.
  # _exe_name_: This should be set to a service .EXE if you're not
  #             going to use PythonService.exe
  
  def __init__(self, args):
    fname = win32serviceutil.GetServiceCustomOption(self, "ConfigurationFile")
    edna.Server.__init__(self, fname)
    TCPServerService.TCPServerService.__init__(self, args)

  def waitForPendingRequests(self):
    """
    Wait for any pending requests to finish/close...

    This only does anything useful if a mixin similar to
    TCPServerService.SvcTrackingThreadingMixin is in the class definition.
    """
    while self.thread_handles:
      # The 5000 says, SCM, I think I'm going to finish shutting down in 5s.
      self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING, 5000)
      #print "Waiting for %d threads to finish..." % (len(self.thread_handles))
      # Actually wait for 3s just to be paranoid.
      rc = win32event.WaitForMultipleObjects(self.thread_handles, 1, 3000)
      # Keep looping until ALL threads go away...
      # You might want to have a maximum time out here to force a shutdown
      # if for example it we've gone 5+ min, and we still have some threads
      # that still haven't shutdown.
      
    
ConfigFileNotFound = "ConfigFileNotFound"

def customOptionHandler(opts):
    "This is only called when the service is installed."
    fFoundConfigFile = 0
    for opt, val in opts:
      if opt == "-c":
        # This installs the location of the Edna configuration file into:
        # HKLM\SYSTEM\CurrentControlSet\Services\Edna\Parameters\ConfigurationFile
        win32serviceutil.SetServiceCustomOption(
          EdnaSvc._svc_name_,
          "ConfigurationFile",
          val)
        fFoundConfigFile = 1
    if not fFoundConfigFile:
      print "Error: You forgot to pass in a path to your Edna configuration file., use the '-c' option."
      raise ConfigFileNotFound
    
if __name__ == "__main__":
    import sys, regsetup
    # This magic function, handles service installation/removal, etc....
    # Sample command lines:
    # Installation:
    #   python ednaNTSvc.py -c d:\edna\edna.conf --startup auto install
    # Removal:
    #   python ednaNTSvc.py remove
    # etc...
    win32serviceutil.HandleCommandLine(
      EdnaSvc,
      argv = sys.argv,
      customInstallOptions = "c:",
      customOptionHandler = customOptionHandler)    
    # Make sure these files are in the Python path...
    sys.path.append("..")
    regsetup.FindRegisterModule("ednaNTSvc", 'ednaNTSvc.py', sys.path)
    regsetup.FindRegisterModule("TCPServerService", 'TCPServerService.py', sys.path)
    regsetup.FindRegisterModule("edna", "edna.py", "..")
    regsetup.FindRegisterModule("ezt", "ezt.py", "..")
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.