-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
54 lines (45 loc) · 1.16 KB
/
main.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
const path = require('path')
const { Worker } = require('worker_threads')
const { sendMessageFactory } = require('./utils')
// If one of these throws an error, we exit
const criticalScripts = [
'access.js',
'gpio.js',
'rfid.js',
]
const scripts = [
...criticalScripts,
'display.js',
'mqtt.js',
'slack.js',
'thermometer.js',
'check-link.js',
'check-ping.js',
]
const workers = []
let deadWorkers = 0
function broadcastMessage(message) {
const dtFmt = new Date().toJSON()
console.error(`[${dtFmt}] ${JSON.stringify(message)}`)
for (const w of workers) {
w.postMessage(message)
}
}
for (const script of scripts) {
const worker = new Worker(path.resolve(`workers/${script}`), {
trackUnmanagedFds: true,
})
workers.push(worker)
worker.once('error', (error) => {
console.error(`Error from ${script}:`, error)
if (criticalScripts.includes(script)) {
process.exit(1)
return
}
deadWorkers++
broadcastMessage({module: 'main', topic: 'dead-worker-count', value: deadWorkers})
})
}
for (const worker of workers) {
worker.on('message', broadcastMessage)
}