 	return err if _.isError(err)
 	return new Error(err.message ? err)
 
-selectAndKill = (appId) ->
+select = (appId) ->
 	knex('app').select().where({ appId })
 	.then ([ app ]) ->
 		if !app?
 			throw new Error('App not found')
-		kill(app)
-
-# Wait for app to signal it's ready to die, or timeout to complete (if it is defined and not-empty)
+		return app
+
+# Wait for app to signal it's ready to die, or timeout to complete.
+# timeout defaults to 1 minute.
 waitToKill = (app, timeout) ->
 	startTime = Date.now()
 	pollInterval = 100
 	timeout = parseInt(timeout)
+	timeout = 60000 if isNaN(timeout)
 	checkFileOrTimeout = ->
 		fs.statAsync(killmePath(app))
 		.catch (err) ->
-			throw err if isNaN(timeout) or (Date.now() - startTime) < timeout
+			throw err unless (Date.now() - startTime) > timeout
 		.then ->
 			fs.unlinkAsync(killmePath(app)).catch(_.noop)
-	checkFileOrTimeout()
-	.catch ->
-		Promise.delay(pollInterval).then(checkFileOrTimeout)
+	retryCheck = ->
+		checkFileOrTimeout()
+		.catch ->
+			Promise.delay(pollInterval).then(retryCheck)
+	retryCheck()
 
 UPDATE_IDLE = 0
 UPDATE_UPDATING = 1

device = require './device'
lockFile = Promise.promisifyAll(require('lockfile'))
bootstrap = require './bootstrap'

{ docker } = dockerUtils


		.then (environment) ->
			app.environment_variable = environment
			utils.extendEnvVars(app.environment_variable, uuid)
		.then (fullEnv) ->
			env = _.omit(fullEnv, _.keys(specialActionEnvVars))
			return [
				{
					appId: '' + app.id
					env: fullEnv
				},
				{
