Merge pull request #336 from resin-io/request-timeout

Add Promise timeouts to all external requests
This commit is contained in:
Pablo Carranza Vélez 2016-10-30 21:47:21 -03:00 committed by GitHub
commit fe68b27acb
5 changed files with 12 additions and 0 deletions

View File

@ -539,6 +539,7 @@ getRemoteState = (uuid, apiKey) ->
, cachedResinApi.passthrough , cachedResinApi.passthrough
cachedResinApi._request(requestParams) cachedResinApi._request(requestParams)
.timeout(config.apiTimeout)
.catch (err) -> .catch (err) ->
console.error("Failed to get state for device #{uuid}. #{err}") console.error("Failed to get state for device #{uuid}. #{err}")
throw err throw err

View File

@ -33,6 +33,7 @@ bootstrap = ->
if userConfig.registered_at? if userConfig.registered_at?
return userConfig return userConfig
deviceRegister.register(resinApi, userConfig) deviceRegister.register(resinApi, userConfig)
.timeout(config.apiTimeout)
.catch DuplicateUuidError, -> .catch DuplicateUuidError, ->
resinApi.get resinApi.get
resource: 'device' resource: 'device'
@ -41,6 +42,7 @@ bootstrap = ->
uuid: userConfig.uuid uuid: userConfig.uuid
customOptions: customOptions:
apikey: userConfig.apiKey apikey: userConfig.apiKey
.timeout(config.apiTimeout)
.then ([ device ]) -> .then ([ device ]) ->
return device return device
.then (device) -> .then (device) ->

View File

@ -35,6 +35,7 @@ exports.getID = do ->
customOptions: customOptions:
apikey: apiKey apikey: apiKey
) )
.timeout(config.apiTimeout)
.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?!')
@ -192,6 +193,7 @@ do ->
body: stateDiff body: stateDiff
customOptions: customOptions:
apikey: apiKey apikey: apiKey
.timeout(config.apiTimeout)
.then -> .then ->
# Update the actual state. # Update the actual state.
_.merge(actualState, stateDiff) _.merge(actualState, stateDiff)

View File

@ -44,6 +44,7 @@ findSimilarImage = (repoTag) ->
return 'resin/scratch' return 'resin/scratch'
DELTA_REQUEST_TIMEOUT = 15 * 60 * 1000 DELTA_REQUEST_TIMEOUT = 15 * 60 * 1000
DELTA_TOTAL_TIMEOUT = 30 * 60 * 1000
getRepoAndTag = (image) -> getRepoAndTag = (image) ->
docker.getRegistryAndName(image) docker.getRegistryAndName(image)
@ -90,6 +91,7 @@ do ->
.on('id', resolve) .on('id', resolve)
.on('error', reject) .on('error', reject)
.on 'error', reject .on 'error', reject
.timeout(DELTA_TOTAL_TIMEOUT)
.then (id) -> .then (id) ->
getRepoAndTag(imgDest) getRepoAndTag(imgDest)
.then ({ repo, tag }) -> .then ({ repo, tag }) ->

View File

@ -67,6 +67,7 @@ router.post '/v1/devices', (req, res) ->
body: d body: d
customOptions: customOptions:
apikey: apiKey apikey: apiKey
.timeout(appConfig.apiTimeout)
.then (dev) -> .then (dev) ->
# If the response has id: null then something was wrong in the request # If the response has id: null then something was wrong in the request
# but we don't know precisely what. # but we don't know precisely what.
@ -168,6 +169,7 @@ router.put '/v1/devices/:uuid', (req, res) ->
body: fieldsToUpdateOnAPI body: fieldsToUpdateOnAPI
customOptions: customOptions:
apikey: apiKey apikey: apiKey
.timeout(appConfig.apiTimeout)
.then -> .then ->
knex('dependentDevice').update(fieldsToUpdateOnDB).where({ uuid }) knex('dependentDevice').update(fieldsToUpdateOnDB).where({ uuid })
.then -> .then ->
@ -278,6 +280,7 @@ exports.fetchAndSetTargetsForDependentApps = (state, fetchFn, apiKey) ->
uuid: uuid uuid: uuid
customOptions: customOptions:
apikey: apiKey apikey: apiKey
.timeout(appConfig.apiTimeout)
.then ([ dev ]) -> .then ([ dev ]) ->
deviceForDB = { deviceForDB = {
uuid: uuid uuid: uuid
@ -328,6 +331,7 @@ do ->
json: true json: true
body: stateToSend body: stateToSend
} }
.timeout(appConfig.apiTimeout)
.spread (response, body) -> .spread (response, body) ->
if response.statusCode == 200 if response.statusCode == 200
acknowledgedState[device.uuid] = formatTargetAsState(device) acknowledgedState[device.uuid] = formatTargetAsState(device)
@ -340,6 +344,7 @@ do ->
sendDeleteHook = (device, endpoint) -> sendDeleteHook = (device, endpoint) ->
uuid = device.uuid uuid = device.uuid
request.delAsync("#{endpoint}#{uuid}") request.delAsync("#{endpoint}#{uuid}")
.timeout(appConfig.apiTimeout)
.spread (response, body) -> .spread (response, body) ->
if response.statusCode == 200 if response.statusCode == 200
knex('dependentDevice').del().where({ uuid }) knex('dependentDevice').del().where({ uuid })