import { promisify } from "util" ; import { exec as fs_exec } from "child_process" ; import { getLogger } from "@cocalc/project/logger" ; const exec = promisify ( fs_exec ) ; const winston = getLogger ( "upstream-jupyter" ) ; export default 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 ; } 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" } ; } }