1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 02:29:26 +02:00

Fix all lib/ files with ESLint rules with 0 errors.

This commit is contained in:
Gunther Brunner 2016-01-19 20:52:38 +09:00
parent 994977ea94
commit 434f63b3a9
69 changed files with 793 additions and 764 deletions

View file

@ -154,9 +154,8 @@ module.exports = function(options) {
log.info('Found device "%s" (%s)', device.id, device.type)
var privateTracker = new EventEmitter()
, willStop = false
, timer
, worker
var willStop = false
var timer, worker
// Wait for others to acknowledge the device
var register = new Promise(function(resolve) {
@ -176,6 +175,155 @@ module.exports = function(options) {
privateTracker.once('register', resolve)
})
// Spawn a device worker
function spawn() {
var allocatedPorts = ports.splice(0, 4)
var proc = options.fork(device, allocatedPorts.slice())
var resolver = Promise.defer()
function exitListener(code, signal) {
if (signal) {
log.warn(
'Device worker "%s" was killed with signal %s, assuming ' +
'deliberate action and not restarting'
, device.id
, signal
)
resolver.resolve()
}
else if (code === 0) {
log.info('Device worker "%s" stopped cleanly', device.id)
resolver.resolve()
}
else {
resolver.reject(new procutil.ExitError(code))
}
}
function errorListener(err) {
log.error(
'Device worker "%s" had an error: %s'
, device.id
, err.message
)
}
function messageListener(message) {
switch (message) {
case 'ready':
_.pull(lists.waiting, device.id)
lists.ready.push(device.id)
break
default:
log.warn(
'Unknown message from device worker "%s": "%s"'
, device.id
, message
)
break
}
}
proc.on('exit', exitListener)
proc.on('error', errorListener)
proc.on('message', messageListener)
lists.waiting.push(device.id)
return resolver.promise
.finally(function() {
log.info('Cleaning up device worker "%s"', device.id)
proc.removeListener('exit', exitListener)
proc.removeListener('error', errorListener)
proc.removeListener('message', messageListener)
// Return used ports to the main pool
Array.prototype.push.apply(ports, allocatedPorts)
// Update lists
_.pull(lists.ready, device.id)
_.pull(lists.waiting, device.id)
})
.cancellable()
.catch(Promise.CancellationError, function() {
log.info('Gracefully killing device worker "%s"', device.id)
return procutil.gracefullyKill(proc, options.killTimeout)
})
.catch(Promise.TimeoutError, function(err) {
log.error(
'Device worker "%s" did not stop in time: %s'
, device.id
, err.message
)
})
}
// Starts a device worker and keeps it alive
function work() {
return (worker = workers[device.id] = spawn())
.then(function() {
log.info('Device worker "%s" has retired', device.id)
delete workers[device.id]
worker = null
// Tell others the device is gone
push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceAbsentMessage(
device.id
))
])
})
.catch(procutil.ExitError, function(err) {
if (!willStop) {
log.error(
'Device worker "%s" died with code %s'
, device.id
, err.code
)
log.info('Restarting device worker "%s"', device.id)
return Promise.delay(500)
.then(function() {
return work()
})
}
})
}
// No more work required
function stop() {
if (worker) {
log.info('Shutting down device worker "%s"', device.id)
worker.cancel()
}
}
// Check if we can do anything with the device
function check() {
clearTimeout(timer)
if (device.present) {
// We might get multiple status updates in rapid succession,
// so let's wait for a while
switch (device.type) {
case 'device':
case 'emulator':
willStop = false
timer = setTimeout(work, 100)
break
default:
willStop = true
timer = setTimeout(stop, 100)
break
}
}
else {
stop()
}
}
register.then(function() {
log.info('Registered device "%s"', device.id)
check()
@ -250,154 +398,6 @@ module.exports = function(options) {
})
}
// Check if we can do anything with the device
function check() {
clearTimeout(timer)
if (device.present) {
// We might get multiple status updates in rapid succession,
// so let's wait for a while
switch (device.type) {
case 'device':
case 'emulator':
willStop = false
timer = setTimeout(work, 100)
break
default:
willStop = true
timer = setTimeout(stop, 100)
break
}
}
else {
stop()
}
}
// Starts a device worker and keeps it alive
function work() {
return (worker = workers[device.id] = spawn())
.then(function() {
log.info('Device worker "%s" has retired', device.id)
delete workers[device.id]
worker = null
// Tell others the device is gone
push.send([
wireutil.global
, wireutil.envelope(new wire.DeviceAbsentMessage(
device.id
))
])
})
.catch(procutil.ExitError, function(err) {
if (!willStop) {
log.error(
'Device worker "%s" died with code %s'
, device.id
, err.code
)
log.info('Restarting device worker "%s"', device.id)
return Promise.delay(500)
.then(function() {
return work()
})
}
})
}
// No more work required
function stop() {
if (worker) {
log.info('Shutting down device worker "%s"', device.id)
worker.cancel()
}
}
// Spawn a device worker
function spawn() {
var allocatedPorts = ports.splice(0, 4)
, proc = options.fork(device, allocatedPorts.slice())
, resolver = Promise.defer()
function exitListener(code, signal) {
if (signal) {
log.warn(
'Device worker "%s" was killed with signal %s, assuming ' +
'deliberate action and not restarting'
, device.id
, signal
)
resolver.resolve()
}
else if (code === 0) {
log.info('Device worker "%s" stopped cleanly', device.id)
resolver.resolve()
}
else {
resolver.reject(new procutil.ExitError(code))
}
}
function errorListener(err) {
log.error(
'Device worker "%s" had an error: %s'
, device.id
, err.message
)
}
function messageListener(message) {
switch (message) {
case 'ready':
_.pull(lists.waiting, device.id)
lists.ready.push(device.id)
break
default:
log.warn(
'Unknown message from device worker "%s": "%s"'
, device.id
, message
)
break
}
}
proc.on('exit', exitListener)
proc.on('error', errorListener)
proc.on('message', messageListener)
lists.waiting.push(device.id)
return resolver.promise
.finally(function() {
log.info('Cleaning up device worker "%s"', device.id)
proc.removeListener('exit', exitListener)
proc.removeListener('error', errorListener)
proc.removeListener('message', messageListener)
// Return used ports to the main pool
Array.prototype.push.apply(ports, allocatedPorts)
// Update lists
_.pull(lists.ready, device.id)
_.pull(lists.waiting, device.id)
})
.cancellable()
.catch(Promise.CancellationError, function() {
log.info('Gracefully killing device worker "%s"', device.id)
return procutil.gracefullyKill(proc, options.killTimeout)
})
.catch(Promise.TimeoutError, function(err) {
log.error(
'Device worker "%s" did not stop in time: %s'
, device.id
, err.message
)
})
}
flippedTracker.on(device.id, deviceListener)
privateTracker.on('change', changeListener)
privateTracker.on('remove', removeListener)