Improve event timeout with heartbeat_at

This commit is contained in:
Dominic Ferrando
2026-07-18 01:10:03 -04:00
parent c24002eaca
commit e4cb2da9cc
4 changed files with 15 additions and 6 deletions
+7 -3
View File
@@ -89,9 +89,12 @@ export class EventsService {
.execute();
}
async setState(id: string, state: JsonValue) {
async setState(id: string, state: JsonValue, shouldHeartbeat: boolean = true) {
await db.updateTable("events")
.set({ state })
.set((eb) => ({
state,
heartbeat_at: shouldHeartbeat ? new Date() : eb.ref("heartbeat_at")
}))
.where("id", "=", id)
.execute();
}
@@ -125,6 +128,7 @@ export class EventsService {
async retry(id: string): Promise<boolean> {
const result = await db.updateTable("events")
.set({
heartbeat_at: null,
started_at: null,
ended_at: null,
has_failed: false,
@@ -147,7 +151,7 @@ export class EventsService {
}))
.where("started_at", "is not", null)
.where("ended_at", "is", null)
.where("started_at", "<", new Date(Date.now() - EventsService.CONCURRENCY_TIMEOUT_MS))
.where("heartbeat_at", "<", new Date(Date.now() - EventsService.CONCURRENCY_TIMEOUT_MS))
.$if(opt.filter?.type !== undefined, (qb) => qb.where("type", "=", opt.filter!.type!))
.$if(opt.filter?.group !== undefined, (qb) => qb.where("group", "=", opt.filter!.group!))
.returning(["id"])