mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
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:
@@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}.`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user