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/frontend/course/student-projects/run-in-all-projects.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 {
7
start_project,
8
exec,
9
} from "@cocalc/frontend/frame-editors/generic/client";
10
import { map as awaitMap } from "awaiting";
11
import { MAX_PARALLEL_TASKS } from "./actions";
12
13
async function run_in_project(
14
project_id: string,
15
command: string,
16
args?: string[],
17
timeout?: number,
18
): Promise<any> {
19
await start_project(project_id, 60);
20
return await exec({ project_id, command, args, timeout, err_on_exit: false });
21
}
22
23
export type Result = {
24
project_id: string;
25
stdout: string;
26
stderr: string;
27
exit_code: number;
28
timeout?: number;
29
total_time: number;
30
};
31
32
export async function run_in_all_projects(
33
project_ids: string[],
34
command: string,
35
args?: string[],
36
timeout?: number,
37
log?: Function,
38
): Promise<Result[]> {
39
let start = Date.now();
40
const task = async (project_id) => {
41
let result: Result;
42
try {
43
result = {
44
...(await run_in_project(project_id, command, args, timeout)),
45
project_id,
46
timeout,
47
total_time: (Date.now() - start) / 1000,
48
};
49
} catch (err) {
50
result = {
51
project_id,
52
stdout: "",
53
stderr: err.toString(),
54
exit_code: -1,
55
total_time: (Date.now() - start) / 1000,
56
timeout,
57
};
58
}
59
if (log != null) {
60
log(result);
61
}
62
return result;
63
};
64
65
return await awaitMap(project_ids, MAX_PARALLEL_TASKS, task);
66
}
67
68