diff --git a/apps/web/src/app/api/checker/cron/10m/route.ts b/apps/web/src/app/api/checker/cron/10m/route.ts index 6c4cef273..f3088d3d5 100644 --- a/apps/web/src/app/api/checker/cron/10m/route.ts +++ b/apps/web/src/app/api/checker/cron/10m/route.ts @@ -1,8 +1,8 @@ -import * as Sentry from "@sentry/nextjs"; import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; import { cron, isAuthorizedDomain } from "../_cron"; +import { runSentryCron } from "../_sentry"; export const runtime = "nodejs"; // export const preferredRegion = ["auto"]; @@ -12,18 +12,13 @@ export const revalidate = 0; export async function GET(req: NextRequest) { if (isAuthorizedDomain(req.url)) { - const checkInId = Sentry.captureCheckIn({ - monitorSlug: "10-min-cron", - status: "in_progress", - }); - - await cron({ periodicity: "10m", req }); - - Sentry.captureCheckIn({ - checkInId, - monitorSlug: "10-min-cron", - status: "ok", - }); + const { cronCompleted, cronFailed } = runSentryCron("10-m-cron"); + try { + await cron({ periodicity: "10m", req }); + await cronCompleted(); + } catch (error) { + await cronFailed(); + } } return NextResponse.json({ success: true }); } diff --git a/apps/web/src/app/api/checker/cron/1h/route.ts b/apps/web/src/app/api/checker/cron/1h/route.ts index c4a72541d..c710f1772 100644 --- a/apps/web/src/app/api/checker/cron/1h/route.ts +++ b/apps/web/src/app/api/checker/cron/1h/route.ts @@ -1,8 +1,8 @@ -import * as Sentry from "@sentry/nextjs"; import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; import { cron, isAuthorizedDomain } from "../_cron"; +import { runSentryCron } from "../_sentry"; export const runtime = "nodejs"; // export const preferredRegion = ["auto"]; @@ -12,16 +12,13 @@ export const revalidate = 0; export async function GET(req: NextRequest) { if (isAuthorizedDomain(req.url)) { - const checkInId = Sentry.captureCheckIn({ - monitorSlug: "1-h-cron", - status: "in_progress", - }); - await cron({ periodicity: "1h", req }); - Sentry.captureCheckIn({ - checkInId, - monitorSlug: "1-h-cron", - status: "ok", - }); + const { cronCompleted, cronFailed } = runSentryCron("1-h-cron"); + try { + await cron({ periodicity: "1h", req }); + await cronCompleted(); + } catch (error) { + await cronFailed(); + } } return NextResponse.json({ success: true }); } diff --git a/apps/web/src/app/api/checker/cron/1m/route.ts b/apps/web/src/app/api/checker/cron/1m/route.ts index df0dbb7db..3f216eaff 100644 --- a/apps/web/src/app/api/checker/cron/1m/route.ts +++ b/apps/web/src/app/api/checker/cron/1m/route.ts @@ -1,8 +1,8 @@ -import * as Sentry from "@sentry/nextjs"; import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; import { cron, isAuthorizedDomain } from "../_cron"; +import { runSentryCron } from "../_sentry"; export const runtime = "nodejs"; // export const preferredRegion = ["auto"]; @@ -12,16 +12,13 @@ export const revalidate = 0; export async function GET(req: NextRequest) { if (isAuthorizedDomain(req.url)) { - const checkInId = Sentry.captureCheckIn({ - monitorSlug: "1-min-cron", - status: "in_progress", - }); - await cron({ periodicity: "1m", req }); - Sentry.captureCheckIn({ - checkInId, - monitorSlug: "1-min-cron", - status: "ok", - }); + const { cronCompleted, cronFailed } = runSentryCron("1-m-cron"); + try { + await cron({ periodicity: "1m", req }); + await cronCompleted(); + } catch (error) { + await cronFailed(); + } } return NextResponse.json({ success: true }); } diff --git a/apps/web/src/app/api/checker/cron/30m/route.ts b/apps/web/src/app/api/checker/cron/30m/route.ts index 10de2183d..e07fdc782 100644 --- a/apps/web/src/app/api/checker/cron/30m/route.ts +++ b/apps/web/src/app/api/checker/cron/30m/route.ts @@ -1,8 +1,8 @@ -import * as Sentry from "@sentry/nextjs"; import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; import { cron, isAuthorizedDomain } from "../_cron"; +import { runSentryCron } from "../_sentry"; export const runtime = "nodejs"; // export const preferredRegion = ["auto"]; @@ -12,16 +12,13 @@ export const revalidate = 0; export async function GET(req: NextRequest) { if (isAuthorizedDomain(req.url)) { - const checkInId = Sentry.captureCheckIn({ - monitorSlug: "30-min-cron", - status: "in_progress", - }); - await cron({ periodicity: "30m", req }); - Sentry.captureCheckIn({ - checkInId, - monitorSlug: "30-min-cron", - status: "ok", - }); + const { cronCompleted, cronFailed } = runSentryCron("30-m-cron"); + try { + await cron({ periodicity: "30m", req }); + await cronCompleted(); + } catch (error) { + await cronFailed(); + } } return NextResponse.json({ success: true }); } diff --git a/apps/web/src/app/api/checker/cron/30s/route.ts b/apps/web/src/app/api/checker/cron/30s/route.ts index b4af19922..3105a32d9 100644 --- a/apps/web/src/app/api/checker/cron/30s/route.ts +++ b/apps/web/src/app/api/checker/cron/30s/route.ts @@ -1,8 +1,8 @@ -import * as Sentry from "@sentry/nextjs"; import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; import { cron, isAuthorizedDomain } from "../_cron"; +import { runSentryCron } from "../_sentry"; export const runtime = "nodejs"; // export const preferredRegion = ["auto"]; @@ -12,16 +12,13 @@ export const revalidate = 0; export async function GET(req: NextRequest) { if (isAuthorizedDomain(req.url)) { - const checkInId = Sentry.captureCheckIn({ - monitorSlug: "30-s-cron", - status: "in_progress", - }); - await cron({ periodicity: "30s", req }); - Sentry.captureCheckIn({ - checkInId, - monitorSlug: "30-s-cron", - status: "ok", - }); + const { cronCompleted, cronFailed } = runSentryCron("30-s-cron"); + try { + await cron({ periodicity: "30s", req }); + await cronCompleted(); + } catch (error) { + await cronFailed(); + } } return NextResponse.json({ success: true }); } diff --git a/apps/web/src/app/api/checker/cron/5m/route.ts b/apps/web/src/app/api/checker/cron/5m/route.ts index d4a301303..1e65e22c8 100644 --- a/apps/web/src/app/api/checker/cron/5m/route.ts +++ b/apps/web/src/app/api/checker/cron/5m/route.ts @@ -1,8 +1,8 @@ -import * as Sentry from "@sentry/nextjs"; import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; import { cron, isAuthorizedDomain } from "../_cron"; +import { runSentryCron } from "../_sentry"; export const runtime = "nodejs"; // export const preferredRegion = ["auto"]; @@ -12,16 +12,13 @@ export const revalidate = 0; export async function GET(req: NextRequest) { if (isAuthorizedDomain(req.url)) { - const checkInId = Sentry.captureCheckIn({ - monitorSlug: "5-min-cron", - status: "in_progress", - }); - await cron({ periodicity: "5m", req }); - Sentry.captureCheckIn({ - checkInId, - monitorSlug: "5-min-cron", - status: "ok", - }); + const { cronCompleted, cronFailed } = runSentryCron("5-m-cron"); + try { + await cron({ periodicity: "5m", req }); + await cronCompleted(); + } catch (error) { + await cronFailed(); + } } return NextResponse.json({ success: true }); } diff --git a/apps/web/src/app/api/checker/cron/_sentry.ts b/apps/web/src/app/api/checker/cron/_sentry.ts new file mode 100644 index 000000000..67b992ffc --- /dev/null +++ b/apps/web/src/app/api/checker/cron/_sentry.ts @@ -0,0 +1,30 @@ +// Props: https://github.com/getsentry/sentry-javascript/issues/9335#issuecomment-1779057528 +import { captureCheckIn, flush } from "@sentry/nextjs"; + +export function runSentryCron(monitorSlug: string) { + // 🟡 Notify Sentry your job is running: + const checkInId = captureCheckIn({ + monitorSlug, + status: "in_progress", + }); + return { + cronCompleted: async () => { + // 🟢 Notify Sentry your job has completed successfully: + captureCheckIn({ + checkInId, + monitorSlug, + status: "ok", + }); + return flush(); + }, + cronFailed: async () => { + // 🔴 Notify Sentry your job has failed: + captureCheckIn({ + checkInId, + monitorSlug, + status: "error", + }); + return flush(); + }, + }; +} diff --git a/apps/web/src/components/monitor-charts/utils.tsx b/apps/web/src/components/monitor-charts/utils.tsx index f38174286..e465edcf6 100644 --- a/apps/web/src/components/monitor-charts/utils.tsx +++ b/apps/web/src/components/monitor-charts/utils.tsx @@ -67,7 +67,7 @@ export function groupDataByTimestamp( */ export function renderTimestamp(timestamp: number, _period: Period) { const date = new Date(timestamp); - return format(date, "yyyy MMM d, HH:mm"); + return date.toISOString(); } export function dataFormatter(number: number) {