From 4e23288cbdcf2f0da9bac7b42b66a11d43de1db5 Mon Sep 17 00:00:00 2001 From: Collin Duncan <3679940+cgduncan7@users.noreply.github.com> Date: Wed, 8 Feb 2023 13:33:42 +0100 Subject: [PATCH] Adding status check for cron job --- src/server/cron.ts | 11 ++++++++++- src/server/http/routes/cron.ts | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/server/cron.ts b/src/server/cron.ts index 93673d3..a719427 100644 --- a/src/server/cron.ts +++ b/src/server/cron.ts @@ -1,4 +1,4 @@ -import { schedule, ScheduledTask, ScheduleOptions } from 'node-cron' +import { schedule, ScheduledTask, ScheduleOptions, getTasks as getCronTasks } from 'node-cron' import { v4 } from 'uuid' import { asyncLocalStorage, Logger, LogLevel } from '../common/logger' import { reserve } from '../common/reserver' @@ -13,6 +13,15 @@ const getTaskConfig = (name: string): ScheduleOptions => ({ const logger = new Logger('cron', 'default', LogLevel.DEBUG) +export const getStatus = () => { + const tasks = getCronTasks() + if (tasks.get('reserver cron')) { + return true + } + + return false +} + export const startTasks = () => { try { if (tasks.length === 0) { diff --git a/src/server/http/routes/cron.ts b/src/server/http/routes/cron.ts index 1a869c2..7f4ebab 100644 --- a/src/server/http/routes/cron.ts +++ b/src/server/http/routes/cron.ts @@ -1,7 +1,7 @@ import { IncomingMessage, ServerResponse } from 'http' import { asyncLocalStorage as l } from '../../../common/logger' import { Router } from './index' -import { startTasks, stopTasks } from '../../cron' +import { getStatus, startTasks, stopTasks } from '../../cron' export class CronRouter extends Router { public async handleRequest( @@ -11,6 +11,10 @@ export class CronRouter extends Router { const { url = '', method } = req const [route] = url.split('?') switch (true) { + case /^\/cron\/$/.test(route) && method === 'GET': { + await this.GET_cron(req, res) + break + } case /^\/cron\/enable$/.test(route) && method === 'POST': { await this.POST_cron_enable(req, res) break @@ -25,6 +29,18 @@ export class CronRouter extends Router { } } + private async GET_cron( + _req: IncomingMessage, + res: ServerResponse, + ) { + l.getStore()?.debug('Checking cron status') + const status = getStatus() + res.writeHead(200, undefined, { + 'content-type': 'text/plain', + }) + res.write(status ? 'Enabled' : 'Disabled') + } + private async POST_cron_enable( _req: IncomingMessage, res: ServerResponse,