From b30075a18b79ec032943e77d9585b24f48730fa7 Mon Sep 17 00:00:00 2001 From: Paulo Castro Date: Thu, 9 Sep 2021 22:21:14 +0100 Subject: [PATCH] push: Await and retry supervisor API requests to a local device Change-type: patch --- lib/utils/device/api.ts | 46 +++++++++++++++++++++++++--------------- lib/utils/device/logs.ts | 13 +++++++----- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/lib/utils/device/api.ts b/lib/utils/device/api.ts index 124ea471..bff6ffbf 100644 --- a/lib/utils/device/api.ts +++ b/lib/utils/device/api.ts @@ -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})`, }); } } diff --git a/lib/utils/device/logs.ts b/lib/utils/device/logs.ts index 125e75cf..1e6fee8b 100644 --- a/lib/utils/device/logs.ts +++ b/lib/utils/device/logs.ts @@ -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);