feat(exec): add executeCommandInheritStdio and use it for nginx config test

- Add spawn and SpawnOptions imports and implement Execute.executeCommandInheritStdio
  that runs commands with inherited stdio, logs errors, and rejects on non-zero exit.
- Update NginxConfigurator to run `nginx -t -c /etc/nginx/nginx.conf` via the new
  inherit-stdio helper before reloading nginx.
This commit is contained in:
Nawaz Dhandala
2025-11-06 19:41:01 +00:00
parent 28073ba819
commit bf6e97c35d
2 changed files with 54 additions and 3 deletions

View File

@@ -1,5 +1,12 @@
import { PromiseRejectErrorFunction } from "../../Types/FunctionTypes";
import { ExecException, ExecOptions, exec, execFile } from "node:child_process";
import {
ExecException,
ExecOptions,
SpawnOptions,
exec,
execFile,
spawn,
} from "node:child_process";
import logger from "./Logger";
import CaptureSpan from "./Telemetry/CaptureSpan";
@@ -79,4 +86,42 @@ export default class Execute {
},
);
}
@CaptureSpan()
public static executeCommandInheritStdio(data: {
command: string;
args?: Array<string>;
options?: SpawnOptions;
}): Promise<void> {
return new Promise((resolve: VoidFunction, reject: PromiseRejectErrorFunction) => {
const spawnOptions: SpawnOptions = {
stdio: ["ignore", "inherit", "inherit"],
shell: false,
...data.options,
};
const child = spawn(data.command, data.args ?? [], spawnOptions);
child.on("error", (err: Error) => {
logger.error(
`Error executing command: ${data.command} ${(data.args ?? []).join(" ")}`,
);
logger.error(err);
reject(err);
});
child.on("close", (code: number | null) => {
if (code === 0) {
resolve();
return;
}
const error: Error = new Error(
`Command failed: ${data.command} ${(data.args ?? []).join(" ")} (exit code ${code ?? "unknown"})`,
);
logger.error(error);
reject(error);
});
});
}
}

View File

@@ -89,8 +89,14 @@ export default class NginxConfigurator {
}
await this.ensureLogFiles();
await Exec.executeCommand("nginx -t");
await Exec.executeCommand("nginx -s reload");
await Exec.executeCommandInheritStdio({
command: "nginx",
args: ["-t", "-c", "/etc/nginx/nginx.conf"],
});
await Exec.executeCommandInheritStdio({
command: "nginx",
args: ["-s", "reload"],
});
logger.info(
`[NginxConfigurator] Reloaded nginx after updating certificate for ${normalizedHost}.`,
);