From d58cb4779bf38f251ee1af5522b1d16642b21e24 Mon Sep 17 00:00:00 2001 From: Courey Elliott Date: Fri, 10 May 2024 10:49:42 -0400 Subject: [PATCH] add windows support --- heartbeat.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/heartbeat.js b/heartbeat.js index b28dbee..9eefb2c 100644 --- a/heartbeat.js +++ b/heartbeat.js @@ -1,41 +1,49 @@ -import { exec } from 'child_process'; +import { exec } from 'child_process' -const pidInput = process.argv[2]; +const pidInput = process.argv[2] const containerName = process.argv[3] let healthy = true let currentIntervalId function checkProcessStatus(pid) { return new Promise((resolve) => { - exec(`ps -p ${pid}`, (error, stdout) => { - if (error) { - resolve({ pid, running: false }); - } else { - const processRunning = stdout.includes(pid.toString()); - resolve({ pid, running: processRunning }); - } - }); - }); + if (process.platform === 'win32') { + exec(`tasklist /FI "PID eq ${pid}"`, (err, stdout) => { + if (err) { + resolve({ pid, running: false }) + } + resolve({ pid, running: stdout.includes(`pid: ${pid}`) }) + }) + } else { + exec(`ps -p ${pid}`, (error, stdout) => { + if (error) { + resolve({ pid, running: false }) + } else { + resolve({ pid, running: stdout.includes(pid.toString()) }) + } + }) + } + }) } async function monitorProcess(pid, container) { try { - const status = await checkProcessStatus(pid); + const status = await checkProcessStatus(pid) if (!status.running){ exec(`docker stop ${container}`) healthy = false process.exit(0) } } catch (error) { - console.error('Error checking process status:', error); + console.error('Error checking process status:', error) } } currentIntervalId = setInterval(() => { - monitorProcess(parseInt(pidInput), containerName); -}, 10000); // ping every 10 seconds + monitorProcess(parseInt(pidInput), containerName) +}, 10000) // ping every 10 seconds process.stdout.write('\u0007') if (!healthy){ - clearInterval(currentIntervalId); + clearInterval(currentIntervalId) } \ No newline at end of file