[workers] Import health check worker

This commit is contained in:
Manav Rathi
2024-06-16 18:35:48 +05:30
parent 9e4412cbee
commit c6f644ef8a
5 changed files with 74 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
/** Proxy file and thumbnail requests for the cast web app */
/** Proxy file and thumbnail requests for the cast web app. */
export default {
async fetch(request: Request) {

View File

@@ -0,0 +1,10 @@
{
"name": "health-check",
"private": true,
"devDependencies": {
"@cloudflare/workers-types": "^4.20240614.0",
"typescript": "^5",
"wrangler": "^3"
},
"packageManager": "yarn@1.22.22"
}

View File

@@ -0,0 +1,45 @@
/** Ping api.ente.io every minute and yell if it doesn't pong. */
export default {
async scheduled(_, env: Env, ctx: ExecutionContext) {
ctx.waitUntil(ping(env));
},
} satisfies ExportedHandler<Env>;
interface Env {
NOTIFY_URL: string;
CHAT_ID: string;
}
const ping = async (env: Env) => {
const notify = async (msg: string) =>
sendMessage(`${msg} on ${Date()}`, env);
try {
let timeout = setTimeout(() => notify("Ping timed out"), 5000);
const res = await fetch("https://api.ente.io/ping", {
headers: {
"User-Agent": "health-check",
},
});
clearTimeout(timeout);
if (!res.ok) await notify(`Ping failed (HTTP ${res.status})`);
} catch (e) {
await notify(`Ping failed (${e instanceof Error ? e.message : e})`);
}
};
const sendMessage = async (message: string, env: Env) => {
console.log(message);
return fetch(env.NOTIFY_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
chat_id: parseInt(env.CHAT_ID),
parse_mode: "html",
text: message,
}),
});
};

View File

@@ -0,0 +1 @@
{ "extends": "../tsconfig.base.json", "include": ["src"] }

View File

@@ -0,0 +1,17 @@
name = "health-check"
main = "src/index.ts"
compatibility_date = "2024-06-14"
[vars]
# Added as a secret via the Cloudflare dashboard
# NOTIFY_URL = ""
# CHAT_ID = ""
# Disable the default route, this worker does not handle fetch.
workers_dev = false
[triggers]
# Every minute
crons = [ "*/1 * * * *" ]
tail_consumers = [{ service = "tail" }]