Skip to content

Commit

Permalink
format heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
Courey committed May 10, 2024
1 parent d610321 commit b4d0320
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions heartbeat.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
import { exec } from 'child_process'
import { exec } from "child_process";

const pidInput = process.argv[2]
const containerName = process.argv[3]
let healthy = true
let currentIntervalId
const pidInput = process.argv[2];
const containerName = process.argv[3];
let healthy = true;
let currentIntervalId;

function checkProcessStatus(pid) {
return new Promise((resolve) => {
if (process.platform === 'win32') {
if (process.platform === "win32") {
exec(`tasklist /FI "PID eq ${pid}"`, (err, stdout) => {
if (err) {
resolve({ pid, running: false })
resolve({ pid, running: false });
}
resolve({ pid, running: stdout.includes(`pid: ${pid}`) })
})
resolve({ pid, running: stdout.includes(`pid: ${pid}`) });
});
} else {
exec(`ps -p ${pid}`, (error, stdout) => {
if (error) {
resolve({ pid, running: false })
resolve({ pid, running: false });
} else {
resolve({ pid, running: stdout.includes(pid.toString()) })
resolve({ pid, running: stdout.includes(pid.toString()) });
}
})
});
}
})
});
}

async function monitorProcess(pid, container) {
try {
const status = await checkProcessStatus(pid)
if (!status.running){
exec(`docker stop ${container}`)
healthy = false
process.exit(0)
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
process.stdout.write('\u0007')
monitorProcess(parseInt(pidInput), containerName);
}, 10000); // ping every 10 seconds

if (!healthy){
clearInterval(currentIntervalId)
}
if (!healthy) {
clearInterval(currentIntervalId);
}

0 comments on commit b4d0320

Please sign in to comment.