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

Adding QgsTask in jobs submission #841

Draft
wants to merge 4 commits into
base: main
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
10 changes: 8 additions & 2 deletions LDMP/calculate_drought_vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import qgis.gui
from qgis.core import QgsGeometry
from qgis.PyQt import QtCore, QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript

from . import conf
from .calculate import DlgCalculateBase
from .jobs.manager import job_manager
from .localexecution import drought
from .logger import log
from .tasks import create_task

DlgCalculateDroughtUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateDrought.ui")
Expand Down Expand Up @@ -145,7 +146,12 @@ def btn_calculate(self):

self.close()

resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)

if resp:
main_msg = "Submitted"
Expand Down
22 changes: 19 additions & 3 deletions LDMP/calculate_lc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from . import calculate, lc_setup
from .jobs.manager import job_manager
from .tasks import create_task

DlgCalculateLcUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateLC.ui")
Expand Down Expand Up @@ -97,9 +98,17 @@ def calculate_on_GEE(self):
"task_name": self.execution_name_le.text(),
"task_notes": self.task_notes.toPlainText(),
}
job = job_manager.submit_remote_job(payload, self.script.id)

if job is not None:
create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
self.job_submitted,
)

def job_submitted(self, exception, result=None):
if result is not None:
main_msg = "Submitted"
description = "Land cover task submitted to Trends.Earth server."
else:
Expand Down Expand Up @@ -206,4 +215,11 @@ def calculate_locally(self):
"legend_nesting": initial_nesting,
"trans_matrix": LCTransitionDefinitionDeg.Schema().dumps(trans_matrix),
}
job_manager.submit_local_job(job_params, self.LOCAL_SCRIPT_NAME, self.aoi)

create_task(
job_manager,
job_params,
self.LOCAL_SCRIPT_NAME,
AlgorithmRunMode.LOCAL,
self.aoi,
)
15 changes: 12 additions & 3 deletions LDMP/calculate_ldn.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import te_algorithms.gdal.land_deg.config as ld_config
from qgis.core import QgsGeometry
from qgis.PyQt import QtCore, QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript
from te_schemas.land_cover import LCLegendNesting, LCTransitionDefinitionDeg
from te_schemas.productivity import ProductivityMode

Expand All @@ -29,6 +29,7 @@
from .jobs.manager import job_manager
from .localexecution import ldn
from .logger import log
from .tasks import create_task

DlgCalculateOneStepUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateOneStep.ui")
Expand Down Expand Up @@ -623,7 +624,9 @@ def btn_calculate(self):
self.close()

for payload in payloads:
resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager, payload, self.script.id, AlgorithmRunMode.REMOTE
)

if resp:
main_msg = "Submitted"
Expand Down Expand Up @@ -1212,7 +1215,13 @@ def btn_calculate(self):
#
# self.close()
#
# resp = job_manager.submit_remote_job(payload, self.script.id)

# resp = create_task(
# job_manager,
# payload,
# self.script.id,
# AlgorithmRunMode.REMOTE,
# )
#
# if resp:
# main_msg = "Submitted"
Expand Down
10 changes: 8 additions & 2 deletions LDMP/calculate_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

import qgis.gui
from qgis.PyQt import QtCore, QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript
from te_schemas.productivity import ProductivityMode

from . import calculate, conf
from .jobs.manager import job_manager
from .logger import log
from .tasks import create_task

DlgCalculateProdUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateProd.ui")
Expand Down Expand Up @@ -270,7 +271,12 @@ def btn_calculate(self):
else:
raise ValueError("Unknown prod_mode {prod_mode}")

resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)

if resp:
main_msg = "Submitted"
Expand Down
10 changes: 8 additions & 2 deletions LDMP/calculate_rest_biomass.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
import qgis.core
import qgis.gui
from qgis.PyQt import QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript

from . import calculate, data_io
from .jobs.manager import job_manager
from .localexecution import biomassrestoration
from .tasks import create_task

DlgCalculateRestBiomassDataUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateRestBiomassData.ui")
Expand Down Expand Up @@ -79,7 +80,12 @@ def calculate_on_GEE(self):
"task_notes": self.task_notes.toPlainText(),
}

resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)
if resp:
main_msg = "Submitted"
description = "Restoration biomass change submitted to Trends.Earth server."
Expand Down
8 changes: 7 additions & 1 deletion LDMP/calculate_soc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .jobs.manager import job_manager
from .lc_setup import get_trans_matrix
from .logger import log
from .tasks import create_task

DlgCalculateSocUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateSOC.ui")
Expand Down Expand Up @@ -286,7 +287,12 @@ def calculate_on_GEE(self):
"task_notes": self.options_tab.task_notes.toPlainText(),
}

resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)
if resp:
main_msg = "Submitted"
description = "Soil organic carbon task submitted to Trends.Earth server."
Expand Down
12 changes: 10 additions & 2 deletions LDMP/calculate_tc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import qgis.gui
from osgeo import gdal, osr
from qgis.PyQt import QtCore, QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript
from te_schemas.schemas import BandInfo

