Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set background tasks #120

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions plugin_qgis_lpo/commons/tasking_refresh_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from functools import partial

Check warning on line 1 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L1

Added line #L1 was not covered by tests

from qgis.core import (

Check warning on line 3 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L3

Added line #L3 was not covered by tests
QgsApplication,
QgsMessageLog,
QgsProcessingAlgRunnerTask,
QgsProcessingContext,
QgsProcessingFeedback,
QgsProject,
QgsTaskManager,
)

from ..toolbelt.log_handler import PlgLogger

Check warning on line 13 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L13

Added line #L13 was not covered by tests

MESSAGE_CATEGORY = "AlgRunnerTask"

Check warning on line 15 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L15

Added line #L15 was not covered by tests

logger = PlgLogger().log

Check warning on line 17 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L17

Added line #L17 was not covered by tests


def task_finished(_context, successful, _results):

Check warning on line 20 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L20

Added line #L20 was not covered by tests
if not successful:
logger.log(

Check warning on line 22 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L22

Added line #L22 was not covered by tests
message=str("Task finished unsucessfully"),
log_level=2,
push=True,
duration=2,
)
else:
logger.log(

Check warning on line 29 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L29

Added line #L29 was not covered by tests
message=str("Task finished successfully"),
log_level=0,
push=True,
duration=2,
)


def bg_task(algorithm: str, params: dict):
alg = QgsApplication.processingRegistry().algorithmById(algorithm)

Check warning on line 38 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L37-L38

Added lines #L37 - L38 were not covered by tests

context = QgsProcessingContext()
feedback = QgsProcessingFeedback()

Check warning on line 41 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L40-L41

Added lines #L40 - L41 were not covered by tests

task = QgsProcessingAlgRunnerTask(alg, params, context, feedback)

Check warning on line 43 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L43

Added line #L43 was not covered by tests

task.executed.connect(partial(task_finished, context))
return task

Check warning on line 46 in plugin_qgis_lpo/commons/tasking_refresh_data.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/tasking_refresh_data.py#L45-L46

Added lines #L45 - L46 were not covered by tests
108 changes: 77 additions & 31 deletions plugin_qgis_lpo/plugin_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
__title__,
__uri_homepage__,
)
from plugin_qgis_lpo.commons.tasking_refresh_data import bg_task

Check warning on line 35 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L35

Added line #L35 was not covered by tests
from plugin_qgis_lpo.gui.dlg_settings import PlgOptionsFactory
from plugin_qgis_lpo.gui.menu_tools import MenuTools
from plugin_qgis_lpo.processing.provider import QgisLpoProvider
Expand Down Expand Up @@ -63,6 +64,7 @@
self.iface: QgisInterface = iface
self.tools_menu: Optional[QMenu] = None
self.main_menu: Optional[QMenu] = None
self.tm = QgsApplication.taskManager()

Check warning on line 67 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L67

Added line #L67 was not covered by tests
# translation
# initialize the locale
self.locale: str = QgsSettings().value("locale/userLocale", QLocale().name())[
Expand Down Expand Up @@ -141,6 +143,12 @@
self.iface.mainWindow(),
)
self.especes_action.triggered.connect(self.runEspeces)
self.refresh_data_action = QAction(

Check warning on line 146 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L146

Added line #L146 was not covered by tests
QIcon(str(__icon_dir_path__ / "refresh.png")),
"Rafraichir les données TEST",
self.iface.mainWindow(),
)
self.refresh_data_action.triggered.connect(self.populateSettings)

Check warning on line 151 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L151

Added line #L151 was not covered by tests
try:
# Try to put the button in the LPO menu bar
# lpo_menu = [
Expand All @@ -167,39 +175,76 @@
"Attention",
"La carte par espèces est accessible via la barre d'outils d'Extensions",
)

self.populateSettings()
# -- Post UI initialization
self.log(

Check warning on line 179 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L179

Added line #L179 was not covered by tests
message="Plugin QGIS LpO loaded",
log_level=0,
push=True,
duration=60,
)
self.iface.initializationCompleted.connect(self.populateSettings)

Check warning on line 185 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L185

Added line #L185 was not covered by tests

def populateSettings(self):
try:
postgres_metadata = QgsProviderRegistry.instance().providerMetadata(
"postgres"
)
if "geonature_lpo" in postgres_metadata.dbConnections():
self.log(
message=f"Loading GeoNature required data",
log_level=0,
push=True,
duration=60,
)
processing.run(
"plugin_qgis_lpo:RefreshData", {"DATABASE": "geonature_lpo"}
)
self.log(
message=f"Loading GeoNature terminated",
log_level=0,
push=True,
duration=2,
)
except QgsProviderConnectionException as exc:
self.log(
message=self.tr("Houston, we've got a problem: {}".format(exc)),
log_level=2,
push=True,
)
raise QgsProcessingException(
f"Could not retrieve connection details : {str(exc)}"
) from exc # processing.run("plugin_qgis_lpo:RefreshData", {"DATABASE": "geonature_lpo"})
self.log(

Check warning on line 188 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L188

Added line #L188 was not covered by tests
message="Loading GeoNature required data",
log_level=0,
push=True,
duration=60,
)
task_no = self.tm.addTask(

Check warning on line 194 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L194

Added line #L194 was not covered by tests
bg_task("plugin_qgis_lpo:RefreshData", {"DATABASE": "geonature_lpo"})
)
self.log(

Check warning on line 197 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L197

Added line #L197 was not covered by tests
message=f"{str(type(self.tm))} {str(dir(self.tm))}",
log_level=0,
push=True,
duration=2,
)
self.log(

Check warning on line 203 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L203

Added line #L203 was not covered by tests
message=f"task_no {task_no}",
log_level=0,
push=True,
duration=2,
)

# self.log(
# message=f"Loading GeoNature terminated",
# log_level=0,
# push=True,
# duration=2,
# )
# try:
# postgres_metadata = QgsProviderRegistry.instance().providerMetadata(
# "postgres"
# )
# if "geonature_lpo" in postgres_metadata.dbConnections():
# self.log(
# message=f"Loading GeoNature required data",
# log_level=0,
# push=True,
# duration=60,
# )
# self.tm.addTask(
# bg_task(
# "plugin_qgis_lpo:RefreshData", {"DATABASE": "geonature_lpo"}
# )
# )
# self.log(
# message=f"Loading GeoNature terminated",
# log_level=0,
# push=True,
# duration=2,
# )
# except QgsProviderConnectionException as exc:
# self.log(
# message=self.tr("Houston, we've got a problem: {}".format(exc)),
# log_level=2,
# push=True,
# )
# raise QgsProcessingException(
# f"Could not retrieve connection details : {str(exc)}"
# ) from exc # processing.run("plugin_qgis_lpo:RefreshData", {"DATABASE": "geonature_lpo"})
return

Check warning on line 247 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L247

Added line #L247 was not covered by tests

def runEspeces(self): # noqa N802
connection_name = get_connection_name()
Expand Down Expand Up @@ -254,6 +299,7 @@
:raises Exception: if there is no item in the feed
"""
try:
self.populateSettings()

Check warning on line 302 in plugin_qgis_lpo/plugin_main.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/plugin_main.py#L302

Added line #L302 was not covered by tests
self.log(
message=self.tr("Everything ran OK."),
log_level=3,
Expand Down