Skip to content

Commit dbbbce1

Browse files
fix: audit sparsekernel task heartbeats
1 parent c00fac2 commit dbbbce1

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

crates/sparsekernel-cli/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,21 @@ mod tests {
22022202
);
22032203
assert!(empty_claim.is_null());
22042204

2205+
let heartbeat = json_call(
2206+
&mut db,
2207+
"POST",
2208+
"/tasks/heartbeat",
2209+
json!({ "task_id": task_id, "worker_id": "worker-a", "lease_seconds": 120 }),
2210+
);
2211+
assert_eq!(heartbeat["ok"], true);
2212+
let audit = db.list_audit(1).unwrap();
2213+
assert_eq!(audit[0].action, "task.heartbeat");
2214+
assert_eq!(audit[0].actor_id.as_deref(), Some("worker-a"));
2215+
assert_eq!(audit[0].object_id.as_deref(), Some(task_id.as_str()));
2216+
assert!(audit[0].payload.as_ref().unwrap()["leaseUntil"]
2217+
.as_str()
2218+
.is_some());
2219+
22052220
let completed = json_call(
22062221
&mut db,
22072222
"POST",

crates/sparsekernel-core/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,11 +1343,27 @@ impl SparseKernelDb {
13431343
worker_id: &str,
13441344
lease_seconds: i64,
13451345
) -> Result<bool> {
1346+
let now = now_iso();
13461347
let lease_until = future_iso(lease_seconds.max(1));
13471348
let updated = self.conn.execute(
13481349
"UPDATE tasks SET lease_until = ?, updated_at = ? WHERE id = ? AND status = 'running' AND lease_owner = ?",
1349-
params![lease_until, now_iso(), task_id, worker_id],
1350+
params![lease_until, now, task_id, worker_id],
13501351
)?;
1352+
if updated > 0 {
1353+
self.record_task_event(
1354+
task_id,
1355+
"heartbeat",
1356+
json!({ "workerId": worker_id, "leaseUntil": lease_until }),
1357+
)?;
1358+
self.record_audit(AuditInput {
1359+
actor_type: Some("worker".to_string()),
1360+
actor_id: Some(worker_id.to_string()),
1361+
action: "task.heartbeat".to_string(),
1362+
object_type: Some("task".to_string()),
1363+
object_id: Some(task_id.to_string()),
1364+
payload: Some(json!({ "leaseUntil": lease_until })),
1365+
})?;
1366+
}
13511367
Ok(updated > 0)
13521368
}
13531369

0 commit comments

Comments
 (0)