1
2
3 """
4 This file is part of the web2py Web Framework
5 Developed by Massimo Di Pierro <mdipierro@cs.depaul.edu> and
6 Limodou <limodou@gmail.com>.
7 License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
8
9 This makes uses of the pywin32 package
10 (http://sourceforge.net/projects/pywin32/).
11 You do not need to install this package to use web2py.
12
13
14 """
15
16 import time
17 import os
18 import sys
19 import traceback
20 try:
21 import win32serviceutil
22 import win32service
23 import win32event
24 except:
25 if os.name == 'nt':
26 print "Warning, winservice is unable to install the Mark Hammond Win32 extensions"
27 import servicemanager
28 import _winreg
29 from fileutils import up
30
31
32 __all__ = ['web2py_windows_service_handler']
33
34
35 -class Service(win32serviceutil.ServiceFramework):
36
37 _svc_name_ = '_unNamed'
38 _svc_display_name_ = '_Service Template'
39
41 win32serviceutil.ServiceFramework.__init__(self, *args)
42 self.stop_event = win32event.CreateEvent(None, 0, 0, None)
43
45 servicemanager.LogInfoMsg(str(msg))
46
48 self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
49 try:
50 self.ReportServiceStatus(win32service.SERVICE_RUNNING)
51 self.start()
52 win32event.WaitForSingleObject(self.stop_event,
53 win32event.INFINITE)
54 except:
55 self.log(traceback.format_exc(sys.exc_info))
56 self.SvcStop()
57 self.ReportServiceStatus(win32service.SERVICE_STOPPED)
58
60 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
61 try:
62 self.stop()
63 except:
64 self.log(traceback.format_exc(sys.exc_info))
65 win32event.SetEvent(self.stop_event)
66 self.ReportServiceStatus(win32service.SERVICE_STOPPED)
67
68
69
72
73
74
77
78
80
81 _svc_name_ = 'web2py'
82 _svc_display_name_ = 'web2py Service'
83 _exe_args_ = 'options'
84 server = None
85
87 try:
88 h = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
89 r'SYSTEM\CurrentControlSet\Services\%s'
90 % self._svc_name_)
91 try:
92 cls = _winreg.QueryValue(h, 'PythonClass')
93 finally:
94 _winreg.CloseKey(h)
95 dir = os.path.dirname(cls)
96 os.chdir(dir)
97 from gluon.settings import global_settings
98 global_settings.gluon_parent = dir
99 return True
100 except:
101 self.log("Can't change to web2py working path; server is stopped")
102 return False
103
105 self.log('web2py server starting')
106 if not self.chdir():
107 return
108 if len(sys.argv) == 2:
109 opt_mod = sys.argv[1]
110 else:
111 opt_mod = self._exe_args_
112 options = __import__(opt_mod, [], [], '')
113 if True:
114 if hasattr(options, 'numthreads') and not hasattr(options, 'minthreads'):
115 options.minthreads = options.numthreads
116 if not hasattr(options, 'minthreads'):
117 options.minthreads = None
118 if not hasattr(options, 'maxthreads'):
119 options.maxthreads = None
120 import main
121 self.server = main.HttpServer(
122 ip=options.ip,
123 port=options.port,
124 password=options.password,
125 pid_filename=options.pid_filename,
126 log_filename=options.log_filename,
127 profiler_filename=options.profiler_filename,
128 ssl_certificate=options.ssl_certificate,
129 ssl_private_key=options.ssl_private_key,
130 min_threads=options.minthreads,
131 max_threads=options.maxthreads,
132 server_name=options.server_name,
133 request_queue_size=options.request_queue_size,
134 timeout=options.timeout,
135 shutdown_timeout=options.shutdown_timeout,
136 path=options.folder
137 )
138 try:
139 from rewrite import load
140 load()
141 self.server.start()
142 except:
143
144
145
146 self.server = None
147 raise
148
156
157
195
197 path = os.path.dirname(__file__)
198 web2py_path = up(path)
199 if web2py_path.endswith('.zip'):
200 web2py_path = os.path.dirname(web2py_path)
201 os.chdir(web2py_path)
202 classstring = os.path.normpath(
203 os.path.join(web2py_path, 'gluon.winservice.'+cls.__name__))
204 if opt_file:
205 cls._exe_args_ = opt_file
206 win32serviceutil.HandleCommandLine(
207 cls, serviceClassString=classstring, argv=['', 'install'])
208 win32serviceutil.HandleCommandLine(
209 cls, serviceClassString=classstring, argv=argv)
210
211 if __name__ == '__main__':
212 register_service_handler(cls=Web2pyService)
213 register_service_handler(cls=Web2pyCronService)
214