feat(triggers): start alert workflow after consecutive cron/http failures

Co-authored-by: Nasyarobby Putra <nasyarobby@gmail.com>
This commit is contained in:
Cursor Agent
2026-08-16 23:45:45 +00:00
co-authored by nsrb
parent 989bf5d487
commit 0c416506d6
8 changed files with 380 additions and 8 deletions
+28
View File
@@ -250,6 +250,34 @@ export async function pruneOlderThan(days) {
return db("workflow_runs").where("started_at", "<", cutoff).del();
}
/**
* @param {string} workflow
* @param {string} triggerType
* @param {string | null | undefined} triggerDetail
*/
export async function countConsecutiveFailures(workflow, triggerType, triggerDetail) {
let q = db("workflow_runs")
.select("status")
.where({ workflow, trigger_type: triggerType })
.whereIn("status", ["success", "failed"])
.orderBy("started_at", "desc")
.limit(100);
if (triggerDetail == null || triggerDetail === "") {
q = q.whereNull("trigger_detail");
} else {
q = q.where("trigger_detail", triggerDetail);
}
const rows = await q;
let count = 0;
for (const row of rows) {
if (row.status === "failed") count += 1;
else break;
}
return count;
}
/**
* @returns {Promise<Record<string, { invocationCount: number, lastInvokedAt: string | null, lastStatus: string | null }>>}
*/