feat: D1 upgrade: deployment and migration process DONE!

This commit is contained in:
lyc8503 committed 2026-01-03 23:06:32 +08:00
1 parent 0258ae1ddc
commit 3de3640d17
12 files changed
+278 -76

No files matched your search

+6 -13
View File
@@ -17,9 +17,7 @@ const Worker = {
console.log(`Running scheduled event on ${workerLocation}...`)
// Create a wrapped MonitorState from stored compacted state
const state = new CompactedMonitorStateWrapper(
await getFromStore(env, 'state')
)
const state = new CompactedMonitorStateWrapper(await getFromStore(env, 'state'))
state.data.overallDown = 0
state.data.overallUp = 0
@@ -147,10 +145,7 @@ const Worker = {
error: [status.err],
})
monitorStatusChanged = true
} else if (
lastIncident.end === null &&
lastIncident.error.slice(-1)[0] !== status.err
) {
} else if (lastIncident.end === null && lastIncident.error.slice(-1)[0] !== status.err) {
// append if the error message changes
lastIncident.start.push(currentTimeSecond)
lastIncident.error.push(status.err)
@@ -160,10 +155,7 @@ const Worker = {
monitorStatusChanged = true
}
const currentIncident = state.getIncident(
monitor.id,
state.incidentLen(monitor.id) - 1
)
const currentIncident = state.getIncident(monitor.id, state.incidentLen(monitor.id) - 1)
try {
if (
// monitor status changed AND...
@@ -240,7 +232,7 @@ const Worker = {
}
// append to latency data
state.appendLatency(monitor.id,{
state.appendLatency(monitor.id, {
loc: checkLocation,
ping: status.ping,
time: currentTimeSecond,
@@ -283,7 +275,8 @@ const Worker = {
// Allow for a cooldown period before writing to storage
if (
statusChanged ||
currentTimeSecond - state.data.lastUpdate >= (workerConfig.kvWriteCooldownMinutes ?? 3) * 60 - 10 // Allow for 10 seconds of clock drift
currentTimeSecond - state.data.lastUpdate >=
(workerConfig.kvWriteCooldownMinutes ?? 3) * 60 - 10 // Allow for 10 seconds of clock drift
) {
console.log('Updating state...')
state.data.lastUpdate = currentTimeSecond
+19 -7
View File
@@ -1,5 +1,10 @@
import { Env } from '.'
import { IncidentRecord, LatencyRecord, MonitorState, MonitorStateCompacted } from '../../types/config'
import {
IncidentRecord,
LatencyRecord,
MonitorState,
MonitorStateCompacted,
} from '../../types/config'
export async function getFromStore(env: Env, key: string): Promise<string | null> {
const stmt = env.UPTIMEFLARE_D1.prepare('SELECT value FROM uptimeflare WHERE key = ?')
@@ -50,8 +55,13 @@ export class CompactedMonitorStateWrapper {
state.incident[monitorId] = []
const incidents = this.data.incident[monitorId]
if (incidents.start.length !== incidents.end.length || incidents.start.length !== incidents.error.length) {
throw new Error('Inconsistent incident data lengths, please report an issue at https://github.com/lyc8503/UptimeFlare')
if (
incidents.start.length !== incidents.end.length ||
incidents.start.length !== incidents.error.length
) {
throw new Error(
'Inconsistent incident data lengths, please report an issue at https://github.com/lyc8503/UptimeFlare'
)
}
for (let i = 0; i < incidents.start.length; i++) {
@@ -79,7 +89,9 @@ export class CompactedMonitorStateWrapper {
const pingArr = new Uint16Array(Uint8Array.fromHex(latencies.ping).buffer)
if (timeArr.length !== pingArr.length || timeArr.length !== locUncompacted.length) {
throw new Error('Inconsistent latency data lengths, please report an issue at https://github.com/lyc8503/UptimeFlare.')
throw new Error(
'Inconsistent latency data lengths, please report an issue at https://github.com/lyc8503/UptimeFlare.'
)
}
for (let i = 0; i < timeArr.length; i++) {
@@ -155,7 +167,7 @@ export class CompactedMonitorStateWrapper {
latencyLen(monitorId: string): number {
const latencies = this.data.latency[monitorId]
if (!latencies) return 0
return latencies.ping.length / 4 // Uint16Array, 4 characters per entry in hex
return latencies.ping.length / 4 // Uint16Array, 4 characters per entry in hex
}
appendLatency(monitorId: string, record: LatencyRecord) {
@@ -172,7 +184,7 @@ export class CompactedMonitorStateWrapper {
}
latencies = this.data.latency[monitorId]
}
// @ts-expect-error
latencies.time += new Uint8Array(new Uint32Array([record.time]).buffer).toHex()
// @ts-expect-error
@@ -212,7 +224,7 @@ export class CompactedMonitorStateWrapper {
unshiftLatency(monitorId: string) {
let latencies = this.data.latency[monitorId]
latencies.time = latencies.time.slice(8)
latencies.ping = latencies.ping.slice(4)
+1 -4
View File
@@ -157,9 +157,7 @@ const formatAndNotify = async (
// Skip notification if monitor is in the skip list
const skipList = workerConfig.notification?.skipNotificationIds
if (skipList && skipList.includes(monitor.id)) {
console.log(
`Skipping notification for ${monitor.name} (${monitor.id} in skipNotificationIds)`
)
console.log(`Skipping notification for ${monitor.name} (${monitor.id} in skipNotificationIds)`)
return
}
@@ -193,7 +191,6 @@ const formatAndNotify = async (
}
}
export {
getWorkerLocation,
fetchTimeout,