Only get the device id once, rather than for every request to updateDeviceInfo

This commit is contained in:
Pagan Gazzard 2014-11-13 18:17:14 +00:00 committed by Pablo Carranza Vélez
parent 6a50889086
commit 26c8b140ce

View File

@ -277,7 +277,13 @@ exports.update = update = ->
# Set the updating as finished # Set the updating as finished
currentlyUpdating = 0 currentlyUpdating = 0
exports.updateDeviceInfo = updateDeviceInfo = (body) -> getDeviceID = do ->
deviceIdPromise = null
return ->
# We initialise the rejected promise just before we catch in order to avoid a useless first unhandled error warning.
deviceIdPromise ?= Promise.rejected()
# Only fetch the device id once (when successful, otherwise retry for each request)
deviceIdPromise = deviceIdPromise.catch ->
Promise.all([ Promise.all([
knex('config').select('value').where(key: 'apiKey') knex('config').select('value').where(key: 'apiKey')
knex('config').select('value').where(key: 'uuid') knex('config').select('value').where(key: 'uuid')
@ -295,7 +301,13 @@ exports.updateDeviceInfo = updateDeviceInfo = (body) ->
.then (devices) -> .then (devices) ->
if devices.length is 0 if devices.length is 0
throw new Error('Could not find this device?!') throw new Error('Could not find this device?!')
deviceID = devices[0].id return devices[0].id
exports.updateDeviceInfo = updateDeviceInfo = (body) ->
Promise.all([
knex('config').select('value').where(key: 'apiKey')
getDeviceID()
]).spread ([{value: apiKey}], deviceID) ->
resinAPI.patch( resinAPI.patch(
resource: 'device' resource: 'device'
id: deviceID id: deviceID