CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/compute/manager.ts
Views: 687
1
export const COMPUTER_SERVER_DB_NAME = ".compute-server.syncdb";
2
3
export const SYNCDB_PARAMS = {
4
path: COMPUTER_SERVER_DB_NAME,
5
primary_keys: ["path"],
6
ephemeral: false,
7
cursors: true,
8
};
9
10
export const COMPUTER_SERVER_CURSOR_TYPE = "compute-server";
11
12
// This is relevant for compute servers. It's how long until we give up
13
// on the compute server if it doesn't actively update its cursor state.
14
// Note that in most cases the compute server will explicitly delete its
15
// cursor on termination so switching is instant. This is a "just in case",
16
// so things aren't broken forever, e.g., in case of a crash.
17
export const COMPUTE_THRESH_MS = 15 * 1000;
18
19
/*
20
For sync, we make the id of each compute server a uuid that is a simple
21
function of the id, so the client_id is stable and easy to identify.
22
*/
23
24
const COMPUTER_SERVER_UUID_END = "0000-4000-8000-000000000000";
25
export function isEncodedNumUUID(uuid: string | undefined): boolean {
26
return !!uuid?.endsWith(COMPUTER_SERVER_UUID_END);
27
}
28
29
export function encodeIntToUUID(num: number | string): string {
30
if (typeof num == "string") {
31
num = parseInt(num);
32
}
33
// Convert to hexadecimal
34
let hex = num.toString(16);
35
while (hex.length < 8) {
36
hex = "0" + hex;
37
}
38
return `${hex}-${COMPUTER_SERVER_UUID_END}`;
39
}
40
41
export function decodeUUIDtoNum(uuid: string): number {
42
if (!isEncodedNumUUID(uuid)) {
43
throw Error(`uuid is not an encoded number -- ${uuid}`);
44
}
45
return parseInt(uuid.slice(0, 8), 16);
46
}
47
48