jobd: support 'name' config option

This commit is contained in:
Evgeny Zinoviev 2021-05-07 23:01:11 +03:00
parent a0a3ee5c26
commit 6589671f02
6 changed files with 22 additions and 10 deletions

View File

@ -61,7 +61,7 @@ time already, and proven to be stable and efficient.
- [remove-target(target: string)](#remove-targettarget-string)
- [set-target-concurrency(target: string, concurrency: int)](#set-target-concurrencytarget-string-concurrency-int)
- [jobd-master requests](#jobd-master-requests)
- [register-worker(targets: string[])](#register-workertargets-string)
- [register-worker(targets: string[], name: string)](#register-workertargets-string-name-string)
- [poke(targets: string[])](#poketargets-string)
- [pause(targets: string[])](#pausetargets-string-1)
- [continue(targets: string[])](#continuetargets-string-1)
@ -411,6 +411,7 @@ Without section:
- `always_allow_localhost` *(boolean, default: `false`)* — when set to `1`
or `true`, allows accepting requests from clients connecting from localhost
without password
- `name` *(string, default: `os.hostname()`)* — worker name
- `master_host` *(string)* — master hostname
- `master_port` *(int)* — master port. If hostname or port is omitted, jobd
will not connect to master.
@ -608,8 +609,8 @@ Here is the list of supported requests, using `type(arguments)` notation.
#### jobd-master requests
* ##### `register-worker(targets: string[])`
Used by a jobd instance to register itself with master. Clients don't need it.
* ##### `register-worker(targets: string[], name: string)`
Used by jobd instances to register themselves with master. Clients don't need it.
* ##### `poke(targets: string[])`
Send [`poll(targets)`](#polltargets-string) requests to all registered workers that serve specified

View File

@ -267,13 +267,13 @@ async function masterListWorkers() {
const columns = ['worker', 'targets', 'concurrency', 'length', 'paused']
const rows = []
for (const worker of response.workers) {
let remoteAddr = `${worker.remoteAddr}:${worker.remotePort}`
let info = `${worker.name}\n(${worker.remoteAddr})`
let targets = Object.keys(worker.workerStatus.targets)
let concurrencies = targets.map(t => worker.workerStatus.targets[t].concurrency)
let lengths = targets.map(t => worker.workerStatus.targets[t].length)
let pauses = targets.map(t => worker.workerStatus.targets[t].paused ? 'yes' : 'no')
rows.push([
remoteAddr,
info,
targets.join("\n"),
concurrencies.join("\n"),
lengths.join("\n"),

View File

@ -149,7 +149,13 @@ async function term() {
*/
async function onRegisterWorker(data, connection) {
const targets = validateInputTargets(data, null)
workers.add(connection, targets)
if (typeof data.name !== 'string')
throw new Error('name is missing or invalid')
workers.add(connection, {
targets,
name: data.name
})
return 'ok'
}

View File

@ -1,5 +1,6 @@
#!/usr/bin/env node
const minimist = require('minimist')
const os = require('os')
const loggerModule = require('./lib/logger')
const config = require('./lib/config')
const db = require('./lib/db')
@ -183,6 +184,7 @@ function connectToMaster() {
try {
let response = await connection.sendRequest(
new RequestMessage('register-worker', {
name: config.get('name') || os.hostname(),
targets: worker.getTargets()
})
)

View File

@ -71,6 +71,7 @@ function parseWorkerConfig(file) {
const raw = readFile(file)
const scheme = {
name: {},
host: {required: true},
port: {required: true, type: 'int'},
password: {},

View File

@ -35,11 +35,12 @@ class WorkersList {
/**
* @param {Connection} connection
* @param {string[]} targets
* @param {string} name
*/
add(connection, targets) {
this.logger.info(`add: connection from ${connection.remoteAddr()}, targets ${JSON.stringify(targets)}`)
add(connection, {targets, name}) {
this.logger.info(`add: connection from ${connection.remoteAddr()}, name ${name}, targets ${JSON.stringify(targets)}`)
this.workers.push({connection, targets})
this.workers.push({connection, targets, name})
connection.on('close', () => {
this.logger.info(`connection from ${connection.remoteAddr()} closed, removing worker`)
this.workers = this.workers.filter(worker => {
@ -161,7 +162,8 @@ class WorkersList {
const workerInfo = {
remoteAddr: worker.connection.socket?.remoteAddress,
remotePort: worker.connection.socket?.remotePort,
targets: worker.targets
targets: worker.targets,
name: worker.name,
}
if (pollWorkers) {