-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathwinserver.py
237 lines (221 loc) · 8.04 KB
/
winserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# This file is part of LibreOsteo.
#
# LibreOsteo is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LibreOsteo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LibreOsteo. If not, see <http://www.gnu.org/licenses/>.
"""
Requires Mark Hammond's pywin32 package.
"""
# Python stdlib imports
import sys
import logging
import os, os.path
if sys.argv[0].endswith('.exe') or not sys.argv[0].endswith('.py'):
setattr(sys, 'frozen', True)
if getattr(sys, 'frozen', False):
# frozen
dir = os.path.dirname(sys.executable)
sys.path.append(dir)
os.environ['PATH'] = (os.environ['PATH'] + ";").join(p + ";"
for p in sys.path)
# Win32 service imports
import win32serviceutil
import win32service, win32api
import servicemanager
# Third-party imports
import cherrypy
import patch
import server
rootLogger = logging.getLogger(__name__)
class LibreosteoService(win32serviceutil.ServiceFramework):
"""Libreosteo NT Service."""
_svc_name_ = "LibreosteoService"
_svc_display_name_ = "Libreosteo Service"
_exe_name_ = 'LibreOsteo.exe'
def log(self, msg):
servicemanager.LogInfoMsg(str(msg))
rootLogger.info(msg)
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
try:
self.log("Create the Libreosteo server")
config = server.configure()
_srvr = server.Server(config)
# in practice, you will want to specify a value for
# log.error_file below or in your config file. If you
# use a config file, be sure to use an absolute path to
# it, as you can't be assured what path your service
# will run in.
self.log("Configure the server")
cherrypy.config.update({
'global': {
'log.screen':
False,
'engine.autoreload.on':
False,
'engine.SIGHUP':
None,
'engine.SIGTERM':
None,
'log.error_file':
os.path.join(_srvr.base_dir, 'libreosteo_error.log'),
'tools.log_tracebacks.on':
True,
'log.access_file':
os.path.join(_srvr.base_dir, 'libreosteo_access.log'),
'server.socket_port':
config["server_port"],
'server.socket_host':
'0.0.0.0',
}
})
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.log("Run the service Libreosteo")
_srvr.run()
except Exception as e:
s = str(e)
self.log('Exception : %s' % s)
self.SvcStop()
def SvcStop(self):
self.log("Stopping service")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
cherrypy.engine.exit()
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
# very important for use with py2exe
# otherwise the Service Controller never knows that it is stopped !
self.log("Service stopped")
if __name__ == '__main__':
if getattr(sys, 'frozen', False):
# frozen
DATA_FOLDER = os.path.dirname(sys.executable)
else:
# unfrozen
DATA_FOLDER = os.path.dirname(os.path.realpath(__file__))
LOG_CONF = {
'version': 1,
'formatters': {
'void': {
'format': ''
},
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': os.path.join(DATA_FOLDER, 'default.log'),
'maxBytes': 10485760,
'backupCount': 20,
'encoding': 'utf8'
},
'cherrypy_console': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': os.path.join(DATA_FOLDER, 'console.log'),
'maxBytes': 10485760,
'backupCount': 20,
'encoding': 'utf8'
},
'cherrypy_access': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': os.path.join(DATA_FOLDER, 'access.log'),
'maxBytes': 10485760,
'backupCount': 20,
'encoding': 'utf8'
},
'cherrypy_error': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'standard',
'filename': os.path.join(DATA_FOLDER, 'errors.log'),
'maxBytes': 10485760,
'backupCount': 20,
'encoding': 'utf8'
},
},
'loggers': {
'django.utils.translation': {
'handlers': ['default'],
'level': 'INFO'
},
'': {
'handlers': ['default'],
'level': 'INFO'
},
'root': {
'handlers': ['default'],
'level': 'INFO'
},
'db': {
'handlers': ['default'],
'level': 'INFO',
'propagate': True
},
'cherrypy.access': {
'handlers': ['cherrypy_access'],
'level': 'INFO',
'propagate': True
},
'cherrypy.error': {
'handlers': ['cherrypy_console', 'cherrypy_error'],
'level': 'INFO',
'propagate': True
},
'libreosteoweb.api': {
'handlers': ['cherrypy_console', 'default'],
'level': 'INFO',
'propagate': True
},
'libreosteo': {
'handlers': ['cherrypy_console', 'default'],
'level': 'INFO',
'propagate': True
},
'Libreosteo': {
'handlers': ['cherrypy_console', 'default'],
'level': 'INFO',
'propagate': True
},
}
}
logging.config.dictConfig(LOG_CONF)
os.chdir(DATA_FOLDER)
logging.info(os.getcwd())
logger = logging.getLogger(__name__)
logger.info("Frozen with attribute value %s" %
(getattr(sys, 'frozen', False)))
if len(sys.argv) == 1:
logging.info("Start service")
logging.info("Handle starting of the service")
try:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(LibreosteoService)
servicemanager.StartServiceCtrlDispatcher()
except Exception as e:
logging.exception("Exception when starting service")
else:
logging.info("Start Controller")
logging.info("Handle command line on service manager")
logging.info("Command = %s" % sys.argv)
try:
win32serviceutil.HandleCommandLine(LibreosteoService)
except Exception as e:
logging.exception("Exception when starting service")