[workers] Import health check worker
This commit is contained in:
@@ -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) {
|
||||
|
||||
10
infra/workers/health-check/package.json
Normal file
10
infra/workers/health-check/package.json
Normal 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"
|
||||
}
|
||||
45
infra/workers/health-check/src/index.ts
Normal file
45
infra/workers/health-check/src/index.ts
Normal 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,
|
||||
}),
|
||||
});
|
||||
};
|
||||
1
infra/workers/health-check/tsconfig.json
Normal file
1
infra/workers/health-check/tsconfig.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "../tsconfig.base.json", "include": ["src"] }
|
||||
17
infra/workers/health-check/wrangler.toml
Normal file
17
infra/workers/health-check/wrangler.toml
Normal 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" }]
|
||||
Reference in New Issue
Block a user