-
Notifications
You must be signed in to change notification settings - Fork 7
/
svr-workers.js
142 lines (121 loc) · 6.11 KB
/
svr-workers.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Distributed under AGPLv3 license: see /LICENSE for terms. Copyright 2019-2023 Dominic Morris.
const { Worker, isMainThread, parentPort } = require('worker_threads')
const configWallet = require('./config/wallet')
const utilsWallet = require('./utils')
const appWorkerCallbacks = require('./actions/appWorkerCallbacks')
const log = require('./sw-cli-log')
module.exports = {
// setup cpuWorkers and singleton appWorker
init: async (store) => {
const globalScope = utilsWallet.getMainThreadGlobalScope()
// create cpu workers
if (globalScope.cpuWorkers === undefined || globalScope.cpuWorkers.length == 0) {
globalScope.cpuWorkers = []
globalScope.CPU_WORKERS = 8
for (var i=0 ; i < globalScope.CPU_WORKERS ; i++) {
const worker = new Worker(`${__dirname}/cpu-worker/worker.js`)
worker.setMaxListeners(30) // a reasonable number which if exceeded would indicate a leak
worker.removeEventListener = worker.removeListener // map same interface as web worker
worker.addEventListener = worker.on
// StMaster - override postMessage to append common global data
worker.postMessageWrapped = function(msg) {
if (msg !== undefined && msg.data !== undefined) {
msg.data.stm_ApiPayload = configWallet.get_stm_ApiPayload() // StMaster - pass through config/wallet.js::stm_ApiPayload
}
//console.log('cpuWorker.postMessageWrapped...', msg)
return this.postMessage(msg)
}
globalScope.cpuWorkers.push(worker)
}
globalScope.nextCpuWorker = 0
}
// create singleton app worker
if (globalScope.appWorker === undefined) {
function createAppWorker() {
const appWorker = new Worker(`${__dirname}/app-worker/worker.js`)
appWorker.setMaxListeners(40)
appWorker.removeEventListener = appWorker.removeListener
appWorker.addEventListener = appWorker.on
appWorker.on('message', event => {
appWorkerCallbacks.appWorkerHandler(store, event) // handle common core app worker callbacks
const postback = event.data
const msg = event.msg
const status = event.status
if (postback && msg === 'NOTIFY_USER') {
utilsWallet.logMajor(postback.type == 'error' ? 'red' : 'green', 'white',
`${postback.type ? postback.type.toUpperCase() : ''}` +
`${postback.headline ? ' [' + postback.headline + ']' : ''}` +
`${postback.info ? ' [' + postback.info + ']': ''}` +
`${postback.desc1 ? ' [' +postback.desc1 + ']': ''}` +
`${postback.desc2 ? ' [' +postback.desc2 + ']' : ''}` +
`${postback.txid ? (' txid: ' + postback.txid + '') : ''}`,
null, { logServerConsole: true })
}
})
// StMaster - override postMessage to append common global data
appWorker.postMessageWrapped = function(msg) {
if (msg !== undefined && msg.data !== undefined) {
msg.data.stm_ApiPayload = configWallet.get_stm_ApiPayload() // StMaster - pass through config/wallet.js::stm_ApiPayload
}
//console.log('appWorker.postMessageWrapped...', msg)
return this.postMessage(msg)
}
return appWorker
}
globalScope.appWorker = createAppWorker()
// test - create appworkers for loading concurrently
// globalScope.loaderWorkers = []
// globalScope.loaderWorkers.push(createAppWorker())
// globalScope.loaderWorkers.push(createAppWorker())
// globalScope.loaderWorkers.push(createAppWorker())
// globalScope.loaderWorkers.push(createAppWorker())
await txdb_init()
}
// ping all workers
const pongs = globalScope.cpuWorkers.concat([globalScope.appWorker]).map(worker => {
return new Promise((resolve) => {
worker.once('message', (data) => { resolve(true) })
worker.postMessageWrapped({ msg: 'DIAG_PING', data: {} })
})
})
return Promise.all(pongs)
},
txdb_init: () => {
txdb_init()
},
terminate: () => {
const globalScope = utilsWallet.getMainThreadGlobalScope()
if (globalScope.volatileSockets_intId) {
log.info(`Clearing volatile sockets reconnector...`)
clearInterval(globalScope.volatileSockets_intId)
}
if (globalScope.cpuWorkers !== undefined) {
log.info(`Terminating ${globalScope.cpuWorkers.length} CPU workers...`)
globalScope.cpuWorkers.forEach(worker => {
worker.unref()
worker.terminate()
})
globalScope.cpuWorkers = undefined
}
if (globalScope.appWorker !== undefined) {
log.info(`Terminating app worker...`)
globalScope.appWorker.unref()
globalScope.appWorker.terminate()
globalScope.appWorker = undefined
}
}
}
async function txdb_init() {
const txDbSetup = new Promise((resolve) => {
utilsWallet.getAppWorker().on('message', (event) => {
if (event.msg === 'SERVER_INIT_TX_DB_DONE') {
resolve()
}
})
const globalScope = utilsWallet.getMainThreadGlobalScope()
const appWorker = utilsWallet.getAppWorker()
//utilsWallet.getAppWorker()
appWorker.postMessageWrapped({ msg: 'SERVER_INIT_TX_DB', data: {} })
})
return txDbSetup
}