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/sage_restart.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { executeCode } from "@cocalc/backend/execute-code";6import { getLogger } from "@cocalc/backend/logger";7import { to_json } from "@cocalc/util/misc";89const winston = getLogger("sage-restart");1011// Wait up to this long for the Sage server to start responding12// connection requests, after we restart it. It can13// take a while, since it pre-imports the sage library14// at startup, before forking.15export const SAGE_SERVER_MAX_STARTUP_TIME_S = 60;1617let restarting = false;18let restarted = 0; // time when we last restarted it1920export async function restart_sage_server() {21const dbg = (m) => winston.debug(`restart_sage_server: ${to_json(m)}`);22if (restarting) {23dbg("hit lock");24throw new Error("already restarting sage server");25}2627const t = Date.now() - restarted;2829if (t <= SAGE_SERVER_MAX_STARTUP_TIME_S * 1000) {30const err = `restarted sage server ${t}ms ago: not allowing too many restarts too quickly...`;31dbg(err);32throw new Error(err);33}3435restarting = true;3637dbg("restarting the daemon");3839try {40const output = await executeCode({41command: "smc-sage-server restart",42timeout: 45,43ulimit_timeout: false, // very important -- so doesn't kill after 30 seconds of cpu!44err_on_exit: true,45bash: true,46});47dbg(48`successfully restarted sage server daemon -- '${JSON.stringify(output)}'`49);50} catch (err) {51const msg = `failed to restart sage server daemon -- ${err}`;52dbg(msg);53throw new Error(msg);54} finally {55restarting = false;56restarted = Date.now();57}58}596061