lastIncident logic improve & index error msg

This commit is contained in:
lyc8503 committed 2023-10-20 02:08:56 +08:00
1 parent a0fb157ae1
commit 93c069c419
5 files changed
+42 -18

No files matched your search

+14 -7
View File
@@ -23,12 +23,11 @@ async function getStatus(monitor: MonitorTarget): Promise<{ ping: number; up: bo
try {
const [hostname, port] = monitor.target.split(":")
// Write "PING" and read response
// Write "PING\n"
const socket = connect({ hostname: hostname, port: Number(port) })
const reader = socket.readable.getReader()
const writer = socket.writable.getWriter()
await writer.write(new TextEncoder().encode("PING\n"))
await reader.read() // The read here is necessary to actually test the connection, however it may hang if the server doesn't respond
await socket.close()
// TODO: should throw an error here but it doesn't?
@@ -109,35 +108,43 @@ export default {
status.up ? state.overallUp++ : state.overallDown++
// Update incidents
let incidentList = state.incident[monitor.id] || []
// Create a dummy incident to store the start time of the monitoring and simplify logic
state.incident[monitor.id] = state.incident[monitor.id] || [
{
start: [currentTimeSecond],
end: currentTimeSecond,
error: ['dummy']
}
]
// Then lastIncident here must not be undefined
const lastIncident = incidentList.slice(-1)[0]
const timeString = new Date().toLocaleString(config.dateLocale, { timeZone: config.timezone })
if (status.up) {
// Current status is up
// close existing incident if any
if (lastIncident && lastIncident.end === undefined) {
if (lastIncident.end === undefined) {
lastIncident.end = currentTimeSecond
await config.callback(`✔️${monitor.name} came back up at ${timeString} after ${Math.round((lastIncident.end - lastIncident.start.slice(-1)[0]) / 60)} minutes of downtime`)
}
} else {
// Current status is down
// open new incident if not already open
if (lastIncident === undefined || lastIncident?.end !== undefined) {
if (lastIncident.end !== undefined) {
incidentList.push({
start: [currentTimeSecond],
end: undefined,
error: [status.err]
})
await config.callback(`❌${monitor.name} went down at ${timeString} with error ${status.err}`)
} else if (lastIncident && lastIncident.end === undefined && lastIncident.error.slice(-1)[0] !== status.err) {
} else if (lastIncident.end === undefined && lastIncident.error.slice(-1)[0] !== status.err) {
// append if the error message changes
lastIncident.start.push(currentTimeSecond)
lastIncident.error.push(status.err)
await config.callback(`❌${monitor.name} is still down at ${timeString} with error ${status.err}`)
}
}
state.incident[monitor.id] = incidentList
// append to latency data
let latencyLists = state.latency[monitor.id] || {
+1 -1
View File
@@ -3,7 +3,7 @@ main = "src/index.ts"
compatibility_date = "2023-05-15"
kv_namespaces = [
{ binding = "UPTIMEFLARE_STATE", id = "30a3733511a246e395ab99ee6a8dceb7" }
{ binding = "UPTIMEFLARE_STATE", id = "ff7ab44866d746aaa19f2225e464ad3e" }
]
[triggers]