author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Thu, 04 Mar 2010 17:56:45 +0100 | |
branch | stable |
changeset 5204 | d175ce5c2e85 |
parent 4719 | aaed3f813ef8 |
child 5088 | 891cdb7d8cbb |
permissions | -rw-r--r-- |
4064
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
1 |
import os |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
2 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
3 |
import win32serviceutil |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
4 |
import win32service |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
5 |
import win32event |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
6 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
7 |
from cubicweb.etwist.server import (CubicWebRootResource, reactor, server, |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
8 |
parsePOSTData, channel) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
9 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
10 |
from logging import getLogger, handlers |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
11 |
from cubicweb import set_log_methods |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
12 |
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
13 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
14 |
logger = getLogger('cubicweb.twisted') |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
15 |
logger.handlers = [handlers.NTEventLogHandler('cubicweb')] |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
16 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
17 |
os.environ['CW_INSTANCES_DIR'] = r'C:\etc\cubicweb.d' |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
18 |
os.environ['USERNAME'] = 'cubicweb' |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
19 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
20 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
21 |
class CWService(object, win32serviceutil.ServiceFramework): |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
22 |
_svc_name_ = None |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
23 |
_svc_display_name_ = None |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
24 |
instance = None |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
25 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
26 |
def __init__(self, *args, **kwargs): |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
27 |
win32serviceutil.ServiceFramework.__init__(self, *args, **kwargs) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
28 |
self._stop_event = win32event.CreateEvent(None, 0, 0, None) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
29 |
cwcfg.load_cwctl_plugins() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
30 |
set_log_methods(CubicWebRootResource, logger) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
31 |
server.parsePOSTData = parsePOSTData |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
32 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
33 |
def SvcStop(self): |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
34 |
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
35 |
logger.info('stopping %s service' % self.instance) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
36 |
win32event.SetEvent(self._stop_event) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
37 |
self.ReportServiceStatus(win32service.SERVICE_STOPPED) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
38 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
39 |
def SvcDoRun(self): |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
40 |
self.ReportServiceStatus(win32service.SERVICE_START_PENDING) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
41 |
logger = getLogger('cubicweb.twisted') |
4122
3c3d2a102d4c
fix NameError
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4064
diff
changeset
|
42 |
logger.info('starting %s service' % self.instance) |
4064
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
43 |
try: |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
44 |
# create the site |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
45 |
config = cwcfg.config_for(self.instance) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
46 |
root_resource = CubicWebRootResource(config, False) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
47 |
website = server.Site(root_resource) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
48 |
# serve it via standard HTTP on port set in the configuration |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
49 |
port = config['port'] or 8080 |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
50 |
logger.info('listening on port %s' % port) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
51 |
reactor.listenTCP(port, channel.HTTPFactory(website)) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
52 |
root_resource.init_publisher() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
53 |
root_resource.start_service() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
54 |
logger.info('instance started on %s', root_resource.base_url) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
55 |
self.ReportServiceStatus(win32service.SERVICE_RUNNING) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
56 |
reactor.run() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
57 |
except Exception, e: |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
58 |
logger.error('service %s stopped (cause: %s)' % (self.instance, e)) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
59 |
logger.exception('what happened ...') |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
60 |
self.SvcStop() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
61 |