etwist/service.py
author Samuel Trégouët <samuel.tregouet@logilab.fr>
Fri, 11 Sep 2015 14:52:09 +0200
changeset 10589 7c23b7de2b8d
parent 10181 215403b9798f
permissions -rw-r--r--
[py3k] print function
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10181
diff changeset
    18
from __future__ import print_function
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10181
diff changeset
    19
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    20
import os
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    21
import sys
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    22
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    23
try:
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    24
    import win32serviceutil
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    25
    import win32service
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    26
except ImportError:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10181
diff changeset
    27
    print('Win32 extensions for Python are likely not installed.')
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    28
    sys.exit(3)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    29
8200
182d47d87a5c [windows service] Copied the 3 lines from etwist/twctl into etwist/service in order to also clean the cache in Windows service mode.
patwat
parents: 6269
diff changeset
    30
from os.path import join
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    31
5495
ce836b665a65 attempt to port windows service to 3.8.1
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
    32
from cubicweb.etwist.server import (CubicWebRootResource, reactor, server)
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    33
8200
182d47d87a5c [windows service] Copied the 3 lines from etwist/twctl into etwist/service in order to also clean the cache in Windows service mode.
patwat
parents: 6269
diff changeset
    34
from logilab.common.shellutils import rm
182d47d87a5c [windows service] Copied the 3 lines from etwist/twctl into etwist/service in order to also clean the cache in Windows service mode.
patwat
parents: 6269
diff changeset
    35
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    36
import logging
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    37
from logging import getLogger, handlers
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    38
from cubicweb import set_log_methods
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    39
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    40
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    41
def _check_env(env):
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    42
    env_vars = ('CW_INSTANCES_DIR', 'CW_INSTANCES_DATA_DIR', 'CW_RUNTIME_DIR')
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    43
    for var in env_vars:
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    44
        if var not in env:
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    45
            raise Exception('The environment variables %s must be set.' % \
5495
ce836b665a65 attempt to port windows service to 3.8.1
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
    46
                            ', '.join(env_vars))
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    47
    if not env.get('USERNAME'):
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    48
        env['USERNAME'] = 'cubicweb'
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    49
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    50
class CWService(object, win32serviceutil.ServiceFramework):
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    51
    _svc_name_ = None
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    52
    _svc_display_name_ = None
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    53
    instance = None
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    54
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    55
    def __init__(self, *args, **kwargs):
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    56
        win32serviceutil.ServiceFramework.__init__(self, *args, **kwargs)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    57
        cwcfg.load_cwctl_plugins()
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    58
        logger = getLogger('cubicweb')
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    59
        set_log_methods(CubicWebRootResource, logger)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    60
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    61
    def SvcStop(self):
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    62
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    63
        logger = getLogger('cubicweb.twisted')
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    64
        logger.info('stopping %s service' % self.instance)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    65
        reactor.stop()
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    66
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    67
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    68
    def SvcDoRun(self):
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    69
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    70
        logger = getLogger('cubicweb.twisted')
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    71
        handler = handlers.NTEventLogHandler('cubicweb')
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    72
        handler.setLevel(logging.INFO)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    73
        logger.addHandler(handler)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    74
        logger.info('starting %s service' % self.instance)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    75
        try:
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    76
            _check_env(os.environ)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    77
            # create the site
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    78
            config = cwcfg.config_for(self.instance)
5600
a826996875e7 [win32 service] activate logging to the configured file as soon as possible
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5495
diff changeset
    79
            config.init_log(force=True)
6269
2220fa621455 ewrongattribute
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6268
diff changeset
    80
            config.debugmode = False
5600
a826996875e7 [win32 service] activate logging to the configured file as soon as possible
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5495
diff changeset
    81
            logger.info('starting cubicweb instance %s ', self.instance)
8200
182d47d87a5c [windows service] Copied the 3 lines from etwist/twctl into etwist/service in order to also clean the cache in Windows service mode.
patwat
parents: 6269
diff changeset
    82
            config.info('clear ui caches')
182d47d87a5c [windows service] Copied the 3 lines from etwist/twctl into etwist/service in order to also clean the cache in Windows service mode.
patwat
parents: 6269
diff changeset
    83
            for cachedir in ('uicache', 'uicachehttps'):
182d47d87a5c [windows service] Copied the 3 lines from etwist/twctl into etwist/service in order to also clean the cache in Windows service mode.
patwat
parents: 6269
diff changeset
    84
                rm(join(config.appdatahome, cachedir, '*'))
10181
215403b9798f [etwist/service] adjust for a signature change
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8695
diff changeset
    85
            root_resource = CubicWebRootResource(config, config.repository())
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    86
            website = server.Site(root_resource)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    87
            # serve it via standard HTTP on port set in the configuration
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    88
            port = config['port'] or 8080
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    89
            logger.info('listening on port %s' % port)
5495
ce836b665a65 attempt to port windows service to 3.8.1
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5424
diff changeset
    90
            reactor.listenTCP(port, website)
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    91
            root_resource.init_publisher()
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    92
            root_resource.start_service()
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    93
            logger.info('instance started on %s', root_resource.base_url)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    94
            self.ReportServiceStatus(win32service.SERVICE_RUNNING)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    95
            reactor.run()
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8200
diff changeset
    96
        except Exception as e:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    97
            logger.error('service %s stopped (cause: %s)' % (self.instance, e))
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    98
            logger.exception('what happened ...')
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5414
diff changeset
    99
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)