-
Notifications
You must be signed in to change notification settings - Fork 17
/
backendHarness.js
101 lines (90 loc) · 2.52 KB
/
backendHarness.js
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
const http = require("http");
const { spawn, exec, execSync } = require("child_process");
// Run a command against a fresh local backend, handling setting up and tearing down the backend.
// Checks for a local backend running on port 3210.
const parsedUrl = new URL("http://127.0.0.1:3210");
async function isBackendRunning(backendUrl) {
return new Promise((resolve) => {
http
.request(
{
hostname: backendUrl.hostname,
port: backendUrl.port,
path: "/version",
method: "GET",
},
(res) => {
resolve(res.statusCode === 200);
},
)
.on("error", () => {
resolve(false);
})
.end();
});
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const waitForLocalBackendRunning = async (backendUrl) => {
let isRunning = await isBackendRunning(backendUrl);
let i = 0;
while (!isRunning) {
if (i % 10 === 0) {
// Progress messages every ~5 seconds
console.log("Waiting for backend to be running...");
}
await sleep(500);
isRunning = await isBackendRunning(backendUrl);
i += 1;
}
return;
};
let backendProcess = null;
function cleanup() {
if (backendProcess !== null) {
console.log("Cleaning up running backend");
backendProcess.kill("SIGTERM");
execSync("just reset-local-backend");
}
}
async function runWithLocalBackend(command, backendUrl) {
const isRunning = await isBackendRunning(backendUrl);
if (isRunning) {
console.error(
"Looks like local backend is already running. Cancel it and restart this command.",
);
process.exit(1);
}
execSync("just reset-local-backend");
backendProcess = exec("CONVEX_TRACE_FILE=1 just run-local-backend");
await waitForLocalBackendRunning(backendUrl);
console.log("Backend running! Logs can be found in convex-local-backend.log");
const innerCommand = new Promise((resolve) => {
const c = spawn(command, {
shell: true,
stdio: "pipe",
env: { ...process.env, FORCE_COLOR: true },
});
c.stdout.on("data", (data) => {
process.stdout.write(data);
});
c.stderr.on("data", (data) => {
process.stderr.write(data);
});
c.on("exit", (code) => {
console.log("inner command exited with code " + code.toString());
resolve(code);
});
});
return innerCommand;
}
runWithLocalBackend(process.argv[2], parsedUrl)
.then((code) => {
cleanup();
process.exit(code);
})
.catch(() => {
cleanup();
process.exit(1);
});