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'
|
||||
, Number
|
||||
, 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() {
|
||||
var serials = cliutil.allUnknownArgs(arguments)
|
||||
, options = cliutil.lastArg(arguments)
|
||||
|
@ -47,16 +55,18 @@ program
|
|||
, restartThreshold: options.restartThreshold
|
||||
, restartTimeout: 1000
|
||||
, killTimeout: 10000
|
||||
, ports: cliutil.range(options.minPort, options.maxPort)
|
||||
, filter: function(device) {
|
||||
return serials.length === 0 || serials.indexOf(device.id) !== -1
|
||||
}
|
||||
, fork: function(device) {
|
||||
, fork: function(device, ports) {
|
||||
var fork = require('child_process').fork
|
||||
return fork(__filename, [
|
||||
'device', device.id
|
||||
, '--provider', options.name
|
||||
, '--connect-sub', options.connectSub.join(',')
|
||||
, '--connect-push', options.connectPush.join(',')
|
||||
, '--ports', ports.join(',')
|
||||
])
|
||||
}
|
||||
, endpoints: {
|
||||
|
@ -78,6 +88,9 @@ program
|
|||
.option('-p, --connect-push <endpoint>'
|
||||
, 'push endpoint'
|
||||
, cliutil.list)
|
||||
.option('--ports <ports>'
|
||||
, 'ports allocated to worker'
|
||||
, cliutil.list)
|
||||
.action(function(serial, options) {
|
||||
if (!options.connectSub) {
|
||||
this.missingArgument('--connect-sub')
|
||||
|
@ -88,10 +101,14 @@ program
|
|||
if (!options.provider) {
|
||||
this.missingArgument('--provider')
|
||||
}
|
||||
if (!options.provider) {
|
||||
this.missingArgument('--ports')
|
||||
}
|
||||
|
||||
require('./roles/device')({
|
||||
serial: serial
|
||||
, provider: options.provider
|
||||
, ports: options.ports
|
||||
, endpoints: {
|
||||
sub: options.connectSub
|
||||
, push: options.connectPush
|
||||
|
|
|
@ -93,7 +93,8 @@ module.exports = function(options) {
|
|||
function maybeConnect(device) {
|
||||
if (isConnectable(device) && !isConnected(device)) {
|
||||
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) {
|
||||
log.error('Device worker "%s" had an error: %s',
|
||||
|
@ -101,9 +102,7 @@ module.exports = function(options) {
|
|||
}
|
||||
|
||||
function exitListener(code, signal) {
|
||||
var data = workers[device.id]
|
||||
delete workers[device.id]
|
||||
counter -= 1
|
||||
var worker = cleanupWorker(device.id)
|
||||
switch (code) {
|
||||
case 0:
|
||||
log.info('Device worker "%s" stopped cleanly', device.id)
|
||||
|
@ -113,7 +112,7 @@ module.exports = function(options) {
|
|||
, device.id)
|
||||
break
|
||||
default:
|
||||
if (Date.now() - data.started < options.restartThreshold) {
|
||||
if (Date.now() - worker.started < options.restartThreshold) {
|
||||
log.error(
|
||||
'Device worker "%s" died with exit code %d, ' +
|
||||
'NOT restarting due to threshold of %dms not being met'
|
||||
|
@ -157,6 +156,7 @@ module.exports = function(options) {
|
|||
device: device
|
||||
, proc: proc
|
||||
, started: Date.now()
|
||||
, ports: ports
|
||||
, unbind: function() {
|
||||
proc.removeListener('error', errorListener)
|
||||
proc.removeListener('exit', exitListener)
|
||||
|
@ -204,10 +204,8 @@ module.exports = function(options) {
|
|||
worker = workers[id]
|
||||
|
||||
function onExit() {
|
||||
delete workers[id]
|
||||
cleanupWorker(id)
|
||||
log.info('Gracefully killed device worker "%s"', id)
|
||||
counter -= 1
|
||||
boast()
|
||||
deferred.resolve()
|
||||
}
|
||||
|
||||
|
@ -227,10 +225,8 @@ module.exports = function(options) {
|
|||
, worker = workers[id]
|
||||
|
||||
function onExit() {
|
||||
delete workers[id]
|
||||
cleanupWorker(id)
|
||||
log.warn('Force killed device worker "%s"', id)
|
||||
counter -= 1
|
||||
boast()
|
||||
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() {
|
||||
log.info('Providing %d device(s)', counter)
|
||||
}
|
||||
|
|
|
@ -9,3 +9,12 @@ module.exports.allUnknownArgs = function(args) {
|
|||
module.exports.lastArg = function(args) {
|
||||
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