Skip to content

Commit

Permalink
add windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
Courey committed May 10, 2024
1 parent b417c64 commit d58cb47
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions heartbeat.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d58cb47

Please sign in to comment.