Skip to content

Commit

Permalink
Fix bug when trying to get job result in binary format
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinPontius committed Aug 22, 2024
1 parent 15be1dc commit 914fc51
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
13 changes: 11 additions & 2 deletions pygeoapi/process/manager/mongodb_.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from pymongo import MongoClient

from pygeoapi.api import FORMAT_TYPES, F_JSON, F_JSONLD
from pygeoapi.process.base import (
JobNotFoundError,
JobResultNotFoundError,
Expand Down Expand Up @@ -148,8 +149,16 @@ def get_job_result(self, job_id):
if entry["status"] != "successful":
LOGGER.info("JOBMANAGER - job not finished or failed")
return (None,)
with open(entry["location"], "r") as file:
data = json.load(file)
if not entry["location"]:
LOGGER.warning(f"job {job_id!r} - unknown result location")
raise JobResultNotFoundError()
if entry["mimetype"] in (None, FORMAT_TYPES[F_JSON],
FORMAT_TYPES[F_JSONLD]):
with open(entry["location"], "r") as file:
data = json.load(file)
else:
with open(entry["location"], "rb") as file:
data = file.read()
LOGGER.info("JOBMANAGER - MongoDB job result queried")
return entry["mimetype"], data
except Exception as err:
Expand Down
10 changes: 8 additions & 2 deletions pygeoapi/process/manager/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from sqlalchemy.engine import make_url
from sqlalchemy.orm import Session

from pygeoapi.api import FORMAT_TYPES, F_JSON, F_JSONLD
from pygeoapi.process.base import (
JobNotFoundError,
JobResultNotFoundError,
Expand Down Expand Up @@ -286,8 +287,13 @@ def get_job_result(self, job_id: str) -> Tuple[str, Any]:
else:
try:
location = Path(location)
with location.open(encoding='utf-8') as fh:
result = json.load(fh)
if mimetype in (None, FORMAT_TYPES[F_JSON],
FORMAT_TYPES[F_JSONLD]):
with location.open('r', encoding='utf-8') as fh:
result = json.load(fh)
else:
with location.open('rb') as fh:
result = fh.read()
except (TypeError, FileNotFoundError, json.JSONDecodeError):
raise JobResultNotFoundError()
else:
Expand Down
10 changes: 8 additions & 2 deletions pygeoapi/process/manager/tinydb_.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import tinydb
from filelock import FileLock

from pygeoapi.api import FORMAT_TYPES, F_JSON, F_JSONLD
from pygeoapi.process.base import (
JobNotFoundError,
JobResultNotFoundError,
Expand Down Expand Up @@ -196,8 +197,13 @@ def get_job_result(self, job_id: str) -> Tuple[str, Any]:
else:
try:
location = Path(location)
with location.open('r', encoding='utf-8') as filehandler:
result = json.load(filehandler)
if mimetype in (None, FORMAT_TYPES[F_JSON],
FORMAT_TYPES[F_JSONLD]):
with location.open('r', encoding='utf-8') as filehandler:
result = json.load(filehandler)
else:
with location.open('rb') as filehandler:
result = filehandler.read()
except (TypeError, FileNotFoundError, json.JSONDecodeError):
raise JobResultNotFoundError()
else:
Expand Down
30 changes: 29 additions & 1 deletion tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

import pytest

from pygeoapi.process.base import UnknownProcessError
from pygeoapi.process.base import UnknownProcessError, JobNotFoundError
from pygeoapi.process.manager.base import get_manager

from .util import get_test_file_path


@pytest.fixture()
def config() -> Dict:
Expand All @@ -41,6 +43,7 @@ def config() -> Dict:
'manager': {
'name': 'TinyDB',
'output_dir': '/tmp',
'connection': '/tmp/pygeoapi-process-manager-test.db'
}
},
'resources': {
Expand Down Expand Up @@ -71,3 +74,28 @@ def test_get_processor_raises_exception(config):
manager = get_manager(config)
with pytest.raises(expected_exception=UnknownProcessError):
manager.get_processor('foo')


def test_get_job_result_binary(config):
manager = get_manager(config)
nc_file = get_test_file_path("tests/data/coads_sst.nc")
job_id = "15eeae38-608c-11ef-81c8-0242ac130002"
job_metadata = {
"type": "process",
"identifier": job_id,
"process_id": "dummy",
"job_start_datetime": "2024-08-22T12:00:00.000000Z",
"job_end_datetime": "2024-08-22T12:00:01.000000Z",
"status": "successful",
"location": nc_file,
"mimetype": "application/x-netcdf",
"message": "Job complete",
"progress": 100
}
try:
manager.get_job(job_id)
except JobNotFoundError:
manager.add_job(job_metadata)
mimetype, result = manager.get_job_result(job_id)
assert mimetype == "application/x-netcdf"
assert isinstance(result, bytes)

0 comments on commit 914fc51

Please sign in to comment.