fix: handle notification endpoint failures
This commit is contained in:
@@ -26,6 +26,10 @@ function isCronAuthorized(request: Request) {
|
|||||||
return authHeader === `Bearer ${secret}` || secretHeader === secret;
|
return authHeader === `Bearer ${secret}` || secretHeader === secret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getErrorMessage(error: unknown) {
|
||||||
|
return error instanceof Error ? error.message : "Unknown notification check error.";
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
if (!isCronAuthorized(request)) {
|
if (!isCronAuthorized(request)) {
|
||||||
return NextResponse.json({ error: "Unauthorized." }, { status: 401 });
|
return NextResponse.json({ error: "Unauthorized." }, { status: 401 });
|
||||||
@@ -34,44 +38,51 @@ export async function GET(request: Request) {
|
|||||||
return NextResponse.json({ error: "Web Push is not configured." }, { status: 503 });
|
return NextResponse.json({ error: "Web Push is not configured." }, { status: 503 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const now = Date.now();
|
try {
|
||||||
const warnings = (await fetchMeteoWarnings(AbortSignal.timeout(12_000))).filter((warning) => isRelevantWarning(warning, now));
|
const now = Date.now();
|
||||||
const subscriptions = getPushSubscriptions().filter((subscription) => subscription.enabled);
|
const warnings = (await fetchMeteoWarnings(AbortSignal.timeout(12_000))).filter((warning) => isRelevantWarning(warning, now));
|
||||||
const activeWarningIds = new Set(warnings.map((warning) => warning.id));
|
const subscriptions = getPushSubscriptions().filter((subscription) => subscription.enabled);
|
||||||
let sent = 0;
|
const activeWarningIds = new Set(warnings.map((warning) => warning.id));
|
||||||
let skipped = 0;
|
let sent = 0;
|
||||||
let failed = 0;
|
let skipped = 0;
|
||||||
|
let failed = 0;
|
||||||
|
|
||||||
pruneSentWarnings(activeWarningIds);
|
pruneSentWarnings(activeWarningIds);
|
||||||
|
|
||||||
for (const subscription of subscriptions) {
|
for (const subscription of subscriptions) {
|
||||||
const matchingWarnings = warnings.filter((warning) => (
|
const matchingWarnings = warnings.filter((warning) => (
|
||||||
subscription.countyTeryt ? warningMatchesCounty(warning, subscription.countyTeryt) : warning.provinces.includes(subscription.province)
|
subscription.countyTeryt ? warningMatchesCounty(warning, subscription.countyTeryt) : warning.provinces.includes(subscription.province)
|
||||||
));
|
));
|
||||||
for (const warning of matchingWarnings) {
|
for (const warning of matchingWarnings) {
|
||||||
if (hasSentWarning(subscription.endpoint, warning.id)) {
|
if (hasSentWarning(subscription.endpoint, warning.id)) {
|
||||||
skipped += 1;
|
skipped += 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sendWarningNotification(subscription, warning);
|
await sendWarningNotification(subscription, warning);
|
||||||
markWarningSent(subscription.endpoint, warning.id);
|
markWarningSent(subscription.endpoint, warning.id);
|
||||||
sent += 1;
|
sent += 1;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
failed += 1;
|
failed += 1;
|
||||||
const statusCode = typeof error === "object" && error !== null && "statusCode" in error ? Number(error.statusCode) : null;
|
const statusCode = typeof error === "object" && error !== null && "statusCode" in error ? Number(error.statusCode) : null;
|
||||||
if (statusCode === 404 || statusCode === 410) removePushSubscription(subscription.endpoint);
|
if (statusCode === 404 || statusCode === 410) removePushSubscription(subscription.endpoint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
ok: true,
|
ok: true,
|
||||||
subscriptions: subscriptions.length,
|
subscriptions: subscriptions.length,
|
||||||
warnings: warnings.length,
|
warnings: warnings.length,
|
||||||
sent,
|
sent,
|
||||||
skipped,
|
skipped,
|
||||||
failed,
|
failed,
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
return NextResponse.json({
|
||||||
|
error: "Unable to check IMGW meteorological warnings.",
|
||||||
|
details: getErrorMessage(error),
|
||||||
|
}, { status: 502 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,11 @@ export async function GET(request: Request) {
|
|||||||
&& Number.isFinite(subscription.latitude)
|
&& Number.isFinite(subscription.latitude)
|
||||||
&& Number.isFinite(subscription.longitude)
|
&& Number.isFinite(subscription.longitude)
|
||||||
));
|
));
|
||||||
const warnings = await fetchMeteoWarnings(AbortSignal.timeout(12_000));
|
let warningsUnavailable = false;
|
||||||
|
const warnings = await fetchMeteoWarnings(AbortSignal.timeout(12_000)).catch(() => {
|
||||||
|
warningsUnavailable = true;
|
||||||
|
return [];
|
||||||
|
});
|
||||||
let sent = 0;
|
let sent = 0;
|
||||||
let skipped = 0;
|
let skipped = 0;
|
||||||
let failed = 0;
|
let failed = 0;
|
||||||
@@ -82,6 +86,7 @@ export async function GET(request: Request) {
|
|||||||
ok: true,
|
ok: true,
|
||||||
date: dateKey,
|
date: dateKey,
|
||||||
subscriptions: subscriptions.length,
|
subscriptions: subscriptions.length,
|
||||||
|
warningsUnavailable,
|
||||||
sent,
|
sent,
|
||||||
skipped,
|
skipped,
|
||||||
failed,
|
failed,
|
||||||
|
|||||||
Reference in New Issue
Block a user