DurableObject remote checker, binding TODO

pull/106/head
lyc8503 2025-04-14 00:33:51 +08:00
parent 44594ca320
commit f02aef8d00
1 changed files with 35 additions and 7 deletions

View File

@ -2,9 +2,11 @@ import { workerConfig } from '../../uptime.config'
import { formatStatusChangeNotification, getWorkerLocation, notifyWithApprise } from './util' import { formatStatusChangeNotification, getWorkerLocation, notifyWithApprise } from './util'
import { MonitorState, MonitorTarget } from '../../uptime.types' import { MonitorState, MonitorTarget } from '../../uptime.types'
import { getStatus } from './monitor' import { getStatus } from './monitor'
import { DurableObject } from 'cloudflare:workers'
export interface Env { export interface Env {
UPTIMEFLARE_STATE: KVNamespace UPTIMEFLARE_STATE: KVNamespace
REMOTE_CHECKER_DO: DurableObjectNamespace<RemoteChecker>
} }
export default { export default {
@ -77,16 +79,26 @@ export default {
let status let status
if (monitor.checkProxy) { if (monitor.checkProxy) {
// Initiate a check using proxy (Geo-specific check) // Initiate a check using proxy (Geo-specific monitoring)
try { try {
console.log('Calling check proxy: ' + monitor.checkProxy) console.log('Calling check proxy: ' + monitor.checkProxy)
const resp = await ( let resp
await fetch(monitor.checkProxy, { if (monitor.checkProxy.startsWith("worker://")) {
method: 'POST', const doLoc = monitor.checkProxy.replace("worker://", "")
headers: { 'Content-Type': 'application/json' }, const doId = env.REMOTE_CHECKER_DO.idFromName(doLoc)
body: JSON.stringify(monitor), const doStub = env.REMOTE_CHECKER_DO.get(doId, {
locationHint: doLoc as DurableObjectLocationHint
}) })
).json<{ location: string; status: { ping: number; up: boolean; err: string } }>() resp = await doStub.getLocationAndStatus(monitor)
} else {
resp = await (
await fetch(monitor.checkProxy, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(monitor),
})
).json<{location: string; status: {ping: number; up: boolean; err: string}}>()
}
checkLocation = resp.location checkLocation = resp.location
status = resp.status status = resp.status
} catch (err) { } catch (err) {
@ -299,3 +311,19 @@ export default {
} }
}, },
} }
export class RemoteChecker extends DurableObject {
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env)
}
async getLocationAndStatus(monitor: MonitorTarget): Promise<{location: string; status: {ping: number; up: boolean; err: string}}> {
const colo = await getWorkerLocation() as string
console.log(`Running remote checker (DurableObject) at ${colo}...`)
const status = await getStatus(monitor)
return {
location: colo,
status: status,
}
}
}