-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
56 lines (44 loc) · 1.72 KB
/
main.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
import getEnv from "./functions/src/utils/environmentVariables.ts";
const env = await getEnv();
let fileLoaderStarted: any;
const startFileLoader = async (iter = 0) => {
const fileLoader = new Worker(new URL("./file-loader.ts", import.meta.url).href, { type: "module" });
fileLoader.onmessage = (event) => {
if (event?.data?.message?.status === 'ok') {
return fileLoaderStarted();
}
fileLoader.terminate();
console.error('File Loader Restarting:', event.data.message);
if (iter < maxRestarts) startFileLoader(iter + 1);
}
fileLoader.onerror = (event) => {
console.error('Error in File Loader Worker:', event.message);
if (iter < maxRestarts) startFileLoader(iter + 1);
};
};
const startApi = async (iter = 0) => {
const fileLoaderUrl = env.FILE_LOADER_URL || "http://localhost:9000";
const apiBaseUrl = env.DEFAULT_LOADER_TYPE === 'local' && !import.meta.url.startsWith('http') ? fileLoaderUrl : import.meta.url
const api = new Worker(new URL("./api.ts", apiBaseUrl), { type: "module" });
api.onmessage = (event) => {
if (event?.data?.message?.status === 'ok') {
return
}
console.log('API Restarting:', event.data.message);
if (env.ENV !== 'production') {
api.terminate();
if (iter < maxRestarts) startApi(iter + 1);
}
}
api.onerror = (event) => {
console.error('Error in API Worker:', event.message);
if (iter < maxRestarts) startApi(iter + 1);
};
};
const waitForFileLoader = new Promise((resolve) => {
startFileLoader();
fileLoaderStarted = resolve;
return;
});
waitForFileLoader.then(() => startApi());
const maxRestarts = 100;