@cocalc/project
Version:
CoCalc: project daemon
49 lines (47 loc) • 1.71 kB
JavaScript
;
/*
Start official upstream Jupyter(Lab) server if necessary, then send a message
to the hub with the port the server is serving on.
This is used by the proxy server to route a certain URL to jupyter(lab).
- socket -- TCP connection between project and hub
- mesg -- the message from the hub requesting the jupyter port.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
const child_process_1 = require("child_process");
const logger_1 = require("@cocalc/project/logger");
const exec = (0, util_1.promisify)(child_process_1.exec);
const winston = (0, logger_1.getLogger)("upstream-jupyter");
async function getPort(lab = false) {
let s = await status(lab);
winston.debug(`jupyter_port, lab=${lab}, status = ${JSON.stringify(s)}`);
if (!s.port || s.status != "running") {
winston.debug("getPort: not running so start");
s = await start(lab);
}
if (!s.port || s.status != "running") {
throw Error(`unable to start jupyter ${lab ? "lab" : "classic"}`);
}
winston.debug(`getPort: started jupyter ${lab ? "lab" : "classic"} at port ${s.port}`);
return s.port;
}
exports.default = getPort;
async function cmd(arg, lab) {
const command = `cc-jupyter${lab ? "lab" : ""} ${arg}`;
winston.debug(command);
return await exec(command);
}
async function start(lab) {
const { stdout } = await cmd("start", lab);
return JSON.parse(stdout);
}
async function status(lab) {
try {
const { stdout } = await cmd("status", lab);
return JSON.parse(stdout);
}
catch (err) {
return { status: "stopped" };
}
}
//# sourceMappingURL=upstream-jupyter.js.map