author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Sat, 06 Feb 2010 10:37:09 +0100 | |
branch | stable |
changeset 4487 | 632369f43baf |
parent 4122 | 3c3d2a102d4c |
child 4719 | aaed3f813ef8 |
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 |
import os.path as osp |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
3 |
import sys |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
4 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
5 |
import win32serviceutil |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
6 |
import win32service |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
7 |
import win32event |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
8 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
9 |
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
|
10 |
parsePOSTData, channel) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
11 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
12 |
from logging import getLogger, handlers |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
13 |
from cubicweb import set_log_methods |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
14 |
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
15 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
16 |
logger = getLogger('cubicweb.twisted') |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
17 |
logger.handlers = [handlers.NTEventLogHandler('cubicweb')] |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
18 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
19 |
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
|
20 |
os.environ['USERNAME'] = 'cubicweb' |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
21 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
22 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
23 |
class CWService(object, win32serviceutil.ServiceFramework): |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
24 |
_svc_name_ = None |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
25 |
_svc_display_name_ = None |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
26 |
instance = None |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
27 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
28 |
def __init__(self, *args, **kwargs): |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
29 |
win32serviceutil.ServiceFramework.__init__(self, *args, **kwargs) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
30 |
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
|
31 |
cwcfg.load_cwctl_plugins() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
32 |
set_log_methods(CubicWebRootResource, logger) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
33 |
server.parsePOSTData = parsePOSTData |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
34 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
35 |
def SvcStop(self): |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
36 |
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
37 |
logger.info('stopping %s service' % self.instance) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
38 |
win32event.SetEvent(self._stop_event) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
39 |
self.ReportServiceStatus(win32service.SERVICE_STOPPED) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
40 |
|
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
41 |
def SvcDoRun(self): |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
42 |
self.ReportServiceStatus(win32service.SERVICE_START_PENDING) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
43 |
logger = getLogger('cubicweb.twisted') |
4122
3c3d2a102d4c
fix NameError
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4064
diff
changeset
|
44 |
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
|
45 |
try: |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
46 |
# create the site |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
47 |
config = cwcfg.config_for(self.instance) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
48 |
root_resource = CubicWebRootResource(config, False) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
49 |
website = server.Site(root_resource) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
50 |
# 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
|
51 |
port = config['port'] or 8080 |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
52 |
logger.info('listening on port %s' % port) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
53 |
reactor.listenTCP(port, channel.HTTPFactory(website)) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
54 |
root_resource.init_publisher() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
55 |
root_resource.start_service() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
56 |
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
|
57 |
self.ReportServiceStatus(win32service.SERVICE_RUNNING) |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
58 |
reactor.run() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
59 |
except Exception, e: |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
60 |
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
|
61 |
logger.exception('what happened ...') |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
62 |
self.SvcStop() |
3b12e4192983
be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff
changeset
|
63 |