batch-cluster
Version:
Manage a cluster of child processes
54 lines • 2.18 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcpsMissingError = void 0;
exports.validateProcpsAvailable = validateProcpsAvailable;
const node_child_process_1 = __importDefault(require("node:child_process"));
const node_fs_1 = require("node:fs");
const Platform_1 = require("./Platform");
/**
* Error thrown when procps is missing on non-Windows systems
*/
class ProcpsMissingError extends Error {
constructor(originalError) {
const message = Platform_1.isWin
? "tasklist command not available"
: "ps command not available. Please install procps package (e.g., 'apt-get install procps' on Ubuntu/Debian)";
super(message);
this.name = "ProcpsMissingError";
if (originalError != null) {
this.originalError = originalError;
}
}
}
exports.ProcpsMissingError = ProcpsMissingError;
/**
* Check if the required process listing command is available
* @throws {ProcpsMissingError} if the command is not available
*/
function validateProcpsAvailable() {
// on POSIX systems with a working /proc we can skip ps entirely
if (!Platform_1.isWin && (0, node_fs_1.existsSync)("/proc")) {
const entries = (0, node_fs_1.readdirSync)("/proc");
// if we see at least one numeric directory, assume /proc is usable
if (entries.some((d) => /^\d+$/.test(d))) {
return;
}
// fall through to check `ps` if /proc is empty or unusable
}
try {
const command = Platform_1.isWin ? "tasklist" : "ps";
const args = Platform_1.isWin ? ["/NH", "/FO", "CSV", "/FI", "PID eq 1"] : ["-p", "1"];
const timeout = Platform_1.isWin ? 15000 : 5000; // 15s for Windows, 5s elsewhere
node_child_process_1.default.execFileSync(command, args, {
stdio: "pipe",
timeout,
});
}
catch (err) {
throw new ProcpsMissingError(err instanceof Error ? err : undefined);
}
}
//# sourceMappingURL=ProcpsChecker.js.map
;