Skip to content

Commit a3b42ba

Browse files
Fix bug when trying to get job result in binary format (geopython#1798)
1 parent 2e4ff71 commit a3b42ba

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

pygeoapi/process/manager/mongodb_.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
from pymongo import MongoClient
3434

35+
from pygeoapi.api import FORMAT_TYPES, F_JSON, F_JSONLD
3536
from pygeoapi.process.base import (
3637
JobNotFoundError,
3738
JobResultNotFoundError,
@@ -151,8 +152,16 @@ def get_job_result(self, job_id):
151152
if entry["status"] != "successful":
152153
LOGGER.info("JOBMANAGER - job not finished or failed")
153154
return (None,)
154-
with open(entry["location"], "r") as file:
155-
data = json.load(file)
155+
if not entry["location"]:
156+
LOGGER.warning(f"job {job_id!r} - unknown result location")
157+
raise JobResultNotFoundError()
158+
if entry["mimetype"] in (None, FORMAT_TYPES[F_JSON],
159+
FORMAT_TYPES[F_JSONLD]):
160+
with open(entry["location"], "r") as file:
161+
data = json.load(file)
162+
else:
163+
with open(entry["location"], "rb") as file:
164+
data = file.read()
156165
LOGGER.info("JOBMANAGER - MongoDB job result queried")
157166
return entry["mimetype"], data
158167
except Exception as err:

pygeoapi/process/manager/postgresql.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from sqlalchemy.engine import make_url
5050
from sqlalchemy.orm import Session
5151

52+
from pygeoapi.api import FORMAT_TYPES, F_JSON, F_JSONLD
5253
from pygeoapi.process.base import (
5354
JobNotFoundError,
5455
JobResultNotFoundError,
@@ -292,8 +293,13 @@ def get_job_result(self, job_id: str) -> Tuple[str, Any]:
292293
else:
293294
try:
294295
location = Path(location)
295-
with location.open(encoding='utf-8') as fh:
296-
result = json.load(fh)
296+
if mimetype in (None, FORMAT_TYPES[F_JSON],
297+
FORMAT_TYPES[F_JSONLD]):
298+
with location.open('r', encoding='utf-8') as fh:
299+
result = json.load(fh)
300+
else:
301+
with location.open('rb') as fh:
302+
result = fh.read()
297303
except (TypeError, FileNotFoundError, json.JSONDecodeError):
298304
raise JobResultNotFoundError()
299305
else:

pygeoapi/process/manager/tinydb_.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import tinydb
3838
from filelock import FileLock
3939

40+
from pygeoapi.api import FORMAT_TYPES, F_JSON, F_JSONLD
4041
from pygeoapi.process.base import (
4142
JobNotFoundError,
4243
JobResultNotFoundError,
@@ -211,8 +212,13 @@ def get_job_result(self, job_id: str) -> Tuple[str, Any]:
211212
else:
212213
try:
213214
location = Path(location)
214-
with location.open('r', encoding='utf-8') as filehandler:
215-
result = json.load(filehandler)
215+
if mimetype in (None, FORMAT_TYPES[F_JSON],
216+
FORMAT_TYPES[F_JSONLD]):
217+
with location.open('r', encoding='utf-8') as filehandler:
218+
result = json.load(filehandler)
219+
else:
220+
with location.open('rb') as filehandler:
221+
result = filehandler.read()
216222
except (TypeError, FileNotFoundError, json.JSONDecodeError):
217223
raise JobResultNotFoundError()
218224
else:

tests/test_manager.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030

3131
import pytest
3232

33-
from pygeoapi.process.base import UnknownProcessError
33+
from pygeoapi.process.base import UnknownProcessError, JobNotFoundError
3434
from pygeoapi.process.manager.base import get_manager
3535

36+
from .util import get_test_file_path
37+
3638

3739
@pytest.fixture()
3840
def config() -> Dict:
@@ -41,6 +43,7 @@ def config() -> Dict:
4143
'manager': {
4244
'name': 'TinyDB',
4345
'output_dir': '/tmp',
46+
'connection': '/tmp/pygeoapi-process-manager-test.db'
4447
}
4548
},
4649
'resources': {
@@ -71,3 +74,28 @@ def test_get_processor_raises_exception(config):
7174
manager = get_manager(config)
7275
with pytest.raises(expected_exception=UnknownProcessError):
7376
manager.get_processor('foo')
77+
78+
79+
def test_get_job_result_binary(config):
80+
manager = get_manager(config)
81+
nc_file = get_test_file_path("tests/data/coads_sst.nc")
82+
job_id = "15eeae38-608c-11ef-81c8-0242ac130002"
83+
job_metadata = {
84+
"type": "process",
85+
"identifier": job_id,
86+
"process_id": "dummy",
87+
"job_start_datetime": "2024-08-22T12:00:00.000000Z",
88+
"job_end_datetime": "2024-08-22T12:00:01.000000Z",
89+
"status": "successful",
90+
"location": nc_file,
91+
"mimetype": "application/x-netcdf",
92+
"message": "Job complete",
93+
"progress": 100
94+
}
95+
try:
96+
manager.get_job(job_id)
97+
except JobNotFoundError:
98+
manager.add_job(job_metadata)
99+
mimetype, result = manager.get_job_result(job_id)
100+
assert mimetype == "application/x-netcdf"
101+
assert isinstance(result, bytes)

0 commit comments

Comments
 (0)