-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathworker.js
More file actions
88 lines (65 loc) · 2.32 KB
/
worker.js
File metadata and controls
88 lines (65 loc) · 2.32 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
83
84
85
86
87
88
// init logger
// init components
// create wraper init func
// call plugin with init func
const { workerData, isMainThread } = require("worker_threads");
if (isMainThread) {
process.exit(123);
}
// "enable colors"
process.stdout.isTTY = true;
process.stderr.isTTY = true;
process.env = Object.freeze({ ...process.env });
//require("./system/shared.js");
//require("./system/dispatcher.js");
const logger = require("../../system/logger");
const log = logger.create(`plugins/${workerData.plugin.uuid}`);
const init_db = require("../../system/init/init.database.js")(logger);
const init_components = require("../../system/init/init.components.js")(logger);
const Plugin = require("./class.plugin.js");
const httpServer = require("./class.httpServer.js");
[Plugin, httpServer].forEach((cls) => {
Object.defineProperty(cls, "scope", {
value: {
logger: log,
},
writable: false,
enumerable: false,
configurable: false
});
});
(async () => {
try {
let data = workerData.plugin;
// init database & components
await init_db();
await init_components([
...data.intents,
//"plugins" // without this, class.httpServer.js is not correctly initalized
// fixed with the forEach/Object.defineProperty above. see #588
// https://github.com/OpenHausIO/backend/issues/588
]);
// feedback
log.info("System init done");
let plugin = new Plugin(data);
await plugin.start();
if (process.env.NODE_ENV !== "production") {
setInterval(() => {
let mem = process.memoryUsage();
console.group(Date.now());
console.log(`RSS: ${Number(mem.rss / 1024 / 1024).toFixed(2)} MB`);
console.log(`Heap Used: ${Number(mem.heapUsed / 1024 / 1024).toFixed(2)} MB`);
console.log(`External: ${Number(mem.external / 1024 / 1024).toFixed(2)} MB`);
console.log();
console.groupEnd();
}, 30_000);
}
} catch (err) {
log.error(err, "Could not initalize system");
// without setTimeout, logs are not printed
// drain the event loop/flush stdout, and exit then
setTimeout(() => {
process.exit(1);
});
}
})();