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