Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docker heartbeat #35

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 48 additions & 0 deletions heartbeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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) => {
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);
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

if (!healthy) {
clearInterval(currentIntervalId);
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"type": "module",
"files": [
"index.js"
"index.js",
"heartbeat.js"
],
"scripts": {
"prepare:husky": "husky install",
Expand Down
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
Loading