Skip to content

Commit 0c5d84e

Browse files
committed
jupyter kernel pool: COCALC_JUPYTER_POOL_SIZE env variable configures the size between 0 and 10.
- makes it very easy to disable the pool entirely by setting COCALC_JUPYTER_POOL_SIZE to 0, which can be done in project settings - will also make it easy to disable in compute servers if necessary - also use different config file for compute servers, to avoid potential interference
1 parent c193fcd commit 0c5d84e

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

src/packages/jupyter/pool/pool-params.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,59 @@ import { join } from "node:path";
1111
import { homedir } from "node:os";
1212
import getLogger from "@cocalc/backend/logger";
1313

14-
const L = getLogger("jupyter:pool-params").debug;
14+
const logger = getLogger("jupyter:pool-params");
1515

1616
// read env vars with that prefix
1717
const PREFIX = "COCALC_JUPYTER_POOL";
18+
// avoid craziness:
19+
const MAX_POOL_SIZE = 10;
1820

1921
// the defaults
20-
const CONFIG_FN = "cocalc-jupyter-pool";
22+
const CONFIG_FILENAME = `cocalc-jupyter-pool${
23+
process.env.COMPUTE_SERVER_ID ?? ""
24+
}`;
2125
const CONFIG_DIR = join(homedir(), ".config");
22-
const CONFIG = join(CONFIG_DIR, CONFIG_FN);
23-
const SIZE = 1; // size of pool, set to 0 to disable it
26+
const CONFIG = join(CONFIG_DIR, CONFIG_FILENAME);
27+
// size of pool, set to 0 to disable it.
28+
function getPoolSize(): number {
29+
try {
30+
const size = parseInt(process.env.COCALC_JUPYTER_POOL_SIZE ?? "1");
31+
if (!isFinite(size)) {
32+
logger.debug(
33+
"getPoolSize ",
34+
process.env.COCALC_JUPYTER_POOL_SIZE,
35+
" not finite",
36+
);
37+
// disable
38+
return 0;
39+
}
40+
if (size < 0) {
41+
logger.debug(
42+
"getPoolSize ",
43+
process.env.COCALC_JUPYTER_POOL_SIZE,
44+
" negative -- setting to 0",
45+
);
46+
return 0;
47+
}
48+
if (size > MAX_POOL_SIZE) {
49+
return MAX_POOL_SIZE;
50+
}
51+
return size;
52+
} catch (err) {
53+
logger.debug("getPoolSize -- error -- disabling pool", err);
54+
return 0;
55+
}
56+
return 0;
57+
}
58+
const SIZE = getPoolSize();
2459
const TIMEOUT_S = 3600; // after that time, clean up old kernels in the pool
2560
const LAUNCH_DELAY_MS = 7500; // additional delay before spawning an additional kernel
2661

2762
const PARAMS = {
2863
SIZE,
2964
TIMEOUT_S,
3065
LAUNCH_DELAY_MS,
31-
CONFIG_FN,
66+
CONFIG_FILENAME,
3267
CONFIG_DIR,
3368
CONFIG,
3469
};
@@ -47,15 +82,16 @@ export function init() {
4782
// if val can be converted to a number, use the integer value
4883
const num = Number(val);
4984
if (!Number.isNaN(num)) {
50-
L(`setting ${key} to ${num} (converted from '${val}')`);
85+
logger.debug(`setting ${key} to ${num} (converted from '${val}')`);
5186
PARAMS[key] = num;
5287
} else {
53-
L(`setting ${key} to '${val}'`);
88+
logger.debug(`setting ${key} to '${val}'`);
5489
PARAMS[key] = val;
5590
}
5691
}
5792
}
58-
PARAMS.CONFIG = join(PARAMS.CONFIG_DIR, PARAMS.CONFIG_FN);
93+
PARAMS.CONFIG = join(PARAMS.CONFIG_DIR, PARAMS.CONFIG_FILENAME);
94+
logger.debug("jupyter kernel pool parameters: ", PARAMS);
5995
}
6096

6197
export function getSize(): number {

0 commit comments

Comments
 (0)