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/jupyter/pool/pool-params.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// Parameters for the Jupyter Pool6// They're loaded right after custom env variables are set, such that not only admins of the platform,7// but also users on their own can tune them.89import { join } from "node:path";10import { homedir } from "node:os";11import getLogger from "@cocalc/backend/logger";1213const logger = getLogger("jupyter:pool-params");1415// read env vars with that prefix16const PREFIX = "COCALC_JUPYTER_POOL";17// avoid craziness:18const MAX_POOL_SIZE = 10;1920// the defaults21const CONFIG_FILENAME = `cocalc-jupyter-pool${22process.env.COMPUTE_SERVER_ID ?? ""23}`;24const CONFIG_DIR = join(homedir(), ".config");25const CONFIG = join(CONFIG_DIR, CONFIG_FILENAME);26// size of pool, set to 0 to disable it.27function getPoolSize(): number {28try {29const size = parseInt(process.env.COCALC_JUPYTER_POOL_SIZE ?? "1");30if (!isFinite(size)) {31logger.debug(32"getPoolSize ",33process.env.COCALC_JUPYTER_POOL_SIZE,34" not finite",35);36// disable37return 0;38}39if (size < 0) {40logger.debug(41"getPoolSize ",42process.env.COCALC_JUPYTER_POOL_SIZE,43" negative -- setting to 0",44);45return 0;46}47if (size > MAX_POOL_SIZE) {48return MAX_POOL_SIZE;49}50return size;51} catch (err) {52logger.debug("getPoolSize -- error -- disabling pool", err);53return 0;54}55return 0;56}57const SIZE = getPoolSize();58const TIMEOUT_S = 3600; // after that time, clean up old kernels in the pool59const LAUNCH_DELAY_MS = 7500; // additional delay before spawning an additional kernel6061const PARAMS = {62SIZE,63TIMEOUT_S,64LAUNCH_DELAY_MS,65CONFIG_FILENAME,66CONFIG_DIR,67CONFIG,68};6970export function init() {71// at this point, project-setup::set_extra_env has already been called.72// hence process.env contains global env vars set in init.sh and user specified env vars73const env = process.env;74for (const key in PARAMS) {75// we derive the full path, see end of this function76if (key === "CONFIG") continue;77const varName = `${PREFIX}_${key}`;78if (varName in env) {79const val = env[varName];80if (val === "") continue; // ignore empty values81// if val can be converted to a number, use the integer value82const num = Number(val);83if (!Number.isNaN(num)) {84logger.debug(`setting ${key} to ${num} (converted from '${val}')`);85PARAMS[key] = num;86} else {87logger.debug(`setting ${key} to '${val}'`);88PARAMS[key] = val;89}90}91}92PARAMS.CONFIG = join(PARAMS.CONFIG_DIR, PARAMS.CONFIG_FILENAME);93logger.debug("jupyter kernel pool parameters: ", PARAMS);94}9596export function getSize(): number {97return PARAMS.SIZE;98}99100export function getTimeoutS(): number {101return PARAMS.TIMEOUT_S;102}103104export function getLaunchDelayMS(): number {105return PARAMS.LAUNCH_DELAY_MS;106}107108export function getConfig(): string {109return PARAMS.CONFIG;110}111112export function getConfigDir(): string {113return PARAMS.CONFIG_DIR;114}115116117