-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronmon.ts
executable file
·76 lines (70 loc) · 1.97 KB
/
cronmon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env -S deno run --ext=ts --allow-env --allow-read --allow-net --import-map=deno.json --lock=lock.json
import { dotenv, Application, Context, Router } from "./deps.ts";
await dotenv.load({ export: true });
const router = new Router();
interface Job {
jobId: number;
enabled: boolean;
title: string;
saveResponses: boolean;
url: string;
lastStatus: number; // > 1 means not OK
lastDuration: number;
lastExecution: number;
nextExecution: number;
type: number;
requestTimeout: number;
redirectSuccess: boolean;
folderId: number;
schedule: {
timezone: string;
hours: number[];
mdays: number[];
minutes: number[];
months: number[];
wdays: number[];
expiresAt: number;
};
requestMethod: number;
}
router.get("/", async (context: Context) => {
try {
const resp = await fetch(`${Deno.env.get("CRON_JOB_API_URL")}/jobs`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${Deno.env.get("CRON_JOB_API_KEY")}`,
},
});
const json = await resp.json();
const failedJobs: Job[] = (json["jobs"] as Job[])
.sort((a: Job, b: Job) => a.jobId - b.jobId)
.filter((job: Job) => job.enabled && job.lastStatus > 1);
if (failedJobs.length > 0) {
context.response.status = 504;
context.response.body = {
success: false,
msg: "Some jobs failed",
// deno-lint-ignore no-explicit-any
failedJobs: failedJobs.map((job: { [x: string]: any }) => job.title),
};
return;
}
context.response.body = {
success: true,
msg: "OK",
};
} catch (e) {
context.response.status = 500;
context.response.body = {
success: false,
msg: e,
};
}
});
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
const url = `${Deno.env.get("CRONMON_HOST")}:${Deno.env.get("CRONMON_PORT")}`;
console.log(`Server running on http://${url}`);
app.listen(url);