mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-01-30 16:13:59 +00:00
push: Await and retry supervisor API requests to a local device
Change-type: patch
This commit is contained in:
parent
a4fc95e99b
commit
b30075a18b
@ -20,6 +20,7 @@ import * as os from 'os';
|
|||||||
import * as request from 'request';
|
import * as request from 'request';
|
||||||
import type * as Stream from 'stream';
|
import type * as Stream from 'stream';
|
||||||
|
|
||||||
|
import { retry } from '../helpers';
|
||||||
import Logger = require('../logger');
|
import Logger = require('../logger');
|
||||||
import * as ApiErrors from './errors';
|
import * as ApiErrors from './errors';
|
||||||
|
|
||||||
@ -260,7 +261,8 @@ export class DeviceAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
const doRequest = async () => {
|
||||||
|
return await new Promise((resolve, reject) => {
|
||||||
return request(opts, (err, response, body) => {
|
return request(opts, (err, response, body) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err);
|
return reject(err);
|
||||||
@ -269,7 +271,9 @@ export class DeviceAPI {
|
|||||||
case 200:
|
case 200:
|
||||||
return resolve(body);
|
return resolve(body);
|
||||||
case 400:
|
case 400:
|
||||||
return reject(new ApiErrors.BadRequestDeviceAPIError(body.message));
|
return reject(
|
||||||
|
new ApiErrors.BadRequestDeviceAPIError(body.message),
|
||||||
|
);
|
||||||
case 503:
|
case 503:
|
||||||
return reject(
|
return reject(
|
||||||
new ApiErrors.ServiceUnavailableAPIError(body.message),
|
new ApiErrors.ServiceUnavailableAPIError(body.message),
|
||||||
@ -279,6 +283,14 @@ export class DeviceAPI {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return await retry({
|
||||||
|
func: doRequest,
|
||||||
|
initialDelayMs: 2000,
|
||||||
|
maxAttempts: 6,
|
||||||
|
label: `Supervisor API (${opts.method} ${(opts as any).url})`,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,16 +80,19 @@ async function displayDeviceLogs(
|
|||||||
jsonStream.on('error', (e) => {
|
jsonStream.on('error', (e) => {
|
||||||
logger.logWarn(`Error parsing NDJSON log chunk: ${e}`);
|
logger.logWarn(`Error parsing NDJSON log chunk: ${e}`);
|
||||||
});
|
});
|
||||||
logs.once('error', reject);
|
logs.once('error', handleError);
|
||||||
logs.once('end', () => {
|
logs.once('end', handleError);
|
||||||
|
logs.pipe(jsonStream);
|
||||||
|
|
||||||
|
function handleError(error?: Error | string) {
|
||||||
logger.logWarn(DeviceConnectionLostError.defaultMsg);
|
logger.logWarn(DeviceConnectionLostError.defaultMsg);
|
||||||
if (gotSignal) {
|
if (gotSignal) {
|
||||||
reject(new SIGINTError('Log streaming aborted on SIGINT signal'));
|
reject(new SIGINTError('Log streaming aborted on SIGINT signal'));
|
||||||
} else {
|
} else {
|
||||||
reject(new DeviceConnectionLostError());
|
const msg = typeof error === 'string' ? error : error?.message;
|
||||||
|
reject(new DeviceConnectionLostError(msg));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
logs.pipe(jsonStream);
|
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
process.removeListener('SIGINT', handleSignal);
|
process.removeListener('SIGINT', handleSignal);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user