Rename state module to device and use sensible function names for the new module name.

This commit is contained in:
Pagan Gazzard 2015-07-20 21:02:37 +01:00 committed by Pablo Carranza Vélez
parent 0917a46a2d
commit b8ecdb3ce2
3 changed files with 18 additions and 17 deletions

View File

@ -31,7 +31,7 @@ knex.init.then ->
api = require './api'
application = require './application'
{ updateDeviceState } = require './state'
device = require './device'
randomstring = require 'randomstring'
console.log('Starting API server..')
@ -40,7 +40,7 @@ knex.init.then ->
# Let API know what version we are, and our api connection info.
console.log('Updating supervisor version and api info')
updateDeviceState(
device.updateState(
api_port: config.listenPort
api_secret: secret
supervisor_version: utils.supervisorVersion
@ -64,7 +64,7 @@ knex.init.then ->
updateIpAddr = ->
utils.findIpAddrs().then (ipAddrs) ->
updateDeviceState(
device.updateState(
ip_address: ipAddrs.join(' ')
)
console.log('Starting periodic check for IP addresses..')

View File

@ -9,7 +9,7 @@ utils = require './utils'
tty = require './lib/tty'
logger = require './lib/logger'
{ cachedResinApi } = require './request'
{ getDeviceID, updateDeviceState } = require './state'
device = require './device'
{ docker } = dockerUtils
@ -84,7 +84,7 @@ logSystemEvent = (logType, app, error) ->
kill = (app) ->
logSystemEvent(logTypes.stopApp, app)
updateDeviceState(status: 'Stopping')
device.updateState(status: 'Stopping')
container = docker.getContainer(app.containerId)
tty.stop(app)
.catch (err) ->
@ -121,12 +121,12 @@ fetch = (app) ->
docker.getImage(app.imageId).inspectAsync()
.catch (error) ->
logSystemEvent(logTypes.downloadApp, app)
updateDeviceState(status: 'Downloading')
device.updateState(status: 'Downloading')
dockerUtils.fetchImageWithProgress app.imageId, (progress) ->
updateDeviceState(download_progress: progress.percentage)
device.updateState(download_progress: progress.percentage)
.then ->
logSystemEvent(logTypes.downloadAppSuccess, app)
updateDeviceState(download_progress: null)
device.updateState(download_progress: null)
docker.getImage(app.imageId).inspectAsync()
.catch (err) ->
logSystemEvent(logTypes.downloadAppError, app, err)
@ -155,7 +155,7 @@ exports.start = start = (app) ->
fetch(app)
.then (imageInfo) ->
logSystemEvent(logTypes.installApp, app)
updateDeviceState(status: 'Installing')
device.updateState(status: 'Installing')
ports = {}
if portList?
@ -193,7 +193,7 @@ exports.start = start = (app) ->
knex('app').insert(app)
.tap (container) ->
logSystemEvent(logTypes.startApp, app)
updateDeviceState(status: 'Starting')
device.updateState(status: 'Starting')
ports = {}
if portList?
portList.forEach (port) ->
@ -218,12 +218,12 @@ exports.start = start = (app) ->
logSystemEvent(logTypes.startAppError, app, err)
throw err
.then ->
updateDeviceState(commit: app.commit)
device.updateState(commit: app.commit)
logger.attach(app)
.tap ->
logSystemEvent(logTypes.startAppSuccess, app)
.finally ->
updateDeviceState(status: 'Idle')
device.updateState(status: 'Idle')
getEnvironment = do ->
envApiEndpoint = url.resolve(config.apiEndpoint, '/environment')
@ -260,7 +260,7 @@ exports.update = update = ->
apiKey = apiKey.value
uuid = uuid.value
deviceId = getDeviceID()
deviceId = device.getID()
remoteApps = cachedResinApi.get
resource: 'application'
@ -351,7 +351,7 @@ exports.update = update = ->
console.log('Scheduling another update attempt due to failure: ', delayTime, err)
setTimeout(update, delayTime)
.finally ->
updateDeviceState(status: 'Idle')
device.updateState(status: 'Idle')
if currentlyUpdating is 2
# If an update is required then schedule it
setTimeout(update)

View File

@ -3,8 +3,9 @@ Promise = require 'bluebird'
knex = require './db'
utils = require './utils'
{ resinApi } = require './request'
device = exports
exports.getDeviceID = getDeviceID = do ->
exports.getID = do ->
deviceIdPromise = null
return ->
# We initialise the rejected promise just before we catch in order to avoid a useless first unhandled error warning.
@ -34,7 +35,7 @@ exports.getDeviceID = getDeviceID = do ->
# Calling this function updates the local device state, which is then used to synchronise
# the remote device state, repeating any failed updates until successfully synchronised.
# This function will also optimise updates by merging multiple updates and only sending the latest state.
exports.updateDeviceState = updateDeviceState = do ->
exports.updateState = do ->
applyPromise = Promise.resolve()
targetState = {}
actualState = {}
@ -49,7 +50,7 @@ exports.updateDeviceState = updateDeviceState = do ->
applyPromise = Promise.join(
knex('config').select('value').where(key: 'apiKey')
getDeviceID()
device.getID()
([{value: apiKey}], deviceID) ->
stateDiff = getStateDiff()
if _.size(stateDiff) is 0