Stop all apps before rebooting or shutting down

This commit is contained in:
Pablo Carranza Velez 2016-05-02 17:27:45 -03:00
parent 88e512a69b
commit 3325ff47d4
4 changed files with 50 additions and 19 deletions

View File

@ -1,3 +1,4 @@
* Stop all apps before rebooting or shutting down [Pablo]
* Update request-progress to v2.0.1 [Pablo]
# v2.7.1

View File

@ -47,25 +47,51 @@ module.exports = (application) ->
application.update(req.body.force)
res.sendStatus(204)
unparsedRouter.post '/v1/reboot', (req, res) ->
new Promise (resolve, reject) ->
application.logSystemMessage('Rebooting', {}, 'Reboot')
utils.gosuper.post('/v1/reboot')
.on('error', reject)
.on('response', -> resolve())
.pipe(res)
parsedRouter.post '/v1/reboot', (req, res) ->
force = req.body.force
Promise.map utils.getKnexApps(), (theApp) ->
Promise.using application.lockUpdates(theApp.appId, force), ->
# There's a slight chance the app changed after the previous select
# So we fetch it again now the lock is acquired
utils.getKnexApp(theApp.appId)
.then (app) ->
application.kill(app, removeContainer: false) if app?
.then ->
new Promise (resolve, reject) ->
application.logSystemMessage('Rebooting', {}, 'Reboot')
utils.gosuper.post('/v1/reboot')
.on('error', reject)
.on('response', -> resolve())
.pipe(res)
.catch (err) ->
res.status(503).send(err?.message or err or 'Unknown error')
if err instanceof application.UpdatesLockedError
status = 423
else
status = 500
res.status(status).send(err?.message or err or 'Unknown error')
unparsedRouter.post '/v1/shutdown', (req, res) ->
new Promise (resolve, reject) ->
application.logSystemMessage('Shutting down', {}, 'Shutdown')
utils.gosuper.post('/v1/shutdown')
.on('error', reject)
.on('response', -> resolve())
.pipe(res)
parsedRouter.post '/v1/shutdown', (req, res) ->
force = req.body.force
Promise.map utils.getKnexApps(), (theApp) ->
Promise.using application.lockUpdates(theApp.appId, force), ->
# There's a slight chance the app changed after the previous select
# So we fetch it again now the lock is acquired
utils.getKnexApp(theApp.appId)
.then (app) ->
application.kill(app, removeContainer: false) if app?
.then ->
new Promise (resolve, reject) ->
application.logSystemMessage('Shutting down', {}, 'Shutdown')
utils.gosuper.post('/v1/shutdown')
.on('error', reject)
.on('response', -> resolve())
.pipe(res)
.catch (err) ->
res.status(503).send(err?.message or err or 'Unknown error')
if err instanceof application.UpdatesLockedError
status = 423
else
status = 500
res.status(status).send(err?.message or err or 'Unknown error')
parsedRouter.post '/v1/purge', (req, res) ->
appId = req.body.appId
@ -130,7 +156,7 @@ module.exports = (application) ->
Promise.using application.lockUpdates(appId, force), ->
utils.getKnexApp(appId)
.tap (app) ->
application.kill(app, true, false)
application.kill(app, removeContainer: false)
.then (app) ->
res.json(_.pick(app, 'containerId'))
.catch utils.AppNotFoundError, (e) ->

View File

@ -105,6 +105,7 @@ logTypes =
humanName: 'Failed to update config for application'
application = {}
application.UpdatesLockedError = UpdatesLockedError
application.logSystemMessage = logSystemMessage = (message, obj, eventName) ->
logger.log({ m: message, s: 1 })
@ -132,7 +133,7 @@ logSpecialAction = (action, value, success) ->
msg = "Applying config variable #{action} = #{value}"
logSystemMessage(msg, {}, "Apply special action #{if success then "success" else "in progress"}")
application.kill = kill = (app, updateDB = true, removeContainer = true) ->
application.kill = kill = (app, { updateDB = true, removeContainer = true } = {}) ->
logSystemEvent(logTypes.stopApp, app)
device.updateState(status: 'Stopping')
container = docker.getContainer(app.containerId)
@ -512,7 +513,7 @@ updateStrategies =
.then ->
waitToKill(localApp, timeout)
.then ->
kill(localApp, false)
kill(localApp, updateDB: false)
.catch (err) ->
logSystemEvent(logTypes.updateAppError, app, err) unless err instanceof UpdatesLockedError
throw err

View File

@ -225,6 +225,9 @@ exports.getKnexApp = (appId, columns) ->
throw new AppNotFoundError('App not found')
return app
exports.getKnexApps = (columns) ->
knex('app').select(columns)
exports.getOSVersion = (path) ->
fs.readFileAsync(path)
.then (releaseData) ->