Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/jupyter/upstream-jupyter.ts
Views: 687
/*1Start official upstream Jupyter(Lab) server if necessary, then send a message2to the hub with the port the server is serving on.34This is used by the proxy server to route a certain URL to jupyter(lab).56- socket -- TCP connection between project and hub7- mesg -- the message from the hub requesting the jupyter port.8*/910import { promisify } from "util";11import { exec as fs_exec } from "child_process";12import { getLogger } from "@cocalc/project/logger";1314const exec = promisify(fs_exec);15const winston = getLogger("upstream-jupyter");1617export default async function getPort(lab: boolean = false): Promise<number> {18let s: { port?: number; status?: string } = await status(lab);19winston.debug(`jupyter_port, lab=${lab}, status = ${JSON.stringify(s)}`);20if (!s.port || s.status != "running") {21winston.debug("getPort: not running so start");22s = await start(lab);23}24if (!s.port || s.status != "running") {25throw Error(`unable to start jupyter ${lab ? "lab" : "classic"}`);26}2728winston.debug(29`getPort: started jupyter ${lab ? "lab" : "classic"} at port ${s.port}`30);31return s.port;32}3334async function cmd(arg: string, lab: boolean) {35const command = `cc-jupyter${lab ? "lab" : ""} ${arg}`;36winston.debug(command);37return await exec(command);38}3940async function start(41lab: boolean42): Promise<{ base: string; pid: number; port: number }> {43const { stdout } = await cmd("start", lab);44return JSON.parse(stdout);45}4647async function status(48lab: boolean49): Promise<{ status: "stopped" | "running"; port?: number }> {50try {51const { stdout } = await cmd("status", lab);52return JSON.parse(stdout);53} catch (err) {54return { status: "stopped" };55}56}575859