etwist/service.py
author Alexandre Fayolle <alexandre.fayolle@logilab.fr>
Tue, 27 Apr 2010 10:32:25 +0000
branchstable
changeset 5414 6eeec78c65f9
parent 5365 ca838c79af97
child 5421 8167de96c523
permissions -rw-r--r--
enforce use of environment variables when running as a service CW_INSTANCES_DIR, CW_INSTANCES_DATA_DIR, CW_RUNTIME_DIR must be positionned at the system level. This will ensure that cubicweb-ctl commands issued on the computer will use the proper directories.
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
5414
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
     2
import sys
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
     3
5414
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
     4
try:
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
     5
    import win32serviceutil
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
     6
    import win32service
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
     7
except ImportError:
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
     8
    print 'Win32 extensions for Python are likely not installed.'
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
     9
    sys.exit(3)
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    10
4064
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 cubicweb.etwist.server import (CubicWebRootResource, reactor, server,
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    13
                                    parsePOSTData, channel)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    14
5414
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    15
import logging
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    16
from logging import getLogger, handlers
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    17
from cubicweb import set_log_methods
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    18
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    19
5414
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    20
def _check_env(env):
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    21
    env_vars = ('CW_INSTANCES_DIR', 'CW_INSTANCES_DATA_DIR', 'CW_RUNTIME_DIR')
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    22
    for var in env_vars:
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    23
        if var not in env:
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    24
            raise Exception('The environment variables %s must be set.' % \
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    25
                            ', '.join(env_vars))            
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    26
    if not env.get('USERNAME'):
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    27
        env['USERNAME'] = 'cubicweb'
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    28
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    29
class CWService(object, win32serviceutil.ServiceFramework):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    30
    _svc_name_ = None
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    31
    _svc_display_name_ = None
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    32
    instance = None
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    33
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    34
    def __init__(self, *args, **kwargs):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    35
        win32serviceutil.ServiceFramework.__init__(self, *args, **kwargs)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    36
        cwcfg.load_cwctl_plugins()
5414
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    37
        logger = getLogger('cubicweb')
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    38
        set_log_methods(CubicWebRootResource, logger)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    39
        server.parsePOSTData = parsePOSTData
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 SvcStop(self):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    42
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
5414
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    43
        logger = getLogger('cubicweb.twisted')
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    44
        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
    45
        reactor.stop()
891cdb7d8cbb handle Service Close request correctly (closes #736039)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 4719
diff changeset
    46
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    47
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    48
    def SvcDoRun(self):
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    49
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    50
        logger = getLogger('cubicweb.twisted')
5414
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    51
        handler = handlers.NTEventLogHandler('cubicweb')
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    52
        handler.setLevel(logging.INFO)
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    53
        logger.addHandler(handler)
4122
3c3d2a102d4c fix NameError
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4064
diff changeset
    54
        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
    55
        try:
5414
6eeec78c65f9 enforce use of environment variables when running as a service
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5365
diff changeset
    56
            _check_env(os.environ)
4064
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    57
            # create the site
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    58
            config = cwcfg.config_for(self.instance)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    59
            root_resource = CubicWebRootResource(config, False)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    60
            website = server.Site(root_resource)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    61
            # 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
    62
            port = config['port'] or 8080
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    63
            logger.info('listening on port %s' % port)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    64
            reactor.listenTCP(port, channel.HTTPFactory(website))
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    65
            root_resource.init_publisher()
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    66
            root_resource.start_service()
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    67
            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
    68
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    69
            reactor.run()
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    70
        except Exception, e:
3b12e4192983 be able to run cw instances as windows services
auc@giediprime.logilab.fr
parents:
diff changeset
    71
            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
    72
            logger.exception('what happened ...')
5088
891cdb7d8cbb handle Service Close request correctly (closes #736039)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 4719
diff changeset
    73
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)