Skip to content

Commit 1ab9929

Browse files
authored
Merge pull request #492 from chrisburr/hbt-tz
Always update HeartBeatTime when /api/jobs/heartbeat is called
2 parents 8feff67 + 19a4130 commit 1ab9929

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

diracx-logic/src/diracx/logic/jobs/status.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from diracx.db.sql.job_logging.db import JobLoggingDB
4747
from diracx.db.sql.sandbox_metadata.db import SandboxMetadataDB
4848
from diracx.db.sql.task_queue.db import TaskQueueDB
49+
from diracx.db.sql.utils.functions import utcnow
4950
from diracx.logic.jobs.utils import check_and_prepare_job
5051
from diracx.logic.task_queues.priority import recalculate_tq_shares_for_entity
5152

@@ -586,6 +587,13 @@ async def add_heartbeat(
586587
)
587588
)
588589

590+
if other_ids := set(data) - set(status_changes):
591+
# If there are no status changes, we still need to update the heartbeat time
592+
heartbeat_updates = {
593+
job_id: {"HeartBeatTime": utcnow()} for job_id in other_ids
594+
}
595+
tg.create_task(job_db.set_job_attributes(heartbeat_updates))
596+
589597
os_data_by_job_id: defaultdict[int, dict[str, Any]] = defaultdict(dict)
590598
for job_id, job_data in data.items():
591599
sql_data = {}

diracx-routers/tests/jobs/test_status.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,3 +1072,29 @@ def test_diracx_476(normal_user_client: TestClient, valid_job_id: int):
10721072
payload = {valid_job_id: {(time - timedelta(minutes=2)).isoformat(): inner_payload}}
10731073
r = normal_user_client.patch("/api/jobs/status", json={valid_job_id: payload})
10741074
assert r.status_code == 200, r.json()
1075+
1076+
1077+
def test_heartbeat(normal_user_client: TestClient, valid_job_id: int):
1078+
search_body = {
1079+
"search": [{"parameter": "JobID", "operator": "eq", "value": valid_job_id}]
1080+
}
1081+
r = normal_user_client.post("/api/jobs/search", json=search_body)
1082+
r.raise_for_status()
1083+
old_data = r.json()[0]
1084+
assert old_data["HeartBeatTime"] is None
1085+
1086+
payload = {valid_job_id: {"Vsize": 1234}}
1087+
r = normal_user_client.patch(
1088+
"/api/jobs/heartbeat", json=payload, params={"force": True}
1089+
)
1090+
r.raise_for_status()
1091+
1092+
r = normal_user_client.post("/api/jobs/search", json=search_body)
1093+
r.raise_for_status()
1094+
new_data = r.json()[0]
1095+
1096+
hbt = datetime.fromisoformat(new_data["HeartBeatTime"])
1097+
# TODO: This should be timezone aware
1098+
assert hbt.tzinfo is None
1099+
hbt = hbt.replace(tzinfo=timezone.utc)
1100+
assert hbt > datetime.now(tz=timezone.utc) - timedelta(seconds=10)

0 commit comments

Comments
 (0)