mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-01-18 02:39:49 +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 type * as Stream from 'stream';
|
||||
|
||||
import { retry } from '../helpers';
|
||||
import Logger = require('../logger');
|
||||
import * as ApiErrors from './errors';
|
||||
|
||||
@ -260,24 +261,35 @@ export class DeviceAPI {
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
return request(opts, (err, response, body) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
switch (response.statusCode) {
|
||||
case 200:
|
||||
return resolve(body);
|
||||
case 400:
|
||||
return reject(new ApiErrors.BadRequestDeviceAPIError(body.message));
|
||||
case 503:
|
||||
return reject(
|
||||
new ApiErrors.ServiceUnavailableAPIError(body.message),
|
||||
);
|
||||
default:
|
||||
return reject(new ApiErrors.DeviceAPIError(body.message));
|
||||
}
|
||||
const doRequest = async () => {
|
||||
return await new Promise((resolve, reject) => {
|
||||
return request(opts, (err, response, body) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
switch (response.statusCode) {
|
||||
case 200:
|
||||
return resolve(body);
|
||||
case 400:
|
||||
return reject(
|
||||
new ApiErrors.BadRequestDeviceAPIError(body.message),
|
||||
);
|
||||
case 503:
|
||||
return reject(
|
||||
new ApiErrors.ServiceUnavailableAPIError(body.message),
|
||||
);
|
||||
default:
|
||||
return reject(new ApiErrors.DeviceAPIError(body.message));
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
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) => {
|
||||
logger.logWarn(`Error parsing NDJSON log chunk: ${e}`);
|
||||
});
|
||||
logs.once('error', reject);
|
||||
logs.once('end', () => {
|
||||
logs.once('error', handleError);
|
||||
logs.once('end', handleError);
|
||||
logs.pipe(jsonStream);
|
||||
|
||||
function handleError(error?: Error | string) {
|
||||
logger.logWarn(DeviceConnectionLostError.defaultMsg);
|
||||
if (gotSignal) {
|
||||
reject(new SIGINTError('Log streaming aborted on SIGINT signal'));
|
||||
} else {
|
||||
reject(new DeviceConnectionLostError());
|
||||
const msg = typeof error === 'string' ? error : error?.message;
|
||||
reject(new DeviceConnectionLostError(msg));
|
||||
}
|
||||
});
|
||||
logs.pipe(jsonStream);
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
process.removeListener('SIGINT', handleSignal);
|
||||
|
Loading…
Reference in New Issue
Block a user