cubicweb/etwist/service.py
changeset 12530 9d88e1177c35
parent 12529 7276f1c89ddd
child 12531 2b9e815d20dc
equal deleted inserted replaced
12529:7276f1c89ddd 12530:9d88e1177c35
     1 # copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 from __future__ import print_function
       
    19 
       
    20 import os
       
    21 import sys
       
    22 
       
    23 try:
       
    24     import win32serviceutil
       
    25     import win32service
       
    26 except ImportError:
       
    27     print('Win32 extensions for Python are likely not installed.')
       
    28     sys.exit(3)
       
    29 
       
    30 from os.path import join
       
    31 
       
    32 from cubicweb.etwist.server import (CubicWebRootResource, reactor, server)
       
    33 
       
    34 from logilab.common.shellutils import rm
       
    35 
       
    36 import logging
       
    37 from logging import getLogger, handlers
       
    38 from cubicweb import set_log_methods
       
    39 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
       
    40 
       
    41 
       
    42 def _check_env(env):
       
    43     env_vars = ('CW_INSTANCES_DIR', 'CW_INSTANCES_DATA_DIR', 'CW_RUNTIME_DIR')
       
    44     for var in env_vars:
       
    45         if var not in env:
       
    46             raise Exception('The environment variables %s must be set.' %
       
    47                             ', '.join(env_vars))
       
    48     if not env.get('USERNAME'):
       
    49         env['USERNAME'] = 'cubicweb'
       
    50 
       
    51 
       
    52 class CWService(object, win32serviceutil.ServiceFramework):
       
    53     _svc_name_ = None
       
    54     _svc_display_name_ = None
       
    55     instance = None
       
    56 
       
    57     def __init__(self, *args, **kwargs):
       
    58         win32serviceutil.ServiceFramework.__init__(self, *args, **kwargs)
       
    59         cwcfg.load_cwctl_plugins()
       
    60         logger = getLogger('cubicweb')
       
    61         set_log_methods(CubicWebRootResource, logger)
       
    62 
       
    63     def SvcStop(self):
       
    64         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
       
    65         logger = getLogger('cubicweb.twisted')
       
    66         logger.info('stopping %s service' % self.instance)
       
    67         reactor.stop()
       
    68         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
       
    69 
       
    70     def SvcDoRun(self):
       
    71         self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
       
    72         logger = getLogger('cubicweb.twisted')
       
    73         handler = handlers.NTEventLogHandler('cubicweb')
       
    74         handler.setLevel(logging.INFO)
       
    75         logger.addHandler(handler)
       
    76         logger.info('starting %s service' % self.instance)
       
    77         try:
       
    78             _check_env(os.environ)
       
    79             # create the site
       
    80             config = cwcfg.config_for(self.instance)
       
    81             config.init_log(force=True)
       
    82             config.debugmode = False
       
    83             logger.info('starting cubicweb instance %s ', self.instance)
       
    84             config.info('clear ui caches')
       
    85             rm(join(config.appdatahome, 'uicache', '*'))
       
    86             root_resource = CubicWebRootResource(config, config.repository())
       
    87             website = server.Site(root_resource)
       
    88             # serve it via standard HTTP on port set in the configuration
       
    89             port = config['port'] or 8080
       
    90             logger.info('listening on port %s' % port)
       
    91             reactor.listenTCP(port, website)
       
    92             root_resource.init_publisher()
       
    93             root_resource.start_service()
       
    94             logger.info('instance started on %s', root_resource.base_url)
       
    95             self.ReportServiceStatus(win32service.SERVICE_RUNNING)
       
    96             reactor.run()
       
    97         except Exception as e:
       
    98             logger.error('service %s stopped (cause: %s)' % (self.instance, e))
       
    99             logger.exception('what happened ...')
       
   100         self.ReportServiceStatus(win32service.SERVICE_STOPPED)