← back to Dear Bubbe Nextjs
app/api/pm2-status/route.ts
46 lines
import { NextRequest, NextResponse } from 'next/server'
import { exec } from 'child_process'
import { promisify } from 'util'
const execAsync = promisify(exec)
export async function GET(request: NextRequest) {
try {
const { stdout } = await execAsync('pm2 jlist')
const processes = JSON.parse(stdout)
const bubbeProcess = processes.find((proc: any) => proc.name === 'bubbe')
if (!bubbeProcess) {
return NextResponse.json({
status: 'error',
details: 'Bubbe process not found'
})
}
const status = bubbeProcess.pm2_env.status
const uptime = bubbeProcess.pm2_env.pm_uptime
const memory = bubbeProcess.pm2_env.axm_monitor?.Memory?.value || '0 MB'
const cpu = bubbeProcess.pm2_env.axm_monitor?.CPU?.value || '0%'
// Calculate uptime in human readable format
const uptimeMs = Date.now() - uptime
const uptimeHours = Math.floor(uptimeMs / (1000 * 60 * 60))
const uptimeMinutes = Math.floor((uptimeMs % (1000 * 60 * 60)) / (1000 * 60))
return NextResponse.json({
status: status === 'online' ? 'online' : 'error',
details: `${uptimeHours}h ${uptimeMinutes}m uptime`,
memory,
cpu,
pid: bubbeProcess.pid,
restarts: bubbeProcess.pm2_env.restart_time
})
} catch (error) {
console.error('Error getting PM2 status:', error)
return NextResponse.json({
status: 'error',
details: 'Failed to get PM2 status'
}, { status: 500 })
}
}