Browse Source

Keep trying to scale if deployment is not yet ready (#701)

* Keep on polling now-scale when deployment is not ready

* Use `ms`
master
Jarmo Isotalo 8 years ago
committed by Leo Lamprecht
parent
commit
1daaf3037b
  1. 89
      lib/index.js

89
lib/index.js

@ -14,9 +14,9 @@ const retry = require('async-retry')
const splitArray = require('split-array') const splitArray = require('split-array')
const { parse: parseIni } = require('ini') const { parse: parseIni } = require('ini')
const { readFile, stat, lstat } = require('fs-extra') const { readFile, stat, lstat } = require('fs-extra')
const { responseError } = require('./error') const ms = require('ms')
// Ours // Utilities
const { const {
staticFiles: getFiles, staticFiles: getFiles,
npm: getNpmFiles, npm: getNpmFiles,
@ -26,6 +26,7 @@ const ua = require('./ua')
const hash = require('./hash') const hash = require('./hash')
const Agent = require('./agent') const Agent = require('./agent')
const toHost = require('./to-host') const toHost = require('./to-host')
const { responseError } = require('./error')
// How many concurrent HTTP/2 stream uploads // How many concurrent HTTP/2 stream uploads
const MAX_CONCURRENT = 10 const MAX_CONCURRENT = 10
@ -918,47 +919,67 @@ module.exports = class Now extends EventEmitter {
} }
setScale(nameOrId, scale) { setScale(nameOrId, scale) {
return this.retry(async (bail, attempt) => { return this.retry(
if (this._debug) { async (bail, attempt) => {
console.time( if (this._debug) {
`> [debug] #${attempt} POST /deployments/${nameOrId}/instances` console.time(
`> [debug] #${attempt} POST /deployments/${nameOrId}/instances`
)
}
const res = await this._fetch(
`/now/deployments/${nameOrId}/instances`,
{
method: 'POST',
body: scale
}
) )
}
const res = await this._fetch(`/now/deployments/${nameOrId}/instances`, { if (this._debug) {
method: 'POST', console.timeEnd(
body: scale `> [debug] #${attempt} POST /deployments/${nameOrId}/instances`
}) )
}
if (this._debug) { if (res.status === 403) {
console.timeEnd( return bail(new Error('Unauthorized'))
`> [debug] #${attempt} POST /deployments/${nameOrId}/instances` }
)
}
if (res.status === 403) { const body = await res.json()
return bail(new Error('Unauthorized'))
}
const body = await res.json() if (res.status !== 200) {
if (res.status === 404 || res.status === 400) {
if (
body &&
body.error &&
body.error.code &&
body.error.code === 'not_snapshotted'
) {
throw new Error(body.error.message)
}
const err = new Error(body.error.message)
err.userError = true
return bail(err)
}
if (res.status !== 200) { if (body.error && body.error.message) {
if (res.status === 404 || res.status === 400) { const err = new Error(body.error.message)
const err = new Error(body.error.message) err.userError = true
err.userError = true return bail(err)
return bail(err) }
throw new Error(
`Error occurred while scaling. Please try again later`
)
} }
if (body.error && body.error.message) { return body
const err = new Error(body.error.message) },
err.userError = true {
return bail(err) retries: 300,
} maxTimeout: ms('5s'),
throw new Error(`Error occurred while scaling. Please try again later`) factor: 1.1
} }
)
return body
})
} }
async unfreeze(depl) { async unfreeze(depl) {

Loading…
Cancel
Save