fix: catch nitro exit process for a clearer message

This commit is contained in:
Louis 2023-11-22 21:49:23 +07:00
parent 8f76b7c3e0
commit d0c44f70f1
2 changed files with 40 additions and 36 deletions

View File

@ -45,8 +45,6 @@ function initModel(modelFile: string): Promise<InitModelResponse> {
.then(checkAndUnloadNitro) .then(checkAndUnloadNitro)
// 2. Spawn the Nitro subprocess // 2. Spawn the Nitro subprocess
.then(spawnNitroProcess) .then(spawnNitroProcess)
// 3. Wait until the port is used (Nitro http server is up)
.then(() => tcpPortUsed.waitUntilUsed(PORT, 300, 30000))
// 4. Load the model into the Nitro subprocess (HTTP POST request) // 4. Load the model into the Nitro subprocess (HTTP POST request)
.then(loadLLMModel) .then(loadLLMModel)
// 5. Check if the model is loaded successfully // 5. Check if the model is loaded successfully
@ -165,47 +163,53 @@ function checkAndUnloadNitro() {
* Using child-process to spawn the process * Using child-process to spawn the process
* Should run exactly platform specified Nitro binary version * Should run exactly platform specified Nitro binary version
*/ */
function spawnNitroProcess() { async function spawnNitroProcess(): Promise<void> {
let binaryFolder = path.join(__dirname, "nitro"); // Current directory by default return new Promise((resolve, reject) => {
let binaryName; let binaryFolder = path.join(__dirname, "nitro"); // Current directory by default
let binaryName;
if (process.platform === "win32") { if (process.platform === "win32") {
// Todo: Need to check for CUDA support to switch between CUDA and non-CUDA binaries // Todo: Need to check for CUDA support to switch between CUDA and non-CUDA binaries
binaryName = "win-start.bat"; binaryName = "win-start.bat";
} else if (process.platform === "darwin") { } else if (process.platform === "darwin") {
// Mac OS platform // Mac OS platform
if (process.arch === "arm64") { if (process.arch === "arm64") {
binaryFolder = path.join(binaryFolder, "mac-arm64"); binaryFolder = path.join(binaryFolder, "mac-arm64");
} else {
binaryFolder = path.join(binaryFolder, "mac-x64");
}
binaryName = "nitro";
} else { } else {
binaryFolder = path.join(binaryFolder, "mac-x64"); // Linux
// Todo: Need to check for CUDA support to switch between CUDA and non-CUDA binaries
binaryName = "linux-start.sh"; // For other platforms
} }
binaryName = "nitro";
} else {
// Linux
// Todo: Need to check for CUDA support to switch between CUDA and non-CUDA binaries
binaryName = "linux-start.sh"; // For other platforms
}
const binaryPath = path.join(binaryFolder, binaryName); const binaryPath = path.join(binaryFolder, binaryName);
// Execute the binary // Execute the binary
subprocess = spawn(binaryPath, [1, "0.0.0.0", PORT], { subprocess = spawn(binaryPath, [1, "0.0.0.0", PORT], {
cwd: binaryFolder, cwd: binaryFolder,
}); });
// Handle subprocess output // Handle subprocess output
subprocess.stdout.on("data", (data) => { subprocess.stdout.on("data", (data) => {
console.log(`stdout: ${data}`); console.log(`stdout: ${data}`);
}); });
subprocess.stderr.on("data", (data) => { subprocess.stderr.on("data", (data) => {
log.error("subprocess error:" + data.toString()); log.error("subprocess error:" + data.toString());
console.error(`stderr: ${data}`); console.error(`stderr: ${data}`);
}); });
subprocess.on("close", (code) => { subprocess.on("close", (code) => {
console.log(`child process exited with code ${code}`); console.log(`child process exited with code ${code}`);
subprocess = null; subprocess = null;
reject(`Nitro process exited. ${code ?? ""}`);
});
tcpPortUsed.waitUntilUsed(PORT, 300, 30000).then(() => {
resolve();
});
}); });
} }

View File

@ -49,7 +49,7 @@ export function useActiveModel() {
console.debug('Init model: ', modelId) console.debug('Init model: ', modelId)
const path = join('models', model.name, modelId) const path = join('models', model.name, modelId)
const res = await initModel(path) const res = await initModel(path)
if (res?.error && modelId === activeModel?.id) { if (res?.error && (!activeModel?.id || modelId === activeModel?.id)) {
const errorMessage = `${res.error}` const errorMessage = `${res.error}`
alert(errorMessage) alert(errorMessage)
setStateModel(() => ({ setStateModel(() => ({