Skip to content

Commit 96c36d9

Browse files
0einstein0slint
authored andcommitted
jobs: updated last_success condition and update_run
1 parent 74388a6 commit 96c36d9

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

invenio_jobs/jobs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ def _build_task_arguments(cls, job_obj, since=None, custom_args=None, **kwargs):
101101
if custom_args:
102102
return custom_args
103103

104-
if since is None and job_obj.last_runs["success"]:
104+
last_success = job_obj.last_runs.get("success")
105+
106+
if since is None and last_success and last_success.started_at:
107+
105108
"""
106109
The most common case: `since` has not been manually specified by the user, so we
107110
set it to the start time of the last successful job.
@@ -110,7 +113,7 @@ def _build_task_arguments(cls, job_obj, since=None, custom_args=None, **kwargs):
110113
For comparison with other dates in job implementors, it's useful to have TZ info in the timestamp.
111114
"""
112115

113-
since = job_obj.last_runs["success"].started_at.replace(tzinfo=timezone.utc)
116+
since = last_success.started_at.replace(tzinfo=timezone.utc)
114117

115118
"""
116119
Otherwise, since is already specified as a datetime with a timezone (see PredefinedArgsSchema) or we have never

invenio_jobs/tasks.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,32 @@ def update_run(run, **kwargs):
2727
"""Method to update and commit run updates."""
2828
if not run:
2929
return
30+
3031
has_active_subtasks = (
3132
run.subtasks.filter(
3233
Run.status.in_([RunStatusEnum.RUNNING.value, RunStatusEnum.QUEUED.value])
3334
).count()
3435
> 0
3536
)
37+
3638
current_app.logger.info(
3739
f"Updating run {run.id} with status {run.status} and active subtasks: {has_active_subtasks}"
3840
)
39-
if has_active_subtasks:
40-
# If there are active subtasks, we keep the run status as RUNNING and simply update the errored entries, if present.
41-
if errored_entries := kwargs.get("errored_entries", None):
41+
42+
new_status = kwargs.get("status")
43+
if has_active_subtasks and new_status != RunStatusEnum.RUNNING:
44+
# If subtasks are active, only update errored_entries
45+
if errored_entries := kwargs.get("errored_entries"):
4246
run.errored_entries += errored_entries
43-
db.session.commit()
47+
db.session.commit()
4448
return
49+
50+
# Update all fields (either no active subtasks, or setting status to RUNNING)
4551
for kw, value in kwargs.items():
46-
setattr(run, kw, value)
52+
if kw == "errored_entries":
53+
run.errored_entries += value
54+
else:
55+
setattr(run, kw, value)
4756
db.session.commit()
4857

4958

0 commit comments

Comments
 (0)