Ejemplo n.º 1
0
def remove_project_from_cache(path):
    """Removes a project from server's cache

    :param path: project path
    :type path: str
    """
    QgsConfigCache.instance().removeEntry(path)
    QGS_SERVER.serverInterface().capabilitiesCache(
    ).removeCapabilitiesDocument(path)
    logger.warning('QGIS Server project removed from cache: %s' % path)
Ejemplo n.º 2
0
def get_qgs_project(path):
    """Reads and returns a project from the cache, trying to load it
    if it's not there.
    A None is returned if the project could not be loaded.

    :param path: the filesystem path to the project
    :type path: str
    :return: the QgsProject instance or None
    :rtype: QgsProject or None
    """

    try:
        # Call process events in case the project has been updated and the cache
        # needs rebuilt
        QgsApplication.instance().processEvents()
        project = QgsConfigCache.instance().project(path, QGS_SERVER_SETTINGS)
        # This is required after QGIS 3.10.11, see https://github.com/qgis/QGIS/pull/38488#issuecomment-692190106
        if project is not None and project != QgsProject.instance():
            try:
                QgsProject.setInstance(project)
            except AttributeError:  # Temporary workaround for 3.10.10
                QgsProject.instance().read(path)
        return project
    except:
        logger.warning(
            'There was an error loading the project from path: %s, this is normally due to unavailable layers. If this is unexpected, please turn on and check server debug logs for further details.'
            % path)
        return None
Ejemplo n.º 3
0
def get_qgs_project(path):
    """Reads and returns a project from the cache, trying to load it
    if it's not there.

    A None is returned if the project could not be loaded.

    :param path: the filesystem path to the project
    :type path: str
    :return: the QgsProject instance or None
    :rtype: QgsProject or None
    """

    try:
        # Call process events in case the project has been updated and the cache
        # needs rebuilt. This triggers the QGIS server internal cache manager that
        # invalidates the cache if the file has changed. QGIS internal implementation
        # does not work reliably with project that are stored on network mounted
        # volumes, in that case we need to use our own cache manager, enable it with
        # G3WADMIN_USE_CUSTOM_CACHE_INVALIDATOR=True

        QgsApplication.instance().processEvents()

        if USE_CUSTOM_CACHE_INVALIDATOR:
            ProjectCacheInvalidator.check_cache(path)

        project = QgsConfigCache.instance().project(path, QGS_SERVER_SETTINGS)

        # This is required after QGIS 3.10.11, see https://github.com/qgis/QGIS/pull/38488#issuecomment-692190106
        if project is not None and project != QgsProject.instance():

            try:
                # Workaround for virtual layers bug  https://github.com/qgis/QGIS/pull/38488#issuecomment-692190106
                QgsProject.setInstance(project)
                needs_reload = False
                for l in list(project.mapLayers().values()):
                    if not l.isValid():
                        logger.warning('Invalid layer %s found in project %s' %
                                       (l.id(), project.fileName()))
                        if l.dataProvider().name() == 'virtual':
                            needs_reload = True
                            logger.warning(
                                'Invalid virtual layer found in project %s: %s'
                                % (project.fileName(), l.publicSource()))
                if needs_reload:
                    logger.warning('Reload project %s' % project.fileName())
                    QgsProject.instance().read(path)
            except AttributeError:  # Temporary workaround for 3.10.10
                logger.warning(
                    'Project reloaded because QgsProject.setInstance() is not available in this QGIS version: %s'
                    % path)
                QgsProject.instance().read(path)
        return QgsProject.instance()
    except Exception as ex:
        logger.warning(
            'There was an error loading the project from path: %s, this is normally due to unavailable layers. If this is unexpected, please turn on and check server debug logs for further details.\n%s.'
            % (path, ex))
        return None
    def executeRequest(self, request, response, project):

        map = request.parameters()['MAP']
        QgsMessageLog.logMessage(
            'INVALIDATECACHE service executeRequest for %s' % map, 'Server',
            Qgis.Info)
        try:
            QgsConfigCache.instance().removeEntry(map)
            msg = 'Success - cache cleared'
            level = Qgis.Info
            response.setStatusCode(200)
        except:
            msg = 'Fail - cache not cleared'
            level = Qgis.Warning
            response.setStatusCode(500)
        finally:
            QgsMessageLog.logMessage(msg, 'Server', level)
            response.write(msg)
Ejemplo n.º 5
0
    def teardown_databases(self, old_config, **kwargs):
        """Destroy all the non-mirror databases and cleanup projects."""

        global QGS_SERVER

        try:
            QgsConfigCache.instance().removeEntry(
                QGS_SERVER.project.qgis_project.fileName())
            QGS_SERVER.project = None
        except:
            pass

        QGS_APPLICATION.exitQgis()

        _teardown_databases(
            old_config,
            verbosity=self.verbosity,
            parallel=self.parallel,
            keepdb=self.keepdb,
        )
Ejemplo n.º 6
0
    def _init(self, ogcserver_name: str, map_file: str) -> None:
        with self.lock:
            try:
                config_cache = QgsConfigCache.instance()
                self.project = config_cache.project(map_file)
                self.layers = None

                from c2cgeoportal_commons.models.main import (  # pylint: disable=import-outside-toplevel
                    OGCServer, )

                session = self.DBSession()
                try:
                    self.ogcserver = (session.query(OGCServer).filter(
                        OGCServer.name == ogcserver_name).one_or_none())
                finally:
                    session.close()
                if self.ogcserver is None:
                    LOG.error(
                        "No OGC server found for '%s', project: '%s' => no rights",
                        ogcserver_name, map_file)
            except Exception:  # pylint: disable=broad-except
                LOG.error("Cannot setup OGCServerAccessControl", exc_info=True)
Ejemplo n.º 7
0
 def project(self):
     return QgsConfigCache.instance().project(self.map_file)