its say spawn id exited with code 0.
this is the link i am following - Introduction
Here is the code
const { spawn } = require(‘child_process’);
const { v4: uuidv4 } = require(‘uuid’);
const express = require(‘express’);
class ServerInfo {
constructor(serverId, wsPort, rendererType, response) {
this.serverId = serverId;
this.wsPort = wsPort;
this.response = response;
this.rendererType = rendererType;
this.lastPingTime = null;
this.startTime = new Date();
}
}
class streamCacheServer {
constructor(licenseFile, executablePath, modelDirectory, expressApp) {
this.licenseFile = licenseFile;
this.executablePath = executablePath;
this.modelDirectory = modelDirectory;
this.expressApp = expressApp;
this.serverPort = null;
this.wsBasePort = 55000;
this.serverInfo = null;
this.availableWsPorts = [];
this.activeSpawns = new Map();
}
start(port) {
this.serverPort = port;
this.expressApp.use(express.json());
this.expressApp.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers");
next();
});
this.expressApp.post("/loadModel", this.startViewer.bind(this));
this.expressApp.post("/liveliness/:serverId", this.liveliness.bind(this));
this.expressApp.listen(this.serverPort);
}
startViewer(req, res) {
let rendererType = "csr";
const requestJson = req.body;
if ("rendererType" in requestJson) {
rendererType = requestJson.rendererType;
}
const serverId = uuidv4();
const wsPort = this.availableWsPorts.length ? this.availableWsPorts.pop() : this.wsBasePort++;
this.serverInfo = new ServerInfo(serverId, wsPort, rendererType, res);
this.activeSpawns.set(serverId, this.serverInfo);
let args = [
"--id", this.serverInfo.serverId,
"--sc-port", this.serverInfo.wsPort,
'--liveliness-endpoint', `http://localhost:${this.serverPort}/liveliness`,
"--model-search-directories", this.modelDirectory,
"--license-file", this.licenseFile
];
if (this.serverInfo.rendererType == "ssr") {
args.push("--ssr", "true");
}
const serverOptions = { env: { "DISPLAY": ":0" } };
let process = spawn(this.executablePath, args, serverOptions);
process.on("exit", (code) => {
console.log(`spawn ${this.serverInfo.serverId} exited with code: ${code}`);
});
res.status(200).json({result: `spawned viewer on websocket port: ${this.serverInfo.wsPort}`});
}
liveliness(req, res) {
const serverId = req.params.serverId;
if (!this.serverInfo) {
console.log(`unknown id: ${serverId}`);
res.status(404).json({ error: "invalid serverId." });
return;
}
const updateType = req.query.liveliness;
switch (updateType) {
case "ready":
console.log(`liveliness ready: ${this.serverInfo.serverId}`);
this.serverInfo.response.status(201).json({
directory: this.modelDirectory,
id: this.serverInfo.serverId,
endpoint: `ws://localhost:${this.serverInfo.wsPort}`,
rendererType: this.serverInfo.rendererType
});
this.serverInfo.response = null;
break;
case "disconnect":
console.log(`liveliness disconnect: ${this.serverInfo.serverId}`);
this.availableWsPorts.push(this.serverInfo.wsPort);
this.activeSpawns.delete(this.serverInfo.spawnId);
res.status(200).end();
break;
case "ping":
this.serverInfo.lastPingTime = new Date();
res.status(200).end();
break;
default:
res.status(400).json({ error: "invalid liveliness value" });
}
}
}
module.exports = { streamCacheServer }