from . import GetTempFilename, calculate, conf, data_io, worker
from .jobs.manager import job_manager
from .logger import log
from .summary import calc_cell_area
from .tasks import create_task

DlgCalculateTcDataUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateTCData.ui")
Expand Down Expand Up @@ -427,7 +428,14 @@ def calculate_on_GEE(self, method, biomass_data):
"task_name": self.execution_name_le.text(),
"task_notes": self.task_notes.toPlainText(),
}
resp = job_manager.submit_remote_job(payload, self.script.id)

resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)

if resp:
main_msg = "Submitted"
description = "Total carbon submitted to Trends.Earth server."
Expand Down
10 changes: 8 additions & 2 deletions LDMP/calculate_unccd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

import qgis.gui
from qgis.PyQt import QtCore, QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript

from . import conf
from .calculate import DlgCalculateBase
from .jobs.manager import job_manager
from .localexecution import unccd
from .logger import log
from .tasks import create_task

DlgCalculateUNCCDUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateUNCCD.ui")
Expand Down Expand Up @@ -135,7 +136,12 @@ def btn_calculate(self):

self.close()

resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)

if resp:
main_msg = "Submitted"
Expand Down
10 changes: 8 additions & 2 deletions LDMP/calculate_urban.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
import qgis.core
import qgis.gui
from qgis.PyQt import QtCore, QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript

from . import calculate, data_io
from .jobs.manager import job_manager
from .logger import log
from .task import create_task

DlgCalculateUrbanDataUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgCalculateUrbanData.ui")
Expand Down Expand Up @@ -196,7 +197,12 @@ def calculate_on_GEE(self):
"task_notes": self.options_tab.task_notes.toPlainText(),
}

resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)

if resp:
main_msg = "Submitted"
Expand Down
11 changes: 9 additions & 2 deletions LDMP/download_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

import qgis.gui
from qgis.PyQt import QtCore, QtGui, QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript

from . import calculate, conf
from .conf import Setting, settings_manager
from .jobs.manager import job_manager
from .logger import log
from .tasks import create_task

DlgDownloadUi, _ = uic.loadUiType(str(Path(__file__).parent / "gui/DlgDownload.ui"))

Expand Down Expand Up @@ -271,7 +272,13 @@ def btn_calculate(self):
"task_notes": "",
}

resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)

if resp:
main_msg = "Success"
description = "Download request submitted to Trends.Earth server."
Expand Down
2 changes: 1 addition & 1 deletion LDMP/jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def set_script_name_version(self, data, **kwargs):
log(f"Failed to get script by id for {script_id}")
script = get_job_local_script(data["script_name"])
else:
log(f"Failed to get script by id for {script_id}")
log(f"Got script from id {script_id}")
script = ExecutionScript(
script_id, run_mode=AlgorithmRunMode.NOT_APPLICABLE
)
Expand Down
11 changes: 9 additions & 2 deletions LDMP/landpks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

import qgis.gui
from qgis.PyQt import QtCore, QtGui, QtWidgets, uic
from te_schemas.algorithms import ExecutionScript
from te_schemas.algorithms import AlgorithmRunMode, ExecutionScript

from . import calculate, conf
from .jobs.manager import job_manager
from .logger import log
from .tasks import create_task

DlgLandPKSDownloadUi, _ = uic.loadUiType(
str(Path(__file__).parent / "gui/DlgLandPKSDownload.ui")
Expand Down Expand Up @@ -270,7 +271,13 @@ def btn_calculate(self):
"task_notes": self.options_tab.task_notes.toPlainText(),
}

resp = job_manager.submit_remote_job(payload, self.script.id)
resp = create_task(
job_manager,
payload,
self.script.id,
AlgorithmRunMode.REMOTE,
)

if resp:
main_msg = "Success"
description = "Download request submitted to LandPKS."
Expand Down
48 changes: 48 additions & 0 deletions LDMP/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from qgis.core import QgsApplication, QgsTask
from te_schemas.algorithms import AlgorithmRunMode


def submit_job(job_manager, payload, script_id, job_type, aoi, task):
"""Function that submits a job and runs in a task."""
try:
if job_type == AlgorithmRunMode.REMOTE:
job = job_manager.submit_remote_job(payload, script_id)
elif job_type == AlgorithmRunMode.LOCAL:
job = job_manager.submit_local_job(payload, script_id, aoi)

return job
except Exception:
return None


def create_task(
job_manager, payload, script_id, job_type, task_finished=None, aoi=None
):
# Create a task using fromFunction
if task_finished is None:
task_finished = on_task_finished

task = QgsTask.fromFunction(
"Submit Job",
submit_job,
on_finished=task_finished,
job_manager=job_manager,
payload=payload,
script_id=script_id,
job_type=job_type,
aoi=aoi,
)

QgsApplication.taskManager().addTask(task)

return task


def on_task_finished(exception, result=None):
if exception is None:
if result:
print(f"Task completed successfully, job: {result}")
else:
print("Task completed but no job returned.")
else:
print(f"Task failed: {exception}")
Loading
Loading