cubicweb/etwist/service.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 24 Nov 2016 16:58:50 +0100
changeset 11870 3a84a79c4ed5
parent 11057 0b59724cb3f2
child 11913 4516c3956d46
permissions -rw-r--r--
Flake8 bits with some copyright updates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
     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
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    41
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
    42
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
    43
    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
    44
    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
    45
        if var not in env:
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    46
            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
    47
                            ', '.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
    48
    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
    49
        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
    50
11870
3a84a79c4ed5 Flake8 bits with some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    51
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
    52
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
    53
    _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
    54
    _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
    55
    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
    56
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
    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
    58
        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
    59
        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
    60
        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
    61
        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
    62
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
    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
    64
        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
    65
        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
    66
        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
    67
        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
    68
        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
    69
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
    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
    71
        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
    72
        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
    73
        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
    74
        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
    75
        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
    76
        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
    77
        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
    78
            _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
    79
            # 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
    80
            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
    81
            config.init_log(force=True)
6269
2220fa621455 ewrongattribute
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6268
diff changeset
    82
            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
    83
            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
    84
            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
    85
            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
    86
                rm(join(config.appdatahome, cachedir, '*'))
10181
215403b9798f [etwist/service] adjust for a signature change
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8695
diff changeset
    87
            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
    88
            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
    89
            # 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
    90
            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
    91
            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
    92
            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
    93
            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
    94
            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
    95
            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
    96
            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
    97
            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
    98
        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
    99
            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
   100
            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
   101
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)