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