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
143
144
145
146
147
148
149
150
151
152
153
|
#!/usr/bin/env node
const minimist = require('minimist')
const loggerModule = require('./logger')
const configModule = require('./config')
const {Server, ResponseMessage, RequestMessage} = require('./server')
const WorkersList = require('./workers-list')
const {masterConfig} = configModule
/**
* @type {Logger}
*/
let logger
/**
* @type {Server}
*/
let server
/**
* @type WorkersList
*/
let workers
main().catch(e => {
console.error(e)
process.exit(1)
})
async function main() {
if (process.argv.length < 3) {
usage()
process.exit(0)
}
process.on('SIGINT', term)
process.on('SIGTERM', term)
const argv = minimist(process.argv.slice(2))
if (!argv.config)
throw new Error('--config option is required')
// read config
try {
configModule.parseMasterConfig(argv.config)
} catch (e) {
console.error(`config parsing error: ${e.message}`)
process.exit(1)
}
await loggerModule.init({
file: masterConfig.log_file,
levelFile: masterConfig.log_level_file,
levelConsole: masterConfig.log_level_console,
})
logger = loggerModule.getLogger('jobd-master')
// console.log(masterConfig)
workers = new WorkersList()
// start server
server = new Server()
server.on('message', onMessage)
server.start(masterConfig.port, masterConfig.host)
logger.info('server started')
}
/**
* @param {RequestMessage|ResponseMessage} message
* @param {Connection} connection
* @return {Promise<*>}
*/
async function onMessage({message, connection}) {
try {
if (!(message instanceof RequestMessage)) {
logger.debug('ignoring message', message)
return
}
if (message.requestType !== 'ping')
logger.info('onMessage:', message)
if (masterConfig.password && message.password !== masterConfig.password) {
connection.send(new ResponseMessage().setError('invalid password'))
return connection.close()
}
switch (message.requestType) {
case 'ping':
connection.send(new ResponseMessage().setError('pong'))
break
case 'register-worker': {
const targets = message.requestData?.targets || []
if (!targets.length) {
connection.send(new ResponseMessage().setError(`targets are empty`))
break
}
workers.add(connection, targets)
connection.send(new ResponseMessage().setData('ok'))
break
}
case 'poke': {
const targets = message.requestData?.targets || []
if (!targets.length) {
connection.send(new ResponseMessage().setError(`targets are empty`))
break
}
workers.poke(targets)
connection.send(new ResponseMessage().setData('ok'))
break
}
case 'status':
const info = workers.getInfo()
connection.send(new ResponseMessage().setData({
workers: info,
memoryUsage: process.memoryUsage()
}))
break
default:
connection.send(new ResponseMessage().setError(`unknown request type: '${message.requestType}'`))
break
}
} catch (error) {
logger.error(`error while handling message:`, message, error)
connection.send(new ResponseMessage().setError('server error: ' + error?.message))
}
}
function usage() {
let s = `${process.argv[1]} OPTIONS
Options:
--config <path>`
console.log(s)
}
function term() {
if (logger)
logger.info('shutdown')
loggerModule.shutdown(function() {
process.exit()
})
}
|