From 24b4b678a87136ebdb64c9a051c1a3abc877cc1f Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 5 Sep 2023 10:43:04 -0500 Subject: [PATCH] Reformat monitor.ts, add logging calls --- src/monitor.ts | 141 +++++++++++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 70 deletions(-) diff --git a/src/monitor.ts b/src/monitor.ts index ad68c8d..b68d7fe 100644 --- a/src/monitor.ts +++ b/src/monitor.ts @@ -1,59 +1,60 @@ -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({ - env: z.string().optional(), - host: z.string().optional(), - message: z.string().optional(), - metric: z - .union([ - z.object({ count: z.number() }), - z.object({ duration: 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(), + env: z.string().optional(), + host: z.string().optional(), + message: z.string().optional(), + metric: z + .union([ + z.object({ count: z.number() }), + z.object({ duration: 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() }); function buildURL(parameters: z.infer) { - const url = new URL(BASE_URL); + const url = new URL(BASE_URL); - // Definite string properties - const { env, host, message, series, state } = parameters; - for (const [key, value] of Object.entries({ - env, - host, - message, - series, - state, - }).filter(([, value]) => value != undefined)) - url.searchParams.append(key, value!); + // Definite string properties + const { env, host, message, series, state } = parameters; + for (const [key, value] of Object.entries({ + env, + host, + message, + series, + state + }).filter(([, value]) => value != undefined)) + url.searchParams.append(key, value!); - // Extract potentially undefined specified properties - if (parameters.metric != undefined) { - const { count, duration, error_count } = parameters.metric as { - count?: number; - duration?: number; - error_count?: number; - }; - if (count != undefined) url.searchParams.append("metric", `count:${count}`); - else if (duration != undefined) - url.searchParams.append("metric", `duration:${duration}`); - else if (error_count != undefined) - url.searchParams.append("metric", `error_count:${error_count}`); - } + // Extract potentially undefined specified properties + if (parameters.metric != undefined) { + const { count, duration, error_count } = parameters.metric as { + count?: number; + duration?: number; + error_count?: number; + }; + if (count != undefined) url.searchParams.append('metric', `count:${count}`); + else if (duration != undefined) + url.searchParams.append('metric', `duration:${duration}`); + else if (error_count != undefined) + url.searchParams.append('metric', `error_count:${error_count}`); + } - return url; + return url; } /** * @returns The duration in seconds between the start and end dates */ function getDuration(start: Date, end: Date) { - return (end.getTime() - start.getTime()) / 1000; + return (end.getTime() - start.getTime()) / 1000; } /** @@ -62,37 +63,37 @@ function getDuration(start: Date, end: Date) { * @throws Any error thrown by the function will be rethrown. Cronitor will be notified of the failure. */ export default async function monitorAsync( - execute: () => Promise + execute: () => Promise ): Promise { - // 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"); + // Tell Cronitor that the job is running + const start = new Date(); + const series = start.getTime().toString(); + await fetch(buildURL({ state: 'run', series }).toString()); + logger.info('Cronitor: Job started'); - // Execute the function, provide try/catch - let result: T | undefined = undefined; - try { - result = await execute(); - } catch (error) { - const duration = getDuration(start, new Date()); - await fetch( - buildURL({ - state: "fail", - series, - message: error instanceof Error ? error.message : undefined, - metric: { duration }, - }).toString() - ); - throw error; - } + // Execute the function, provide try/catch + let result: T | undefined = undefined; + try { + result = await execute(); + } catch (error) { + const duration = getDuration(start, new Date()); + await fetch( + buildURL({ + state: 'fail', + series, + message: error instanceof Error ? error.message : undefined, + metric: { duration } + }).toString() + ); + throw error; + } - // Tell Cronitor that the job is complete (success) - console.log("Cronitor: Job completed"); - const duration = getDuration(start, new Date()); - await fetch( - buildURL({ state: "complete", series, metric: { duration } }).toString() - ); + // Tell Cronitor that the job is complete (success) + logger.info('Cronitor: Job completed'); + const duration = getDuration(start, new Date()); + await fetch( + buildURL({ state: 'complete', series, metric: { duration } }).toString() + ); - return result as T; + return result as T; }