Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to configure action retries #108

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@semrush/purr",
"version": "3.15.8",
"version": "3.15.10",
"description": "",
"main": "src/cli.cjs",
"scripts": {
Expand Down
37 changes: 18 additions & 19 deletions src/check/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ class CheckRunner {

result = result.then(async () => {
const actionReport = new ActionReport(stepName, step, '***hidden***');
const maxRetries = 5;
const baseDelay = 1000;

// eslint-disable-next-line consistent-return
const runAction = async (attempt = 1) => {
Expand Down Expand Up @@ -215,23 +213,24 @@ class CheckRunner {
});
}
} catch (err) {
const isResetError = err.message.includes('ERR_CONNECTION_RESET');
const isClosedError = err.message.includes('ERR_CONNECTION_CLOSED');
if (isResetError || isClosedError) {
log.error('ERR_CONNECTION_* error occurred: ', err);
}
if ((isResetError || isClosedError) && attempt <= maxRetries) {
const jitter = Math.random() * 1000;
const delay = baseDelay * attempt + jitter;
log.warn(
`Retrying action '${stepName}' (Attempt ${attempt} of ${maxRetries}) after ${delay.toFixed(
0
)}ms due to connection issue.`
);
await new Promise((resolve) => {
setTimeout(resolve, delay);
});
return runAction(attempt + 1);
const isRetryableError = config.actionRetryErrors.some((item) =>
err.message.includes(item)
);
if (isRetryableError) {
log.error('Retryable error occurred: ', err);
if (attempt <= config.actionRetryCount) {
const jitter = Math.random() * 1000;
const delay = config.actionRetryTimeout * attempt + jitter;
log.warn(
`Retrying action '${stepName}' (Attempt ${attempt} of ${config.actionRetryCount}) after ${delay.toFixed(
0
)}ms due to connection issue.`
);
await new Promise((resolve) => {
setTimeout(resolve, delay);
});
return runAction(attempt + 1);
}
}
actionReport.success = false;
actionReport.shortMessage = err.message;
Expand Down
3 changes: 3 additions & 0 deletions src/config/__tests__/configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ describe('Test Configuration class', () => {
const configuration = new Configuration(envParams, rootDir);

const defaultConfiguration = {
actionRetryCount: 5,
actionRetryErrors: ['ERR_CONNECTION_RESET', 'ERR_CONNECTION_CLOSED'],
actionRetryTimeout: 1000,
apiUrlPrefix: '/api/v1',
apiWaitTimeout: 27000,
artifactsDir: '/rootDir/storage',
Expand Down
12 changes: 12 additions & 0 deletions src/config/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ class Configuration {
180000
);

this.actionRetryErrors = getDefault(
envParams.ACTION_RETRY_ERRORS,
'ERR_CONNECTION_RESET,ERR_CONNECTION_CLOSED'
)
.split(',')
.map((msg) => msg.trim())
.filter((msg) => msg.length > 0);

this.actionRetryCount = getDefault(envParams.ACTION_RETRY_COUNT, 5);

this.actionRetryTimeout = getDefault(envParams.ACTION_RETRY_TIMEOUT, 1000);

if (this.artifactsGroupByCheckName && isUndefined(this.artifactsDir)) {
throw new Error(
'Enabled group artifacts by check name and artifacts path not specified'
Expand Down
24 changes: 24 additions & 0 deletions src/config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,30 @@ const DEFAULT_ENV_PARAMS = {
* @default {180000}
*/
BROWSER_PROTOCOL_TIMEOUT: 180000,

/**
* Check action retry comma-separated errors list (checked by err.message.includes())
*
* @type string
* @default ERR_CONNECTION_RESET,ERR_CONNECTION_CLOSED
*/
ACTION_RETRY_ERRORS: 'ERR_CONNECTION_RESET,ERR_CONNECTION_CLOSED',

/**
* Check action retry count
*
* @type number
* @default 5
*/
ACTION_RETRY_COUNT: 5,

/**
* Check action retry timeout (ms)
*
* @type number
* @default 1000
*/
ACTION_RETRY_TIMEOUT: 1000,
};

class EnvParams {
Expand Down
Loading