-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathmain.ts
More file actions
82 lines (71 loc) · 2.31 KB
/
main.ts
File metadata and controls
82 lines (71 loc) · 2.31 KB
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
/*
* main.ts
*
* Utilities for main() functions (setup, cleanup, etc)
*
* Copyright (C) 2022 Posit Software, PBC
*/
import { initializeLogger, logError, logOptions } from "./log.ts";
import { Args } from "flags";
import { parse } from "flags";
import { exitWithCleanup } from "./cleanup.ts";
import {
captureFileReads,
type MetricsKeys,
reportPerformanceMetrics,
} from "./performance/metrics.ts";
import { makeTimedFunctionAsync } from "./performance/function-times.ts";
import { isWindows } from "../deno_ral/platform.ts";
import { convertCombinedLuaProfileToCSV } from "./performance/perfetto-utils.ts";
import { info } from "../deno_ral/log.ts";
type Runner = (args: Args) => Promise<unknown>;
export async function mainRunner(runner: Runner) {
try {
// Parse the raw args to read globals and initialize logging
const args = parse(Deno.args);
await initializeLogger(logOptions(args));
// install termination signal handlers
// Even though windows doesn't technically have signals, Deno
// does the "expected" thing here and calls abend
// on interruption.
Deno.addSignalListener("SIGINT", abend);
if (!isWindows) {
Deno.addSignalListener("SIGTERM", abend);
}
const metricEnv = Deno.env.get("QUARTO_REPORT_PERFORMANCE_METRICS");
if (metricEnv === "true" || metricEnv?.split(",").includes("fileReads")) {
captureFileReads();
}
const main = makeTimedFunctionAsync("main", async () => {
return await runner(args);
});
await main();
// if profiling, wait for 10 seconds before quitting
if (Deno.env.get("QUARTO_TS_PROFILE") !== undefined) {
info("Program finished. Turn off the Chrome profiler now!");
info("Waiting for 10 seconds ...");
await new Promise((resolve) => setTimeout(resolve, 10000));
}
const combinedLuaProfile = Deno.env.get("QUARTO_COMBINED_LUA_PROFILE");
if (combinedLuaProfile) {
convertCombinedLuaProfileToCSV(combinedLuaProfile);
}
if (metricEnv !== undefined) {
if (metricEnv !== "true") {
reportPerformanceMetrics(metricEnv.split(",") as MetricsKeys[]);
} else {
reportPerformanceMetrics();
}
}
exitWithCleanup(0);
} catch (e) {
if (e) {
logError(e);
}
} finally {
abend();
}
}
function abend() {
exitWithCleanup(1);
}