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/redux/run-all-loop.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { delay } from "awaiting";67import { close } from "@cocalc/util/misc";8import { JupyterActions } from "./project-actions";910export class RunAllLoop {11private actions: JupyterActions;12public interval_s: number;13private closed: boolean = false;14private dbg: Function;1516constructor(actions, interval_s) {17this.actions = actions;18this.interval_s = interval_s;19this.dbg = actions.dbg("RunAllLoop");20this.dbg(`interval_s=${interval_s}`);21this.loop();22}2324public set_interval(interval_s: number): void {25if (this.closed) {26throw Error("should not call set_interval if RunAllLoop is closed");27}28if (this.interval_s == interval_s) return;29this.dbg(`.set_interval: interval_s=${interval_s}`);30this.interval_s = interval_s;31}3233private async loop(): Promise<void> {34this.dbg("starting loop...");35while (true) {36if (this.closed) break;37try {38this.dbg("loop: restart");39await this.actions.restart();40} catch (err) {41this.dbg(`restart failed (will try run-all anyways) - ${err}`);42}43if (this.closed) break;44try {45this.dbg("loop: run_all_cells");46await this.actions.run_all_cells(true);47} catch (err) {48this.dbg(`run_all_cells failed - ${err}`);49}50if (this.closed) break;51this.dbg(`loop: waiting ${this.interval_s} seconds`);52await delay(this.interval_s * 1000);53}54this.dbg("terminating loop...");55}5657public close() {58this.dbg("close");59close(this);60this.closed = true;61}62}636465