Skip to content

Commit d22cee9

Browse files
committed
PR #736 finetune test_thread_worker
- support waiting directly in process_futures - switch to DummyBackend - use more dummy tasks TestJobManagerWorkerThreadPool to simplify setup
1 parent 03d7f44 commit d22cee9

File tree

4 files changed

+208
-132
lines changed

4 files changed

+208
-132
lines changed

openeo/extra/job_management/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ def _process_threadworker_updates(
688688
:return:
689689
None: All updates are applied in-place to the job_db and stats parameters.
690690
"""
691-
results = worker_pool.process_futures()
691+
results, _ = worker_pool.process_futures()
692692
stats_updates = collections.defaultdict(int)
693693

694694
for result in results:

openeo/extra/job_management/_thread_worker.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import logging
77
from abc import ABC, abstractmethod
88
from dataclasses import dataclass, field
9-
from typing import Any, Dict, List, Optional, Tuple
9+
from typing import Any, Dict, List, Optional, Tuple, Union
1010

1111
import openeo
1212

@@ -150,42 +150,42 @@ def submit_task(self, task: Task) -> None:
150150
future = self._executor.submit(task.execute)
151151
self._future_task_pairs.append((future, task)) # Track pairs
152152

153-
def process_futures(self) -> List[_TaskResult]:
153+
def process_futures(self, timeout: Union[float, None] = 0) -> Tuple[List[_TaskResult], int]:
154154
"""
155-
Process and retrieve results from completed tasks.
155+
Checks state of futures and collect results from completed ones.
156156
157-
This method checks which futures have finished without blocking,
158-
collects their results.
157+
:param timeout: whether to wait for futures to complete or not:
158+
- 0: don't wait, just return current state.
159+
- non-zero value: wait for that many seconds to allow futures to complete,
160+
- None: (not recommended) wait indefinitely.
159161
160162
:returns:
161-
A list of `_TaskResult` objects from completed tasks.
163+
Tuple of two elements: list of `_TaskResult` objects from completed tasks.
164+
and number of remaining tasks that are still in progress.
162165
"""
163166
results = []
164167
to_keep = []
165168

166-
# Use timeout=0 to avoid blocking and check for completed futures
167-
done, _ = concurrent.futures.wait(
168-
[f for f, _ in self._future_task_pairs], timeout=0, return_when=concurrent.futures.FIRST_COMPLETED
169-
)
169+
done, _ = concurrent.futures.wait([f for f, _ in self._future_task_pairs], timeout=timeout)
170170

171-
# Process completed futures and their tasks
172171
for future, task in self._future_task_pairs:
173172
if future in done:
174173
try:
175174
result = future.result()
176175
except Exception as e:
177-
_log.exception(f"Failed to get result from future: {e}")
176+
_log.exception(f"Threaded task {task!r} failed: {e!r}")
178177
result = _TaskResult(
179178
job_id=task.job_id,
180-
db_update={"status": "future.result() failed"},
181-
stats_update={"future.result() error": 1},
179+
db_update={"status": "threaded task failed"},
180+
stats_update={"threaded task failed": 1},
182181
)
183182
results.append(result)
184183
else:
185184
to_keep.append((future, task))
185+
_log.info("process_futures: %d tasks done, %d tasks remaining", len(results), len(to_keep))
186186

187187
self._future_task_pairs = to_keep
188-
return results
188+
return results, len(to_keep)
189189

190190
def shutdown(self) -> None:
191191
"""Shuts down the thread pool gracefully."""

openeo/rest/_testing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class DummyBackend:
3535
# TODO: move to openeo.testing
3636
# TODO: unify "batch_jobs", "batch_jobs_full" and "extra_job_metadata_fields"?
3737
# TODO: unify "sync_requests" and "sync_requests_full"?
38+
# TODO: support checking bearer token
3839

3940
__slots__ = (
4041
"_requests_mock",

0 commit comments

Comments
 (0)