Adding status check for cron job

This commit is contained in:
Collin Duncan 2023-02-08 13:33:42 +01:00
parent 921aa9c65c
commit 4e23288cbd
No known key found for this signature in database
2 changed files with 27 additions and 2 deletions

View file

@ -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 { v4 } from 'uuid'
import { asyncLocalStorage, Logger, LogLevel } from '../common/logger' import { asyncLocalStorage, Logger, LogLevel } from '../common/logger'
import { reserve } from '../common/reserver' import { reserve } from '../common/reserver'
@ -13,6 +13,15 @@ const getTaskConfig = (name: string): ScheduleOptions => ({
const logger = new Logger('cron', 'default', LogLevel.DEBUG) 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 = () => { export const startTasks = () => {
try { try {
if (tasks.length === 0) { if (tasks.length === 0) {

View file

@ -1,7 +1,7 @@
import { IncomingMessage, ServerResponse } from 'http' import { IncomingMessage, ServerResponse } from 'http'
import { asyncLocalStorage as l } from '../../../common/logger' import { asyncLocalStorage as l } from '../../../common/logger'
import { Router } from './index' import { Router } from './index'
import { startTasks, stopTasks } from '../../cron' import { getStatus, startTasks, stopTasks } from '../../cron'
export class CronRouter extends Router { export class CronRouter extends Router {
public async handleRequest( public async handleRequest(
@ -11,6 +11,10 @@ export class CronRouter extends Router {
const { url = '', method } = req const { url = '', method } = req
const [route] = url.split('?') const [route] = url.split('?')
switch (true) { switch (true) {
case /^\/cron\/$/.test(route) && method === 'GET': {
await this.GET_cron(req, res)
break
}
case /^\/cron\/enable$/.test(route) && method === 'POST': { case /^\/cron\/enable$/.test(route) && method === 'POST': {
await this.POST_cron_enable(req, res) await this.POST_cron_enable(req, res)
break break
@ -25,6 +29,18 @@ export class CronRouter extends Router {
} }
} }
private async GET_cron(
_req: IncomingMessage,
res: ServerResponse<IncomingMessage>,
) {
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( private async POST_cron_enable(
_req: IncomingMessage, _req: IncomingMessage,
res: ServerResponse<IncomingMessage>, res: ServerResponse<IncomingMessage>,