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
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