mirror of
https://github.com/Xevion/bus-reminder.git
synced 2025-12-07 01:14:34 -06:00
Reformat monitor.ts, add logging calls
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { env } from "@/env/server.mjs";
|
||||
import { z } from "zod";
|
||||
import { env } from '@/env/server.mjs';
|
||||
import { z } from 'zod';
|
||||
import logger from '@/logger';
|
||||
|
||||
const BASE_URL = `https://cronitor.link/p/${env.CRONITOR_ACCOUNT_ID}/${env.CRONITOR_JOB_ID}`;
|
||||
const parameterSchema = z.object({
|
||||
@@ -10,12 +11,12 @@ const parameterSchema = z.object({
|
||||
.union([
|
||||
z.object({ count: z.number() }),
|
||||
z.object({ duration: z.number() }),
|
||||
z.object({ error_count: z.number() }),
|
||||
z.object({ error_count: z.number() })
|
||||
])
|
||||
.optional(),
|
||||
series: z.string().optional(),
|
||||
state: z.enum(["run", "complete", "fail", "ok"]).optional(),
|
||||
status_code: z.number().int().optional(),
|
||||
state: z.enum(['run', 'complete', 'fail', 'ok']).optional(),
|
||||
status_code: z.number().int().optional()
|
||||
});
|
||||
|
||||
function buildURL(parameters: z.infer<typeof parameterSchema>) {
|
||||
@@ -28,7 +29,7 @@ function buildURL(parameters: z.infer<typeof parameterSchema>) {
|
||||
host,
|
||||
message,
|
||||
series,
|
||||
state,
|
||||
state
|
||||
}).filter(([, value]) => value != undefined))
|
||||
url.searchParams.append(key, value!);
|
||||
|
||||
@@ -39,11 +40,11 @@ function buildURL(parameters: z.infer<typeof parameterSchema>) {
|
||||
duration?: number;
|
||||
error_count?: number;
|
||||
};
|
||||
if (count != undefined) url.searchParams.append("metric", `count:${count}`);
|
||||
if (count != undefined) url.searchParams.append('metric', `count:${count}`);
|
||||
else if (duration != undefined)
|
||||
url.searchParams.append("metric", `duration:${duration}`);
|
||||
url.searchParams.append('metric', `duration:${duration}`);
|
||||
else if (error_count != undefined)
|
||||
url.searchParams.append("metric", `error_count:${error_count}`);
|
||||
url.searchParams.append('metric', `error_count:${error_count}`);
|
||||
}
|
||||
|
||||
return url;
|
||||
@@ -67,8 +68,8 @@ export default async function monitorAsync<T = any>(
|
||||
// Tell Cronitor that the job is running
|
||||
const start = new Date();
|
||||
const series = start.getTime().toString();
|
||||
await fetch(buildURL({ state: "run", series }).toString());
|
||||
console.log("Cronitor: Job started");
|
||||
await fetch(buildURL({ state: 'run', series }).toString());
|
||||
logger.info('Cronitor: Job started');
|
||||
|
||||
// Execute the function, provide try/catch
|
||||
let result: T | undefined = undefined;
|
||||
@@ -78,20 +79,20 @@ export default async function monitorAsync<T = any>(
|
||||
const duration = getDuration(start, new Date());
|
||||
await fetch(
|
||||
buildURL({
|
||||
state: "fail",
|
||||
state: 'fail',
|
||||
series,
|
||||
message: error instanceof Error ? error.message : undefined,
|
||||
metric: { duration },
|
||||
metric: { duration }
|
||||
}).toString()
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Tell Cronitor that the job is complete (success)
|
||||
console.log("Cronitor: Job completed");
|
||||
logger.info('Cronitor: Job completed');
|
||||
const duration = getDuration(start, new Date());
|
||||
await fetch(
|
||||
buildURL({ state: "complete", series, metric: { duration } }).toString()
|
||||
buildURL({ state: 'complete', series, metric: { duration } }).toString()
|
||||
);
|
||||
|
||||
return result as T;
|
||||
|
||||
Reference in New Issue
Block a user