Skip to content

Commit bd9160c

Browse files
committed
Issue #505 fintune #687
- add changelog entry - doc finetuning - cover mlmodel and vectorcube too - darker code style - make test more to the point
1 parent f18dc62 commit bd9160c

File tree

7 files changed

+71
-54
lines changed

7 files changed

+71
-54
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Added `show_error_logs` argument to `cube.execute_batch()`/`job.start_and_wait()`/... to toggle the automatic printing of error logs on failure ([#505](https://github.com/Open-EO/openeo-python-client/issues/505))
13+
1214
### Changed
1315

1416
### Removed

docs/batch_jobs.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ When using
292292
:py:meth:`job.start_and_wait() <openeo.rest.job.BatchJob.start_and_wait>`
293293
or :py:meth:`cube.execute_batch() <openeo.rest.datacube.DataCube.execute_batch>`
294294
to run a batch job and it fails,
295-
the openEO Python client library will automatically
296-
print the batch job logs and instructions to help with further investigation:
295+
the openEO Python client library will print (by default)
296+
the batch job's error logs and instructions to help with further investigation:
297297
298298
.. code-block:: pycon
299299

openeo/rest/datacube.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,9 @@ def execute_batch(
25022502
25032503
.. versionadded:: 0.36.0
25042504
Added argument ``additional``.
2505+
2506+
.. versionchanged:: 0.37.0
2507+
Added argument ``show_error_logs``.
25052508
"""
25062509
# TODO: start showing deprecation warnings about these inconsistent argument names
25072510
if "format" in format_options and not out_format:
@@ -2531,8 +2534,10 @@ def execute_batch(
25312534
)
25322535
return job.run_synchronous(
25332536
outputfile=outputfile,
2534-
print=print, max_poll_interval=max_poll_interval, connection_retry_interval=connection_retry_interval,
2535-
show_error_logs=show_error_logs
2537+
print=print,
2538+
max_poll_interval=max_poll_interval,
2539+
connection_retry_interval=connection_retry_interval,
2540+
show_error_logs=show_error_logs,
25362541
)
25372542

25382543
def create_job(

openeo/rest/job.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,12 @@ def logs(
235235
return VisualList("logs", data=entries)
236236

237237
def run_synchronous(
238-
self, outputfile: Union[str, Path, None] = None,
239-
print=print, max_poll_interval=60, connection_retry_interval=30, show_error_logs: bool = True
238+
self,
239+
outputfile: Union[str, Path, None] = None,
240+
print=print,
241+
max_poll_interval=60,
242+
connection_retry_interval=30,
243+
show_error_logs: bool = True,
240244
) -> BatchJob:
241245
"""
242246
Start the job, wait for it to finish and download result
@@ -246,20 +250,28 @@ def run_synchronous(
246250
:param max_poll_interval: maximum number of seconds to sleep between status polls
247251
:param connection_retry_interval: how long to wait when status poll failed due to connection issue
248252
:param show_error_logs: whether to automatically print error logs when the batch job failed.
249-
:return:
253+
254+
.. versionchanged:: 0.37.0
255+
Added argument ``show_error_logs``.
250256
"""
251257
self.start_and_wait(
252-
print=print, max_poll_interval=max_poll_interval, connection_retry_interval=connection_retry_interval,
253-
show_error_logs=show_error_logs
258+
print=print,
259+
max_poll_interval=max_poll_interval,
260+
connection_retry_interval=connection_retry_interval,
261+
show_error_logs=show_error_logs,
254262
)
255263
# TODO #135 support multi file result sets too?
256264
if outputfile is not None:
257265
self.download_result(outputfile)
258266
return self
259267

260268
def start_and_wait(
261-
self, print=print, max_poll_interval: int = 60, connection_retry_interval: int = 30, soft_error_max=10,
262-
show_error_logs: bool = True
269+
self,
270+
print=print,
271+
max_poll_interval: int = 60,
272+
connection_retry_interval: int = 30,
273+
soft_error_max=10,
274+
show_error_logs: bool = True,
263275
) -> BatchJob:
264276
"""
265277
Start the batch job, poll its status and wait till it finishes (or fails)
@@ -269,7 +281,9 @@ def start_and_wait(
269281
:param connection_retry_interval: how long to wait when status poll failed due to connection issue
270282
:param soft_error_max: maximum number of soft errors (e.g. temporary connection glitches) to allow
271283
:param show_error_logs: whether to automatically print error logs when the batch job failed.
272-
:return:
284+
285+
.. versionchanged:: 0.37.0
286+
Added argument ``show_error_logs``.
273287
"""
274288
# TODO rename `connection_retry_interval` to something more generic?
275289
start_time = time.time()

openeo/rest/mlmodel.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ def execute_batch(
7171
connection_retry_interval=30,
7272
additional: Optional[dict] = None,
7373
job_options: Optional[dict] = None,
74+
show_error_logs: bool = True,
7475
) -> BatchJob:
7576
"""
7677
Evaluate the process graph by creating a batch job, and retrieving the results when it is finished.
7778
This method is mostly recommended if the batch job is expected to run in a reasonable amount of time.
7879
79-
For very long running jobs, you probably do not want to keep the client running.
80+
For very long-running jobs, you probably do not want to keep the client running.
8081
8182
:param job_options:
8283
:param outputfile: The path of a file to which a result can be written
@@ -85,9 +86,13 @@ def execute_batch(
8586
:param additional: additional (top-level) properties to set in the request body
8687
:param job_options: dictionary of job options to pass to the backend
8788
(under top-level property "job_options")
89+
:param show_error_logs: whether to automatically print error logs when the batch job failed.
8890
8991
.. versionadded:: 0.36.0
9092
Added argument ``additional``.
93+
94+
.. versionchanged:: 0.37.0
95+
Added argument ``show_error_logs``.
9196
"""
9297
job = self.create_job(
9398
title=title,
@@ -100,7 +105,10 @@ def execute_batch(
100105
return job.run_synchronous(
101106
# TODO #135 support multi file result sets too
102107
outputfile=outputfile,
103-
print=print, max_poll_interval=max_poll_interval, connection_retry_interval=connection_retry_interval
108+
print=print,
109+
max_poll_interval=max_poll_interval,
110+
connection_retry_interval=connection_retry_interval,
111+
show_error_logs=show_error_logs,
104112
)
105113

106114
def create_job(

openeo/rest/vectorcube.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ def execute_batch(
259259
job_options: Optional[dict] = None,
260260
validate: Optional[bool] = None,
261261
auto_add_save_result: bool = True,
262+
show_error_logs: bool = True,
262263
# TODO: avoid using kwargs as format options
263264
**format_options,
264265
) -> BatchJob:
@@ -277,6 +278,7 @@ def execute_batch(
277278
:param validate: Optional toggle to enable/prevent validation of the process graphs before execution
278279
(overruling the connection's ``auto_validate`` setting).
279280
:param auto_add_save_result: Automatically add a ``save_result`` node to the process graph if there is none yet.
281+
:param show_error_logs: whether to automatically print error logs when the batch job failed.
280282
281283
.. versionchanged:: 0.21.0
282284
When not specified explicitly, output format is guessed from output file extension.
@@ -286,6 +288,9 @@ def execute_batch(
286288
287289
.. versionadded:: 0.36.0
288290
Added argument ``additional``.
291+
292+
.. versionchanged:: 0.37.0
293+
Added argument ``show_error_logs``.
289294
"""
290295
cube = self
291296
if auto_add_save_result:
@@ -310,7 +315,10 @@ def execute_batch(
310315
return job.run_synchronous(
311316
# TODO #135 support multi file result sets too
312317
outputfile=outputfile,
313-
print=print, max_poll_interval=max_poll_interval, connection_retry_interval=connection_retry_interval
318+
print=print,
319+
max_poll_interval=max_poll_interval,
320+
connection_retry_interval=connection_retry_interval,
321+
show_error_logs=show_error_logs,
314322
)
315323

316324
def create_job(

tests/rest/test_job.py

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -150,56 +150,36 @@ def test_execute_batch_with_error(con100, requests_mock, tmpdir):
150150
"Full logs can be inspected in an openEO (web) editor or with `connection.job('f00ba5').logs()`.",
151151
]
152152

153-
def test_execute_batch_with_error_with_error_logs_disabled(con100, requests_mock, tmpdir):
153+
154+
@pytest.mark.parametrize("show_error_logs", [True, False])
155+
def test_execute_batch_show_error_logs(con100, requests_mock, show_error_logs):
154156
requests_mock.get(API_URL + "/file_formats", json={"output": {"GTiff": {"gis_data_types": ["raster"]}}})
155157
requests_mock.get(API_URL + "/collections/SENTINEL2", json={"foo": "bar"})
156158
requests_mock.post(API_URL + "/jobs", status_code=201, headers={"OpenEO-Identifier": "f00ba5"})
157159
requests_mock.post(API_URL + "/jobs/f00ba5/results", status_code=202)
158-
requests_mock.get(
159-
API_URL + "/jobs/f00ba5",
160-
[
161-
{"json": {"status": "submitted"}},
162-
{"json": {"status": "queued"}},
163-
{"json": {"status": "running", "progress": 15}},
164-
{"json": {"status": "running", "progress": 80}},
165-
{"json": {"status": "error", "progress": 100}},
166-
],
167-
)
160+
requests_mock.get(API_URL + "/jobs/f00ba5", json={"status": "error", "progress": 100})
168161
requests_mock.get(
169162
API_URL + "/jobs/f00ba5/logs",
170-
json={
171-
"logs": [
172-
{"id": "12", "level": "info", "message": "starting"},
173-
{"id": "34", "level": "error", "message": "nope"},
174-
]
175-
},
163+
json={"logs": [{"id": "34", "level": "error", "message": "nope"}]},
176164
)
177165

178-
path = tmpdir.join("tmp.tiff")
179-
log = []
180-
181-
try:
182-
with fake_time():
183-
con100.load_collection("SENTINEL2").execute_batch(
184-
outputfile=path, out_format="GTIFF",
185-
max_poll_interval=.1, print=log.append, show_error_logs=False
186-
)
187-
pytest.fail("execute_batch should fail")
188-
except JobFailedException as e:
189-
assert e.job.status() == "error"
190-
assert [(l.level, l.message) for l in e.job.logs()] == [
191-
("info", "starting"),
192-
("error", "nope"),
193-
]
166+
stdout = []
167+
with fake_time(), pytest.raises(JobFailedException):
168+
con100.load_collection("SENTINEL2").execute_batch(
169+
max_poll_interval=0.1, print=stdout.append, show_error_logs=show_error_logs
170+
)
194171

195-
assert log == [
172+
expected = [
196173
"0:00:01 Job 'f00ba5': send 'start'",
197-
"0:00:02 Job 'f00ba5': submitted (progress N/A)",
198-
"0:00:04 Job 'f00ba5': queued (progress N/A)",
199-
"0:00:07 Job 'f00ba5': running (progress 15%)",
200-
"0:00:12 Job 'f00ba5': running (progress 80%)",
201-
"0:00:20 Job 'f00ba5': error (progress 100%)",
174+
"0:00:02 Job 'f00ba5': error (progress 100%)",
202175
]
176+
if show_error_logs:
177+
expected += [
178+
"Your batch job 'f00ba5' failed. Error logs:",
179+
[{"id": "34", "level": "error", "message": "nope"}],
180+
"Full logs can be inspected in an openEO (web) editor or with `connection.job('f00ba5').logs()`.",
181+
]
182+
assert stdout == expected
203183

204184

205185
@pytest.mark.parametrize(["error_response", "expected"], [

0 commit comments

Comments
 (0)