mirror of
https://github.com/openstf/stf
synced 2025-10-04 02:09:32 +02:00
Allocate two ports to each worker.
This commit is contained in:
parent
e0426d01be
commit
c45c081c6f
3 changed files with 43 additions and 12 deletions
19
lib/cli.js
19
lib/cli.js
|
@ -31,6 +31,14 @@ program
|
||||||
, 'restart worker only if it stays alive for longer than this'
|
, 'restart worker only if it stays alive for longer than this'
|
||||||
, Number
|
, Number
|
||||||
, 10000)
|
, 10000)
|
||||||
|
.option('--min-port <port>'
|
||||||
|
, 'minimum port number for worker use'
|
||||||
|
, Number
|
||||||
|
, 7400)
|
||||||
|
.option('--max-port <port>'
|
||||||
|
, 'maximum port number for worker use'
|
||||||
|
, Number
|
||||||
|
, 7800)
|
||||||
.action(function() {
|
.action(function() {
|
||||||
var serials = cliutil.allUnknownArgs(arguments)
|
var serials = cliutil.allUnknownArgs(arguments)
|
||||||
, options = cliutil.lastArg(arguments)
|
, options = cliutil.lastArg(arguments)
|
||||||
|
@ -47,16 +55,18 @@ program
|
||||||
, restartThreshold: options.restartThreshold
|
, restartThreshold: options.restartThreshold
|
||||||
, restartTimeout: 1000
|
, restartTimeout: 1000
|
||||||
, killTimeout: 10000
|
, killTimeout: 10000
|
||||||
|
, ports: cliutil.range(options.minPort, options.maxPort)
|
||||||
, filter: function(device) {
|
, filter: function(device) {
|
||||||
return serials.length === 0 || serials.indexOf(device.id) !== -1
|
return serials.length === 0 || serials.indexOf(device.id) !== -1
|
||||||
}
|
}
|
||||||
, fork: function(device) {
|
, fork: function(device, ports) {
|
||||||
var fork = require('child_process').fork
|
var fork = require('child_process').fork
|
||||||
return fork(__filename, [
|
return fork(__filename, [
|
||||||
'device', device.id
|
'device', device.id
|
||||||
, '--provider', options.name
|
, '--provider', options.name
|
||||||
, '--connect-sub', options.connectSub.join(',')
|
, '--connect-sub', options.connectSub.join(',')
|
||||||
, '--connect-push', options.connectPush.join(',')
|
, '--connect-push', options.connectPush.join(',')
|
||||||
|
, '--ports', ports.join(',')
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
, endpoints: {
|
, endpoints: {
|
||||||
|
@ -78,6 +88,9 @@ program
|
||||||
.option('-p, --connect-push <endpoint>'
|
.option('-p, --connect-push <endpoint>'
|
||||||
, 'push endpoint'
|
, 'push endpoint'
|
||||||
, cliutil.list)
|
, cliutil.list)
|
||||||
|
.option('--ports <ports>'
|
||||||
|
, 'ports allocated to worker'
|
||||||
|
, cliutil.list)
|
||||||
.action(function(serial, options) {
|
.action(function(serial, options) {
|
||||||
if (!options.connectSub) {
|
if (!options.connectSub) {
|
||||||
this.missingArgument('--connect-sub')
|
this.missingArgument('--connect-sub')
|
||||||
|
@ -88,10 +101,14 @@ program
|
||||||
if (!options.provider) {
|
if (!options.provider) {
|
||||||
this.missingArgument('--provider')
|
this.missingArgument('--provider')
|
||||||
}
|
}
|
||||||
|
if (!options.provider) {
|
||||||
|
this.missingArgument('--ports')
|
||||||
|
}
|
||||||
|
|
||||||
require('./roles/device')({
|
require('./roles/device')({
|
||||||
serial: serial
|
serial: serial
|
||||||
, provider: options.provider
|
, provider: options.provider
|
||||||
|
, ports: options.ports
|
||||||
, endpoints: {
|
, endpoints: {
|
||||||
sub: options.connectSub
|
sub: options.connectSub
|
||||||
, push: options.connectPush
|
, push: options.connectPush
|
||||||
|
|
|
@ -93,7 +93,8 @@ module.exports = function(options) {
|
||||||
function maybeConnect(device) {
|
function maybeConnect(device) {
|
||||||
if (isConnectable(device) && !isConnected(device)) {
|
if (isConnectable(device) && !isConnected(device)) {
|
||||||
log.info('Spawning device worker "%s"', device.id)
|
log.info('Spawning device worker "%s"', device.id)
|
||||||
var proc = options.fork(device)
|
var ports = options.ports.splice(0, 2)
|
||||||
|
, proc = options.fork(device, ports)
|
||||||
|
|
||||||
function errorListener(err) {
|
function errorListener(err) {
|
||||||
log.error('Device worker "%s" had an error: %s',
|
log.error('Device worker "%s" had an error: %s',
|
||||||
|
@ -101,9 +102,7 @@ module.exports = function(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function exitListener(code, signal) {
|
function exitListener(code, signal) {
|
||||||
var data = workers[device.id]
|
var worker = cleanupWorker(device.id)
|
||||||
delete workers[device.id]
|
|
||||||
counter -= 1
|
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case 0:
|
case 0:
|
||||||
log.info('Device worker "%s" stopped cleanly', device.id)
|
log.info('Device worker "%s" stopped cleanly', device.id)
|
||||||
|
@ -113,7 +112,7 @@ module.exports = function(options) {
|
||||||
, device.id)
|
, device.id)
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
if (Date.now() - data.started < options.restartThreshold) {
|
if (Date.now() - worker.started < options.restartThreshold) {
|
||||||
log.error(
|
log.error(
|
||||||
'Device worker "%s" died with exit code %d, ' +
|
'Device worker "%s" died with exit code %d, ' +
|
||||||
'NOT restarting due to threshold of %dms not being met'
|
'NOT restarting due to threshold of %dms not being met'
|
||||||
|
@ -157,6 +156,7 @@ module.exports = function(options) {
|
||||||
device: device
|
device: device
|
||||||
, proc: proc
|
, proc: proc
|
||||||
, started: Date.now()
|
, started: Date.now()
|
||||||
|
, ports: ports
|
||||||
, unbind: function() {
|
, unbind: function() {
|
||||||
proc.removeListener('error', errorListener)
|
proc.removeListener('error', errorListener)
|
||||||
proc.removeListener('exit', exitListener)
|
proc.removeListener('exit', exitListener)
|
||||||
|
@ -204,10 +204,8 @@ module.exports = function(options) {
|
||||||
worker = workers[id]
|
worker = workers[id]
|
||||||
|
|
||||||
function onExit() {
|
function onExit() {
|
||||||
delete workers[id]
|
cleanupWorker(id)
|
||||||
log.info('Gracefully killed device worker "%s"', id)
|
log.info('Gracefully killed device worker "%s"', id)
|
||||||
counter -= 1
|
|
||||||
boast()
|
|
||||||
deferred.resolve()
|
deferred.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,10 +225,8 @@ module.exports = function(options) {
|
||||||
, worker = workers[id]
|
, worker = workers[id]
|
||||||
|
|
||||||
function onExit() {
|
function onExit() {
|
||||||
delete workers[id]
|
cleanupWorker(id)
|
||||||
log.warn('Force killed device worker "%s"', id)
|
log.warn('Force killed device worker "%s"', id)
|
||||||
counter -= 1
|
|
||||||
boast()
|
|
||||||
deferred.resolve()
|
deferred.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,6 +258,15 @@ module.exports = function(options) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cleanupWorker(id) {
|
||||||
|
var worker = workers[id]
|
||||||
|
delete workers[id]
|
||||||
|
Array.prototype.push.apply(options.ports, worker.ports)
|
||||||
|
counter -= 1
|
||||||
|
boast()
|
||||||
|
return worker
|
||||||
|
}
|
||||||
|
|
||||||
function boast() {
|
function boast() {
|
||||||
log.info('Providing %d device(s)', counter)
|
log.info('Providing %d device(s)', counter)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,3 +9,12 @@ module.exports.allUnknownArgs = function(args) {
|
||||||
module.exports.lastArg = function(args) {
|
module.exports.lastArg = function(args) {
|
||||||
return args[args.length - 1]
|
return args[args.length - 1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.range = function(from, to) {
|
||||||
|
var items = []
|
||||||
|
, i
|
||||||
|
for (i = from; i <= to; ++i) {
|
||||||
|
items.push(i)
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue