-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
111 lines (87 loc) · 3.07 KB
/
index.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
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
102
103
104
105
106
107
108
109
110
111
import cluster, { type Worker } from 'node:cluster'
import os from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { secondsToMillis } from '@cityssm/to-millis'
import Debug from 'debug'
import { asyncExitHook } from 'exit-hook'
import schedule from 'node-schedule'
import { getConfigProperty } from './helpers/config.functions.js'
import type { WorkerMessage } from './types/applicationTypes.js'
const debug = Debug(`lot-occupancy-system:www:${process.pid}`)
const directoryName = path.dirname(fileURLToPath(import.meta.url))
process.title = `Faster Web Helper (Primary)`
debug(`Primary pid: ${process.pid}`)
debug(`Primary title: ${process.title}`)
/**
* Initialize module tasks
*/
async function initializeModuleTasks(): Promise<void> {
const promises: Array<Promise<void>> = []
if (getConfigProperty('modules.autocomplete.isEnabled')) {
const initializeAutocompleteModule = await import(
'./modules/autocomplete/initializeAutocompleteModule.js'
)
promises.push(initializeAutocompleteModule.initializeAutocompleteTasks())
}
if (getConfigProperty('modules.inventoryScanner.isEnabled')) {
const initializeInventoryScannerModule = await import(
'./modules/inventoryScanner/initializeInventoryScanner.js'
)
initializeInventoryScannerModule.initializeInventoryScannerTasks()
}
if (getConfigProperty('modules.tempFolderCleanup.isEnabled')) {
const initializeTempFolderCleanupModule = await import(
'./modules/tempFolderCleanup/initializeTempFolderCleanupModule.js'
)
initializeTempFolderCleanupModule.initializeTempFolderCleanupTask()
}
if (getConfigProperty('modules.worktechUpdate.isEnabled')) {
const initializeWorktechUpdateModule = await import(
'./modules/worktechUpdate/initializeWorktechUpdate.js'
)
initializeWorktechUpdateModule.initializeWorktechUpdateTasks()
}
await Promise.all(promises)
asyncExitHook(
async () => {
await schedule.gracefulShutdown()
},
{
wait: secondsToMillis(1)
}
)
}
/**
* Initialize app workers
*/
function initializeAppWorkers(): void {
const processCount = Math.min(os.cpus().length, 2)
debug(`Launching ${processCount} web app processes`)
const clusterSettings = {
exec: `${directoryName}/app/appProcess.js`
}
cluster.setupPrimary(clusterSettings)
const activeWorkers = new Map<number, Worker>()
for (let index = 0; index < processCount; index += 1) {
const worker = cluster.fork()
activeWorkers.set(worker.process.pid ?? 0, worker)
}
cluster.on('message', (worker, message: WorkerMessage) => {
for (const [pid, activeWorker] of activeWorkers.entries()) {
if (pid === message.pid) {
continue
}
debug(`Relaying message to worker: ${pid}`)
activeWorker.send(message)
}
})
cluster.on('exit', (worker) => {
debug(`Worker ${(worker.process.pid ?? 0).toString()} has been killed`)
activeWorkers.delete(worker.process.pid ?? 0)
debug('Starting another worker')
cluster.fork()
})
}
await initializeModuleTasks()
initializeAppWorkers()