diff --git a/lib/device.js b/lib/device.js index 5d251d8a..e0eb51a4 100644 --- a/lib/device.js +++ b/lib/device.js @@ -6,3 +6,17 @@ assert.ok(process.env.ANDROID_SERIAL, var log = require('./util/logger') .setGlobalIdentifier(process.env.ANDROID_SERIAL) .createLogger('device') + +function gracefullyExit() { + process.exit(0) +} + +process.on('SIGINT', function() { + gracefullyExit() +}) + +process.on('SIGTERM', function() { + gracefullyExit() +}) + +log.info('Started') diff --git a/lib/provider.js b/lib/provider.js index c0d43dae..782fc994 100644 --- a/lib/provider.js +++ b/lib/provider.js @@ -48,26 +48,27 @@ function isConnected(device) { function maybeConnect(device) { if (isConnectable(device) && !isConnected(device)) { - log.debug('Spawning worker for device "%s"', device.id) + log.info('Spawning worker for device "%s"', device.id) var proc = fork(path.join(__dirname, 'device'), { env: { ANDROID_SERIAL: device.id } }) proc.on('error', function(err) { - log.error('Worker of device "%s" had an error: %s', + log.error('Device worker "%s" had an error: %s', device.id, err.message) }) proc.on('exit', function(code, signal) { var data = workers[device.id] delete workers[device.id] if (code === 0) { - log.info('Worker of device "%s" stopped cleanly', device.id) + log.info('Device worker "%s" stopped cleanly', device.id) } else { - log.error('Worker of device "%s" had a dirty exit', device.id) + log.error('Device worker "%s" had a dirty exit (code %d)', + device.id, code) if (Date.now() - data.started < 10000) { - log.error('Worker of device "%s" failed in less than 10 seconds,' + + log.error('Device worker "%s" failed within 10 seconds of startup,' + ' will not attempt to restart', device.id) } else { @@ -96,12 +97,13 @@ function maybeDisconnect(device) { } function gracefullyKillWorker(id, done) { - var proc = workers[id] + var proc = workers[id].proc , timer timer = setTimeout(function() { - log.error('Worker of "%s" did not stop in time', id) + log.error('Device worker "%s" did not stop in time', id) proc.removeListener('exit', onExit) + log.warn('Force killing worker of device "%s"', id) proc.kill('SIGKILL') done() }, 10000) @@ -124,11 +126,11 @@ function gracefullyExit() { } process.on('SIGINT', function(e) { - log.debug('Received SIGINT') + log.info('Received SIGINT') gracefullyExit() }) process.on('SIGTERM', function(e) { - log.debug('Received SIGTERM') + log.info('Received SIGTERM') gracefullyExit() })