Merge pull request #38 from resin-io/37-save-after-starting

Only save the app if starting the container was successful
This commit is contained in:
Pablo Carranza Vélez 2015-12-03 09:21:56 -08:00
commit d0ddfd8532
2 changed files with 13 additions and 9 deletions

View File

@ -1,3 +1,5 @@
* Only save the app if starting the container was successful [Pablo]
# v1.3.0
* Remove volumes when removing a container [Pablo]

View File

@ -201,13 +201,6 @@ application.start = start = (app) ->
.catch (err) ->
logSystemEvent(logTypes.installAppError, app, err)
throw err
.tap (container) ->
# Update the app info the moment we create the container, even if then starting the container fails. This
# stops issues with constantly creating new containers for an image that fails to start.
app.containerId = container.id
knex('app').update(app).where(appId: app.appId)
.then (affectedRows) ->
knex('app').insert(app) if affectedRows == 0
.tap (container) ->
logSystemEvent(logTypes.startApp, app)
device.updateState(status: 'Starting')
@ -226,11 +219,20 @@ application.start = start = (app) ->
# 304 means the container was already started, precisely what we want :)
if statusCode is '304'
return
# If starting the container failed, we remove it so that it doesn't litter
container.removeAsync(v: true)
.finally ->
logSystemEvent(logTypes.startAppError, app, err)
throw err
.then ->
device.updateState(commit: app.commit)
logger.attach(app)
.tap (container) ->
# Update the app info, only if starting the container worked.
app.containerId = container.id
knex('app').update(app).where(appId: app.appId)
.then (affectedRows) ->
knex('app').insert(app) if affectedRows == 0
.tap ->
logSystemEvent(logTypes.startAppSuccess, app)
.finally ->