etwist/service.py
author Alexandre Fayolle <alexandre.fayolle@logilab.fr>
Sun, 11 Apr 2010 11:59:45 +0000
branchstable
changeset 5211 8fac9cd3c510
parent 5088 891cdb7d8cbb
child 5365 ca838c79af97
permissions -rw-r--r--
merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
class CWService(object, win32serviceutil.ServiceFramework):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    21
    _svc_name_ = None
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    22
    _svc_display_name_ = None
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    23
    instance = None
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    24
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    25
    def __init__(self, *args, **kwargs):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    26
        win32serviceutil.ServiceFramework.__init__(self, *args, **kwargs)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    27
        cwcfg.load_cwctl_plugins()
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    28
        set_log_methods(CubicWebRootResource, logger)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    29
        server.parsePOSTData = parsePOSTData
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    30
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    31
    def SvcStop(self):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    32
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    33
        logger.info('stopping %s service' % self.instance)
5088
891cdb7d8cbb handle Service Close request correctly (closes #736039)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 4719
diff changeset
    34
        reactor.stop()
891cdb7d8cbb handle Service Close request correctly (closes #736039)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 4719
diff changeset
    35
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    36
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    37
    def SvcDoRun(self):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    38
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    39
        logger = getLogger('cubicweb.twisted')
4122
3c3d2a102d4c fix NameError
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4064
diff changeset
    40
        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
    41
        try:
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    42
            # create the site
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    43
            config = cwcfg.config_for(self.instance)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    44
            root_resource = CubicWebRootResource(config, False)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    45
            website = server.Site(root_resource)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    46
            # 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
    47
            port = config['port'] or 8080
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    48
            logger.info('listening on port %s' % port)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    49
            reactor.listenTCP(port, channel.HTTPFactory(website))
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    50
            root_resource.init_publisher()
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    51
            root_resource.start_service()
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    52
            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
    53
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    54
            reactor.run()
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    55
        except Exception, e:
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    56
            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
    57
            logger.exception('what happened ...')
5088
891cdb7d8cbb handle Service Close request correctly (closes #736039)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 4719
diff changeset
    58
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)