diff --git a/lib/units/device/plugins/service.js b/lib/units/device/plugins/service.js index b0efc6df..6cf08b2e 100644 --- a/lib/units/device/plugins/service.js +++ b/lib/units/device/plugins/service.js @@ -69,7 +69,6 @@ module.exports = syrup.serial() .timeout(10000) }) .then(function(out) { - lifecycle.share('Agent shell', out) streamutil.talk(log, 'Agent says: "%s"', out) }) .then(function() { @@ -80,10 +79,33 @@ module.exports = syrup.serial() agent.socket = conn agent.writer = new ms.DelimitingStream() agent.writer.pipe(conn) - lifecycle.share('Agent connection', conn) + return prepareForAgentDeath(conn) }) } + function prepareForAgentDeath(conn) { + function endListener() { + var startTime = Date.now() + log.important('Agent connection ended, attempting to relaunch') + openService() + .timeout(5000) + .then(function() { + log.important('Agent relaunched in %dms', Date.now() - startTime) + }) + .catch(function(err) { + log.fatal('Agent connection could not be relaunched', err.stack) + lifecycle.fatal() + }) + } + + conn.once('end', endListener) + + conn.on('error', function(err) { + log.fatal('Agent connection had an error', err.stack) + lifecycle.fatal() + }) + } + function stopAgent() { return devutil.killProcsByComm( adb @@ -162,10 +184,33 @@ module.exports = syrup.serial() service.reader.on('data', handleEnvelope) service.writer = new ms.DelimitingStream() service.writer.pipe(conn) - lifecycle.share('Service connection', conn) + return prepareForServiceDeath(conn) }) } + function prepareForServiceDeath(conn) { + function endListener() { + var startTime = Date.now() + log.important('Service connection ended, attempting to relaunch') + openService() + .timeout(5000) + .then(function() { + log.important('Service relaunched in %dms', Date.now() - startTime) + }) + .catch(function(err) { + log.fatal('Service connection could not be relaunched', err.stack) + lifecycle.fatal() + }) + } + + conn.once('end', endListener) + + conn.on('error', function(err) { + log.fatal('Service connection had an error', err.stack) + lifecycle.fatal() + }) + } + function handleEnvelope(data) { var envelope = apk.wire.Envelope.decode(data) , message