/
eval.ts
304 lines (273 loc) · 7.94 KB
/
eval.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { execFile } from 'child_process';
import fs from 'fs';
import circularSafeStringify from 'json-stringify-safe';
import { EOL } from 'os';
import path from 'path';
import { file as makeTmpFile } from 'tmp-promise';
import { Cancelled, EVAL_ERRORS, NoResultError } from '../../shared/errors';
import log from '../../shared/log';
import { newId } from '../../shared/object';
import { PanelBody } from '../../shared/rpc';
import { ConnectorInfo, PanelInfo, PanelResult } from '../../shared/state';
import {
CODE_ROOT,
DISK_ROOT,
DSPROJ_FLAG,
FS_BASE_FLAG,
PANEL_FLAG,
PANEL_META_FLAG,
SETTINGS_FILE_FLAG,
} from '../constants';
import { ensureFile } from '../fs';
import { parsePartialJSONFile } from '../partial';
import { Dispatch, RPCHandler } from '../rpc';
import { getProjectResultsFile } from '../store';
import { getProjectAndPanel } from './shared';
const runningProcesses: Record<string, Set<number>> = {};
const cancelledPids = new Set<number>();
function killAllByPanelId(panelId: string) {
const workers = runningProcesses[panelId];
if (workers) {
Array.from(workers).map((pid) => {
try {
log.info('Killing existing process');
process.kill(pid, 'SIGINT');
cancelledPids.add(pid);
} catch (e) {
// If process doesn't exist, that's ok
if (!e.message.includes('ESRCH')) {
throw e;
}
}
});
}
}
export async function evalInSubprocess(
subprocess: {
node: string;
go?: string;
settingsFileOverride?: string;
},
projectName: string,
panel: PanelInfo,
connectors: ConnectorInfo[]
): Promise<[Partial<PanelResult>, string]> {
const tmp = await makeTmpFile({ prefix: 'resultmeta-' });
let pid = 0;
try {
// This means only one user can run a panel at a time
killAllByPanelId(panel.id);
let base = process.argv[0];
const args = [
subprocess.node,
DSPROJ_FLAG,
projectName,
PANEL_FLAG,
panel.id,
PANEL_META_FLAG,
tmp.path,
FS_BASE_FLAG,
DISK_ROOT.value,
];
if (subprocess.settingsFileOverride) {
args.push(SETTINGS_FILE_FLAG, subprocess.settingsFileOverride);
}
if (subprocess.go) {
base = subprocess.go;
args.shift();
}
// Only run during tests, triggering Go code coverage
// https://blog.cloudflare.com/go-coverage-with-external-tests/
if (subprocess.go && subprocess.go.includes('_test')) {
ensureFile(path.join(CODE_ROOT, 'coverage', 'fake.cov'));
args.unshift('-test.run');
args.unshift('^TestRunMain$');
args.unshift('-test.coverprofile=coverage/gorunner.' + newId() + '.cov');
}
log.info(`Launching "${base} ${args.join(' ')}"`);
const child = execFile(base, args, {
windowsHide: true,
});
pid = child.pid;
if (!runningProcesses[panel.id]) {
runningProcesses[panel.id] = new Set();
}
runningProcesses[panel.id].add(pid);
let stderr = '';
child.stderr.on('data', (data) => {
// Can't find any way to suppress this error appearing in Node processes.
if (data.includes('stream/web is an experimental feature.')) {
return;
}
stderr += data;
process.stderr.write(data);
});
child.stdout.on('data', (data) => {
stderr += data;
process.stdout.write(data);
});
await new Promise<void>((resolve, reject) => {
try {
child.on('exit', (code) => {
if (code === 0) {
if (stderr) {
process.stderr.write(stderr + EOL);
}
resolve();
return;
}
if (cancelledPids.has(pid)) {
cancelledPids.delete(pid);
reject(new Cancelled());
return;
}
reject(new Error(stderr));
});
} catch (e) {
if (stderr) {
process.stderr.write(stderr + EOL);
}
reject(e);
}
});
const resultMeta = JSON.parse(fs.readFileSync(tmp.path).toString());
let parsePartial = typeof resultMeta.preview === 'undefined';
if (!parsePartial) {
const rm: Partial<PanelResult> = resultMeta;
// Case of existing Node.js runner
return [rm, stderr];
}
// Case of new Go runner
const projectResultsFile = getProjectResultsFile(projectName);
let rm: Partial<PanelResult>;
try {
rm = parsePartialJSONFile(projectResultsFile + panel.id);
} catch (e) {
if (!e.stdout) {
e.stdout = resultMeta.stdout;
}
if (e instanceof NoResultError && resultMeta.exception) {
// do nothing. The underlying exception is more interesting
} else {
throw e;
}
}
// Table and graph panels get their results passed back to me displayed in the UI
if (['table', 'graph'].includes(panel.type)) {
const projectResultsFile = getProjectResultsFile(projectName);
const bytes = fs.readFileSync(projectResultsFile + panel.id);
rm.value = JSON.parse(bytes.toString());
}
return [{ ...rm, ...resultMeta }, stderr];
} finally {
try {
if (pid) {
runningProcesses[panel.id].delete(pid);
}
tmp.cleanup();
} catch (e) {
log.error(e);
}
}
}
async function evalNoUpdate(
projectId: string,
body: PanelBody,
dispatch: Dispatch,
subprocessEval?: {
node: string;
go?: string;
}
): Promise<[Partial<PanelResult>, string]> {
const { project, panel } = await getProjectAndPanel(
dispatch,
projectId,
body.panelId
);
// Reset the result
await dispatch({
resource: 'updatePanelResult',
projectId,
body: { data: new PanelResult(), panelId: panel.id },
});
if (!subprocessEval) {
throw new Error('Developer error: all eval must use subprocess');
}
return evalInSubprocess(
subprocessEval,
project.projectName,
panel,
project.connectors
);
}
export const makeEvalHandler = (subprocessEval?: {
node: string;
go?: string;
}): RPCHandler<PanelBody, PanelResult> => ({
resource: 'eval',
handler: async function (
projectId: string,
body: PanelBody,
dispatch: Dispatch
): Promise<PanelResult> {
// Clear out existing results. This isn't ideal but harder to do correctness without
const projectResultsFile = getProjectResultsFile(projectId);
try {
fs.unlinkSync(projectResultsFile + body.panelId);
} catch (e) {
/* ignore */
}
let stderr = '';
const start = new Date();
let res: Partial<PanelResult> = { loading: true };
try {
[res, stderr] = await evalNoUpdate(
projectId,
body,
dispatch,
subprocessEval
);
} catch (e) {
log.error(e);
res.exception = e;
if (res.exception.stdout) {
res.stdout = res.exception.stdout;
delete res.exception.stdout;
}
}
if (
res.exception &&
!EVAL_ERRORS.find((ee) => ee.name === res.exception.name) &&
stderr
) {
// Just a generic exception, we already caught all info in `stderr`, so just store that.
res.exception = new Error(res.stdout || stderr);
}
// I'm not really sure why this is necessary but sometimes the
// exception comes out in an object that can't be stringified. And
// yet I don't believe there's any throwing of errors from the Go
// code.
if (res.exception && circularSafeStringify(res.exception) === '{}') {
res.exception = {
name: res.exception.name,
message: res.exception.message,
...res.exception,
};
}
res.lastRun = start;
res.loading = false;
res.elapsed = new Date().valueOf() - start.valueOf();
await dispatch({
resource: 'updatePanelResult',
projectId,
body: { data: res, panelId: body.panelId },
});
return Object.assign(new PanelResult(), res);
},
});
export const killProcessHandler: RPCHandler<PanelBody, void> = {
resource: 'killProcess',
handler: async function (_: string, body: PanelBody) {
killAllByPanelId(body.panelId);
},
};