update: health check info

This commit is contained in:
Ben Allfree 2024-01-23 12:02:34 +00:00
parent a3e4a52c53
commit cb68bc3251

View File

@ -76,6 +76,7 @@ type Check = {
port?: number
ago?: string
mem?: string
created?: Date
}
const containers = _exec(`docker ps --format '{{json .}}'`)
@ -87,18 +88,19 @@ const containers = _exec(`docker ps --format '{{json .}}'`)
.map((line) => JSON.parse(line) as DockerPs)
.map<Check>((rec) => {
const name = rec.Names.replace(/-\d+/, '')
const port = parseInt(rec.Ports.match(/:(\d+)/)?.[1] || '0', 10)
const mem = rec.Size.match(/(\d+MB)/)?.[1] || '0MB'
const created = new Date(rec.CreatedAt)
return {
name,
priority: 0,
emoji: ':guitar:',
emoji: ':octopus:',
port,
isHealthy: false,
url: `http://localhost:${port}/api/health`,
ago: rec.RunningFor,
mem,
created,
}
})
@ -150,10 +152,7 @@ function getFreeMemoryInGB(): string {
return freeMemoryGB.toFixed(2) // Rounds to 2 decimal places
}
await fetch(DISCORD_URL, {
method: 'POST',
body: JSON.stringify({
content: [
const content = [
`===================`,
`Server: SFO-1`,
`${new Date()}`,
@ -164,15 +163,56 @@ await fetch(DISCORD_URL, {
.sort((a, b) => {
if (a.priority > b.priority) return -1
if (a.priority < b.priority) return 1
const now = new Date()
return +(b.created || now) - +(a.created || now)
return a.name.localeCompare(b.name)
})
.map(
({ name, isHealthy, emoji, mem, ago }) =>
`${
.map(({ name, isHealthy, emoji, mem, ago }) => {
const isInstance = !!mem
if (isInstance) {
return `${
isHealthy ? ':white_check_mark:' : ':face_vomiting: '
} ${emoji} ${name} ${mem || ''} ${ago || ''}`,
),
].join(`\n`),
} ${emoji} \`${name.padStart(20)} ${(mem || '').padStart(10)} ${
ago || ''
}\``
}
return `${
isHealthy ? ':white_check_mark:' : ':face_vomiting: '
} ${emoji} ${name}`
}),
]
function splitIntoChunks(lines: string[], maxChars: number = 2000): string[] {
const chunks: string[] = []
let currentChunk: string = ''
lines.forEach((line) => {
// Check if adding the next line exceeds the maxChars limit
if (currentChunk.length + line.length + 1 > maxChars) {
chunks.push(currentChunk)
currentChunk = ''
}
currentChunk += line + '\n' // Add the line and a newline character
})
// Add the last chunk if it's not empty
if (currentChunk) {
chunks.push(currentChunk)
}
return chunks
}
dbg(content)
await Promise.all(
splitIntoChunks(content).map(async (content) => {
await fetch(DISCORD_URL, {
method: 'POST',
body: JSON.stringify({
content,
}),
headers: { 'content-type': 'application/json' },
})
})
}),
)