chore: initial project import
Some checks failed
CI - Production Readiness / Verify (push) Has been cancelled
Some checks failed
CI - Production Readiness / Verify (push) Has been cancelled
This commit is contained in:
52
lib/job-alerts.ts
Normal file
52
lib/job-alerts.ts
Normal file
@ -0,0 +1,52 @@
|
||||
type CampaignRetryFailureAlert = {
|
||||
jobName: string;
|
||||
runId: string;
|
||||
error: string;
|
||||
runAt: string;
|
||||
tenantId?: string;
|
||||
campaignId?: string;
|
||||
processedCampaigns: number;
|
||||
totalAttempted: number;
|
||||
totalFailed: number;
|
||||
};
|
||||
|
||||
function normalizeWebhookEnabled() {
|
||||
const raw = process.env.CAMPAIGN_RETRY_ALERT_WEBHOOK_URL?.trim();
|
||||
return raw ? raw : "";
|
||||
}
|
||||
|
||||
function normalizeBoolean(value: string | undefined) {
|
||||
if (!value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ["1", "true", "yes", "on"].includes(value.trim().toLowerCase());
|
||||
}
|
||||
|
||||
export async function notifyCampaignRetryFailure(payload: CampaignRetryFailureAlert) {
|
||||
const webhookUrl = normalizeWebhookEnabled();
|
||||
if (!webhookUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const shouldNotify = normalizeBoolean(process.env.CAMPAIGN_RETRY_ALERT_ON_FAILURE);
|
||||
if (!shouldNotify) {
|
||||
return;
|
||||
}
|
||||
|
||||
const message = {
|
||||
event: "campaign_retry_job_failed",
|
||||
...payload,
|
||||
timestamp: new Date().toISOString()
|
||||
};
|
||||
|
||||
try {
|
||||
await fetch(webhookUrl, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(message)
|
||||
});
|
||||
} catch {
|
||||
// observability channel should not block retries
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user