[WIP] terraform cloudflare provider v5 works! more performance/migration/deployment work todo

This commit is contained in:
lyc8503 committed 2025-12-30 23:19:18 +08:00
1 parent 8295edd35d
commit 45d86424b2
7 files changed
+115 -107

No files matched your search

+8 -8
View File
@@ -3,10 +3,12 @@ import { MonitorState, MonitorTarget } from '../../types/config'
import { maintenances, workerConfig } from '../../uptime.config'
import { getStatus, getStatusWithGlobalPing } from './monitor'
import { formatStatusChangeNotification, getWorkerLocation, webhookNotify } from './util'
import { getFromStore, setToStore } from './store'
export interface Env {
UPTIMEFLARE_STATE: KVNamespace
REMOTE_CHECKER_DO: DurableObjectNamespace<RemoteChecker>
UPTIMEFLARE_D1: D1Database
}
const Worker = {
@@ -62,18 +64,16 @@ const Worker = {
}
// Read state, set init state if it doesn't exist
let state =
((await env.UPTIMEFLARE_STATE.get('state', {
type: 'json',
})) as unknown as MonitorState) ||
({
version: 1,
let state = JSON.parse(await getFromStore(env, 'state') || '{}') as unknown as MonitorState
if (!state || Object.keys(state).length === 0) {
state = {
lastUpdate: 0,
overallUp: 0,
overallDown: 0,
incident: {},
latency: {},
} as MonitorState)
}
}
state.overallDown = 0
state.overallUp = 0
@@ -340,7 +340,7 @@ const Worker = {
) {
console.log('Updating state...')
state.lastUpdate = currentTimeSecond
await env.UPTIMEFLARE_STATE.put('state', JSON.stringify(state))
await setToStore(env, 'state', JSON.stringify(state))
} else {
console.log('Skipping state update due to cooldown period.')
}
+17
View File
@@ -0,0 +1,17 @@
import { Env } from ".";
export async function getFromStore(env: Env, key: string): Promise<string | null> {
const stmt = env.UPTIMEFLARE_D1.prepare("SELECT value FROM uptimeflare WHERE key = ?")
const result = await stmt.bind(key).first<{ value: string }>()
if (!result) {
return await env.UPTIMEFLARE_STATE.get(key)
}
return result?.value || null
}
export async function setToStore(env: Env, key: string, value: string): Promise<void> {
const stmt = env.UPTIMEFLARE_D1.prepare("INSERT INTO uptimeflare (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value;")
await stmt.bind(key, value).run()
}