Changeset cd3ea37 for src


Ignore:
Timestamp:
May 11, 2020, 9:02:00 PM (7 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
b5d1ed0
Parents:
30949f8
Message:

Database of mapservice_impl is now created before first request

Location:
src/gnuboat
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/gnuboat/deviceservice_impl/tests/__init__.py

    r30949f8 rcd3ea37  
    3131import subprocess
    3232import socket
     33from urllib.parse import urlparse
     34from werkzeug.utils import import_string
    3335
    3436from gnuboat.common.datahandler import (
    3537    rrdhandler, loghandler, logdataset,
     38)
     39from gnuboat.common.webservice.Config import (
     40    config_get
    3641)
    3742
    … …  
    7479    _create_maps(testcase, application)
    7580
    76     _mapservice_start(testcase)
     81    _mapservice_start(testcase, application)
    7782
    7883    return application
    … …  
    214219
    215220
    216 def _mapservice_start(testcase):
     221def _mapservice_start(testcase, application):
    217222    if 'TestMapserviceController' not in testcase.id():
    218223        testcase.mapservice_process = None
    219224    else:
     225        print('MAPSERVICE Starting')
     226        mapservice_url = config_get(application.config, 'MAPSERVICE', None)
     227        if mapservice_url is None:
     228            raise LookupError('MAPSERVICE not configured')
     229        fragments = urlparse(mapservice_url)
     230        if fragments.hostname is None:
     231            raise LookupError('MAPSERVICE host not found')
     232        if fragments.port is None:
     233            raise LookupError('MAPSERVICE port not found')
     234
     235        mapservice_config = 'Testing'
     236        mapservice_config_object = import_string(
     237            'gnuboat.mapservice_impl.Config.{}Config'.format(mapservice_config)
     238        )
     239        database_uri = config_get(mapservice_config_object,
     240                                  'SQLALCHEMY_DATABASE_URI', None)
     241        if database_uri is None:
     242            raise LookupError(
     243                'SQLALCHEMY_DATABASE_URI not in MAPSERVICE Testing config')
     244        database_uri = database_uri.replace('sqlite:///', '')
     245        if os.path.isfile(database_uri):
     246            os.remove(database_uri)
     247
    220248        mapservice_env = dict(os.environ)
    221         mapservice_env['FLASK_ENV'] = 'Testing'
     249        mapservice_env['FLASK_ENV'] = mapservice_config
     250        # the timeout is needed, because of the spatiallite table creation,
     251        # which is pretty slow
    222252        testcase.mapservice_process = subprocess.Popen(
    223253            [
    224                 'gnuboat-dev-mapservice', '--bind=localhost:8080',
     254                'gnuboat-dev-mapservice',
     255                '--bind={}:{}'.format(fragments.hostname, fragments.port),
    225256                '--log-level=debug'
    226257            ],
    … …  
    229260        )
    230261        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     262        # we need to give mapservice a chance to start before calling
     263        # connect_ex
     264        time.sleep(2)
    231265        s.settimeout(10)
    232         if s.connect_ex(('localhost', int(8090))) == 0:
     266        if s.connect_ex((fragments.hostname, int(fragments.port))) == 0:
     267            print('MAPSERVICE running at {}:{}'.format(
     268                fragments.hostname, fragments.port))
    233269            s.shutdown(socket.SHUT_RDWR)
     270        else:
     271            print('MAPSERVICE Failed to start at {}:{}'.format(
     272                fragments.hostname, fragments.port))
     273            testcase.mapservice_process.terminate()
     274            (stdout_data, stderr_data) = \
     275                testcase.mapservice_process.communicate(timeout=10)
     276            if stdout_data:
     277                stdout_data = stdout_data.decode('utf-8')
     278                print('MAPSERVICE STDOUT: {}'.format(stdout_data))
     279            if stderr_data:
     280                stderr_data = stderr_data.decode('utf-8')
     281                print('MAPSERVICE STDERR: {}'.format(stderr_data))
     282            testcase.mapservice_process = None
  • src/gnuboat/mapservice_impl/__init__.py

    r30949f8 rcd3ea37  
    5959
    6060
    61 @application.before_first_request
    62 def create_database():
    63     @event.listens_for(db.engine, "connect")
    64     def load_spatialite_extension(dbapi_conn, connection_record):
    65         load_spatialite(dbapi_conn, connection_record)
    66 
    67     if flask.current_app.config['CREATE_TEST_DATABASE'] is True:
    68         from gnuboat.mapservice_impl.demo import DbSetup as demoDbSetup
    69         demoDbSetup.setup()
    70     else:
    71         db_setup()
    72 
    73 
    7461def setup_app(app_name, config, template_folder=None):
    7562    application.json_encoder = encoder.JSONEncoder
    … …  
    9178    mail.init_app(application)
    9279    celery.config_from_object(config)
     80    with application.app_context():
     81        @event.listens_for(db.engine, "connect")
     82        def load_spatialite_extension(dbapi_conn, connection_record):
     83            load_spatialite(dbapi_conn, connection_record)
     84
     85        if flask.current_app.config['CREATE_TEST_DATABASE'] is True:
     86            from gnuboat.mapservice_impl.demo import DbSetup as demoDbSetup
     87            demoDbSetup.setup()
     88        else:
     89            db_setup()
Note: See TracChangeset for help on using the changeset viewer.