From 21e8728a0ad0129eaf2e4a21f271cb549465397b Mon Sep 17 00:00:00 2001 From: Devin Jean Date: Fri, 9 Aug 2024 13:42:36 -0500 Subject: [PATCH 1/2] reduced logging + show retries --- src/procedures/matlab/matlab.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/procedures/matlab/matlab.js b/src/procedures/matlab/matlab.js index 907a5cac..d2c0f357 100644 --- a/src/procedures/matlab/matlab.js +++ b/src/procedures/matlab/matlab.js @@ -23,16 +23,19 @@ const MATLAB = {}; MATLAB.serviceName = "MATLAB"; async function requestWithRetry(url, body, numRetries = 0) { - try { - return await request.post(url, body, { - timeout: 10000, - }); - } catch (err) { - if (err.code === "ECONNABORTED" && numRetries > 0) { - return requestWithRetry(url, body, numRetries - 1); + let err = undefined; + for (let attempts = 1; attempts <= 1 + numRetries; ++attempts) { + try { + const res = await request.post(url, body, { timeout: 10000 }); + return [res, attempts]; + } catch (e) { + err = e; + if (e.code !== "ECONNABORTED") { + break; + } } - throw err; } + throw err || Error('no attempts were made'); } /** @@ -126,15 +129,11 @@ MATLAB.function = async function (fn, args = [], numReturnValues = 1) { // - start // - batch requests while starting // - send requests on start - // - keepWarm + const startTime = Date.now(); - const resp = await requestWithRetry(`${MATLAB_URL}/feval-fast`, body, 5); + const [resp, attempts] = await requestWithRetry(`${MATLAB_URL}/feval-fast`, body, 5); const duration = Date.now() - startTime; - logger.info( - `${duration} body: ${JSON.stringify(body)} response: ${ - JSON.stringify(resp.data) - }`, - ); + logger.info(`matlab response -- duration: ${duration / 1000}s, attempts: ${attempts}`) const results = resp.data.FEvalResponse; // TODO: add batching queue From 474200710904e4bfc550e2a1038c28dd121a747b Mon Sep 17 00:00:00 2001 From: Format Bot Date: Fri, 9 Aug 2024 18:43:20 +0000 Subject: [PATCH 2/2] Fix code formatting --- src/procedures/matlab/matlab.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/procedures/matlab/matlab.js b/src/procedures/matlab/matlab.js index d2c0f357..dd8cfe9e 100644 --- a/src/procedures/matlab/matlab.js +++ b/src/procedures/matlab/matlab.js @@ -35,7 +35,7 @@ async function requestWithRetry(url, body, numRetries = 0) { } } } - throw err || Error('no attempts were made'); + throw err || Error("no attempts were made"); } /** @@ -97,7 +97,11 @@ MATLAB.imageFromMatrix = async function (matrix) { for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { const [r, g, b, a = 255] = matrix[y][x]; - res.setPixelColor(jimp.rgbaToInt(clamp(r), clamp(g), clamp(b), clamp(a)), x, y); + res.setPixelColor( + jimp.rgbaToInt(clamp(r), clamp(g), clamp(b), clamp(a)), + x, + y, + ); } } return utils.sendImageBuffer( @@ -131,9 +135,15 @@ MATLAB.function = async function (fn, args = [], numReturnValues = 1) { // - send requests on start const startTime = Date.now(); - const [resp, attempts] = await requestWithRetry(`${MATLAB_URL}/feval-fast`, body, 5); + const [resp, attempts] = await requestWithRetry( + `${MATLAB_URL}/feval-fast`, + body, + 5, + ); const duration = Date.now() - startTime; - logger.info(`matlab response -- duration: ${duration / 1000}s, attempts: ${attempts}`) + logger.info( + `matlab response -- duration: ${duration / 1000}s, attempts: ${attempts}`, + ); const results = resp.data.FEvalResponse; // TODO: add batching queue