-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |