Files
pfm-ocr/backend/pfm-web-app/test_exec.js
T

88 lines
2.5 KiB
JavaScript

const http = require("http");
function dockerRequest(path, method, body = null) {
return new Promise((resolve, reject) => {
const options = {
socketPath: "/var/run/docker.sock",
path: path,
method: method,
headers: {
"Content-Type": "application/json",
},
};
const req = http.request(options, (res) => {
let data = "";
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
try {
resolve(data ? JSON.parse(data) : null);
} catch (e) {
resolve(data);
}
} else {
reject(new Error(`Docker API Error ${res.statusCode}: ${data}`));
}
});
});
req.on("error", (err) => reject(err));
if (body) {
req.write(JSON.stringify(body));
}
req.end();
});
}
async function runExec(containerName, cmd) {
try {
// 1. Create exec instance
const execConfig = {
AttachStdout: true,
AttachStderr: true,
Cmd: cmd,
};
const createRes = await dockerRequest(`/containers/${containerName}/exec`, "POST", execConfig);
const execId = createRes.Id;
// 2. Start exec instance
// Note: Start API returns raw stream, so we use http.request directly to read it
return new Promise((resolve, reject) => {
const options = {
socketPath: "/var/run/docker.sock",
path: `/exec/${execId}/start`,
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
const req = http.request(options, (res) => {
let output = "";
res.on("data", (chunk) => (output += chunk));
res.on("end", () => {
// Docker multiplexes stdout/stderr in the stream.
// First 8 bytes of each frame contain header info: [stream_type, 0, 0, 0, size1, size2, size3, size4]
// For simple outputs, we can clean up non-printable characters or parse directly
resolve(output);
});
});
req.on("error", (err) => reject(err));
req.write(JSON.stringify({ Detach: false, Tty: false }));
req.end();
});
} catch (err) {
throw err;
}
}
async function main() {
const result = await runExec("paddleocr-vllm-server", ["nvidia-smi", "--query-gpu=index,name,utilization.gpu,utilization.memory,memory.total,memory.used,memory.free,uuid", "--format=csv,noheader,nounits"]);
console.log("Exec output:");
console.log(result);
}
main().catch(console.error);