Skip to content

Commit

Permalink
add docker heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
Courey committed May 8, 2024
1 parent 4d9f879 commit b417c64
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
*.js
index.js
*.tgz
41 changes: 41 additions & 0 deletions heartbeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { exec } from 'child_process';

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 });
}
});
});
}

async function monitorProcess(pid, container) {
try {
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);
}
}

currentIntervalId = setInterval(() => {
monitorProcess(parseInt(pidInput), containerName);
}, 10000); // ping every 10 seconds
process.stdout.write('\u0007')

if (!healthy){
clearInterval(currentIntervalId);
}
24 changes: 24 additions & 0 deletions run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ type SearchEngineLauncherFunction<T = object> = (
waitUntilStopped: () => Promise<void>
}>

async function launchListener({
parentPID,
containerId,
}: {
parentPID: number
containerId: string
}) {
const subprocess = await spawn(
'node',
[
'./node_modules/@nasa-gcn/architect-plugin-search/heartbeat.js',
parentPID.toString(),
containerId,
],
{
detached: true,
stdio: 'ignore',
}
)

subprocess.unref()
}

const launchBinary: SearchEngineLauncherFunction<{ bin: string }> = async ({
bin,
dataDir,
Expand Down Expand Up @@ -86,6 +109,7 @@ const launchDocker: SearchEngineLauncherFunction = async ({
const stream = await container.attach({ stream: true, stderr: true })
stream.pipe(process.stderr)
await container.start()
await launchListener({ parentPID: process.ppid, containerId: container.id })

return {
async kill() {
Expand Down

0 comments on commit b417c64

Please sign in to comment.