Skip to content

Commit efe0727

Browse files
committed
fixup! PR #736 addressing some review notes (exclude bearer from repr)
1 parent 90c7cdd commit efe0727

4 files changed

Lines changed: 45 additions & 39 deletions

File tree

openeo/extra/job_management/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,13 @@ def _process_threadworker_updates(
683683
# Process database updates
684684
if res.db_update:
685685
try:
686-
updates.append({
687-
'id': res.job_id,
688-
'df_idx': res.df_idx,
689-
**res.db_update,
690-
})
686+
updates.append(
687+
{
688+
"id": res.job_id,
689+
"df_idx": res.df_idx,
690+
**res.db_update,
691+
}
692+
)
691693
except Exception as e:
692694
_log.error(f"Skipping invalid db_update {res.db_update!r} for job {res.job_id!r}: {e}")
693695

@@ -705,21 +707,20 @@ def _process_threadworker_updates(
705707
return
706708

707709
# Build DataFrame of updates indexed by df_idx
708-
df_updates = pd.DataFrame(updates).set_index('df_idx', drop=True)
710+
df_updates = pd.DataFrame(updates).set_index("df_idx", drop=True)
709711

710712
# Determine which rows to upsert
711713
existing_indices = set(df_updates.index).intersection(job_db.read().index)
712714
if existing_indices:
713715
df_upsert = df_updates.loc[sorted(existing_indices)]
714716
job_db.persist(df_upsert)
715-
stats['job_db persist'] = stats.get('job_db persist', 0) + 1
717+
stats["job_db persist"] = stats.get("job_db persist", 0) + 1
716718

717719
# Any df_idx not in original index are errors
718720
missing = set(df_updates.index) - existing_indices
719721
if missing:
720722
_log.error(f"Skipping non-existing dataframe indices: {sorted(missing)}")
721723

722-
723724
def on_job_done(self, job: BatchJob, row):
724725
"""
725726
Handles jobs that have finished. Can be overridden to provide custom behaviour.

openeo/extra/job_management/_thread_worker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class _TaskResult:
3535
"""
3636

3737
job_id: str # Mandatory
38-
df_idx: int # Mandatory
38+
df_idx: int # Mandatory
3939
db_update: Dict[str, Any] = field(default_factory=dict) # Optional
4040
stats_update: Dict[str, int] = field(default_factory=dict) # Optional
4141

@@ -118,7 +118,7 @@ def execute(self) -> _TaskResult:
118118
_log.info(f"Job {self.job_id!r} started successfully")
119119
return _TaskResult(
120120
job_id=self.job_id,
121-
df_idx = self.df_idx,
121+
df_idx=self.df_idx,
122122
db_update={"status": "queued"},
123123
stats_update={"job start": 1},
124124
)
@@ -127,9 +127,9 @@ def execute(self) -> _TaskResult:
127127
# TODO: more insights about the failure (e.g. the exception) are just logged, but lost from the result
128128
return _TaskResult(
129129
job_id=self.job_id,
130-
df_idx = self.df_idx,
130+
df_idx=self.df_idx,
131131
db_update={"status": "start_failed"},
132-
stats_update={"start_job error": 1}
132+
stats_update={"start_job error": 1},
133133
)
134134

135135

tests/extra/job_management/test_job_management.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,14 @@ class DummyTask(Task):
9494
"""
9595

9696
def __init__(self, job_id, df_idx, db_update, stats_update):
97-
super().__init__(job_id=job_id, df_idx = df_idx)
97+
super().__init__(job_id=job_id, df_idx=df_idx)
9898
self._db_update = db_update or {}
9999
self._stats_update = stats_update or {}
100100

101101
def execute(self) -> _TaskResult:
102-
103102
return _TaskResult(
104103
job_id=self.job_id,
105-
df_idx = self.df_idx,
104+
df_idx=self.df_idx,
106105
db_update=self._db_update,
107106
stats_update=self._stats_update,
108107
)
@@ -759,10 +758,12 @@ def test_process_threadworker_updates(self, tmp_path, caplog):
759758
# Invalid index (not in DB)
760759
pool.submit_task(DummyTask("j-missing", df_idx=4, db_update={"status": "created"}, stats_update=None))
761760

762-
df_initial = pd.DataFrame({
763-
"id": ["j-0", "j-1", "j-2", "j-3"],
764-
"status": ["created", "created", "created", "created"],
765-
})
761+
df_initial = pd.DataFrame(
762+
{
763+
"id": ["j-0", "j-1", "j-2", "j-3"],
764+
"status": ["created", "created", "created", "created"],
765+
}
766+
)
766767
job_db = CsvJobDatabase(tmp_path / "jobs.csv").initialize_from_df(df_initial)
767768

768769
mgr = MultiBackendJobManager(root_dir=tmp_path / "jobs")
@@ -786,7 +787,7 @@ def test_process_threadworker_updates(self, tmp_path, caplog):
786787
assert stats["job_db persist"] == 1
787788

788789
# Assert error log for invalid index
789-
assert any("Skipping non-existing dataframe indiches" in msg for msg in caplog.messages)
790+
assert any("Skipping non-existing dataframe indices" in msg for msg in caplog.messages)
790791

791792
def test_no_results_leaves_db_and_stats_untouched(self, tmp_path, caplog):
792793
pool = _JobManagerWorkerThreadPool(max_workers=2)
@@ -802,7 +803,6 @@ def test_no_results_leaves_db_and_stats_untouched(self, tmp_path, caplog):
802803
assert df_final.loc[0, "status"] == "created"
803804
assert stats == {}
804805

805-
806806
def test_logs_on_invalid_update(self, tmp_path, caplog):
807807
pool = _JobManagerWorkerThreadPool(max_workers=2)
808808
stats = collections.defaultdict(int)

tests/extra/job_management/test_thread_worker.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def dummy_backend(requests_mock) -> DummyBackend:
2525

2626
class TestTaskResult:
2727
def test_default(self):
28-
result = _TaskResult(job_id="j-123", df_idx = 0)
28+
result = _TaskResult(job_id="j-123", df_idx=0)
2929
assert result.job_id == "j-123"
30-
assert result.df_idx ==0
30+
assert result.df_idx == 0
3131
assert result.db_update == {}
3232
assert result.stats_update == {}
3333

@@ -37,12 +37,14 @@ def test_start_success(self, dummy_backend, caplog):
3737
caplog.set_level(logging.WARNING)
3838
job = dummy_backend.connection.create_job(process_graph={})
3939

40-
task = _JobStartTask(job_id=job.job_id, df_idx=0, root_url=dummy_backend.connection.root_url, bearer_token="h4ll0")
40+
task = _JobStartTask(
41+
job_id=job.job_id, df_idx=0, root_url=dummy_backend.connection.root_url, bearer_token="h4ll0"
42+
)
4143
result = task.execute()
4244

4345
assert result == _TaskResult(
4446
job_id="job-000",
45-
df_idx = 0,
47+
df_idx=0,
4648
db_update={"status": "queued"},
4749
stats_update={"job start": 1},
4850
)
@@ -54,7 +56,9 @@ def test_start_failure(self, dummy_backend, caplog):
5456
job = dummy_backend.connection.create_job(process_graph={})
5557
dummy_backend.setup_job_start_failure()
5658

57-
task = _JobStartTask(job_id=job.job_id, df_idx=0, root_url=dummy_backend.connection.root_url, bearer_token="h4ll0")
59+
task = _JobStartTask(
60+
job_id=job.job_id, df_idx=0, root_url=dummy_backend.connection.root_url, bearer_token="h4ll0"
61+
)
5862
result = task.execute()
5963

6064
assert result == _TaskResult(
@@ -77,8 +81,6 @@ def test_hide_token(self, serializer):
7781
assert secret not in serialized
7882

7983

80-
81-
8284
class NopTask(Task):
8385
"""Do Nothing"""
8486

@@ -115,8 +117,6 @@ def execute(self) -> _TaskResult:
115117
return _TaskResult(job_id=self.job_id, df_idx=self.df_idx, db_update={"status": "all fine"})
116118

117119

118-
119-
120120
class TestJobManagerWorkerThreadPool:
121121
@pytest.fixture
122122
def worker_pool(self) -> Iterator[_JobManagerWorkerThreadPool]:
@@ -154,7 +154,7 @@ def test_submit_and_process_with_error(self, worker_pool):
154154
assert results == [
155155
_TaskResult(
156156
job_id="j-666",
157-
df_idx = 0,
157+
df_idx=0,
158158
db_update={"status": "threaded task failed"},
159159
stats_update={"threaded task failed": 1},
160160
),
@@ -171,7 +171,7 @@ def test_submit_and_process_iterative(self, worker_pool):
171171
worker_pool.submit_task(NopTask(job_id="j-22", df_idx=22))
172172
worker_pool.submit_task(NopTask(job_id="j-222", df_idx=222))
173173
results, remaining = worker_pool.process_futures(timeout=1)
174-
assert results == [_TaskResult(job_id="j-22", df_idx=22), _TaskResult(job_id="j-222", df_idx=222)]
174+
assert results == [_TaskResult(job_id="j-22", df_idx=22), _TaskResult(job_id="j-222", df_idx=222)]
175175
assert remaining == 0
176176

177177
def test_submit_multiple_simple(self, worker_pool):
@@ -212,7 +212,7 @@ def test_submit_multiple_blocking_and_failing(self, worker_pool):
212212
events[0].set()
213213
results, remaining = worker_pool.process_futures(timeout=0.1)
214214
assert results == [
215-
_TaskResult(job_id="j-0", df_idx = 0, db_update={"status": "all fine"}),
215+
_TaskResult(job_id="j-0", df_idx=0, db_update={"status": "all fine"}),
216216
]
217217
assert remaining == n - 1
218218

@@ -221,10 +221,13 @@ def test_submit_multiple_blocking_and_failing(self, worker_pool):
221221
events[j].set()
222222
results, remaining = worker_pool.process_futures(timeout=0.1)
223223
assert results == [
224-
_TaskResult(job_id="j-1", df_idx = 1, db_update={"status": "all fine"}),
225-
_TaskResult(job_id="j-2", df_idx = 2, db_update={"status": "all fine"}),
224+
_TaskResult(job_id="j-1", df_idx=1, db_update={"status": "all fine"}),
225+
_TaskResult(job_id="j-2", df_idx=2, db_update={"status": "all fine"}),
226226
_TaskResult(
227-
job_id="j-3", df_idx = 3, db_update={"status": "threaded task failed"}, stats_update={"threaded task failed": 1}
227+
job_id="j-3",
228+
df_idx=3,
229+
db_update={"status": "threaded task failed"},
230+
stats_update={"threaded task failed": 1},
228231
),
229232
]
230233
assert remaining == 1
@@ -234,7 +237,7 @@ def test_submit_multiple_blocking_and_failing(self, worker_pool):
234237
events[j].set()
235238
results, remaining = worker_pool.process_futures(timeout=0.1)
236239
assert results == [
237-
_TaskResult(job_id="j-4", df_idx = 4, db_update={"status": "all fine"}),
240+
_TaskResult(job_id="j-4", df_idx=4, db_update={"status": "all fine"}),
238241
]
239242
assert remaining == 0
240243

@@ -260,7 +263,7 @@ def test_job_start_task(self, worker_pool, dummy_backend, caplog):
260263
assert results == [
261264
_TaskResult(
262265
job_id="job-000",
263-
df_idx = 0,
266+
df_idx=0,
264267
db_update={"status": "queued"},
265268
stats_update={"job start": 1},
266269
)
@@ -278,7 +281,9 @@ def test_job_start_task_failure(self, worker_pool, dummy_backend, caplog):
278281

279282
results, remaining = worker_pool.process_futures(timeout=1)
280283
assert results == [
281-
_TaskResult(job_id="job-000", df_idx=0, db_update={"status": "start_failed"}, stats_update={"start_job error": 1})
284+
_TaskResult(
285+
job_id="job-000", df_idx=0, db_update={"status": "start_failed"}, stats_update={"start_job error": 1}
286+
)
282287
]
283288
assert remaining == 0
284289
assert caplog.messages == [

0 commit comments

Comments
 (0)