Skip to content

Commit

Permalink
Increase x2 the polling interval for each attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
vibaiher-qatium committed Jul 6, 2023
1 parent eb20402 commit 9b2ca6d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
63 changes: 63 additions & 0 deletions __tests__/wait.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,69 @@ describe("wait", () => {
`✋Awaiting run ${input.runId - 1} ...`
);
});

it("will wait for all previous runs with exponential backoff", async () => {
input.exponentialBackoffRetries = true;
const inProgressRuns = [
{
id: 1,
status: "in_progress",
html_url: "1",
},
{
id: 2,
status: "in_progress",
html_url: "2",
},
{
id: 3,
status: "queued",
html_url: "3",
},
];
// Give the current run an id that makes it the last in the queue.
input.runId = inProgressRuns.length + 1;
// Add an in-progress run to simulate a run getting queued _after_ the one we
// are interested in.
inProgressRuns.push({
id: input.runId + 1,
status: "in_progress",
html_url: input.runId + 1 + "",
});

const mockedRunsFunc = jest.fn();
mockedRunsFunc
.mockReturnValueOnce(Promise.resolve(inProgressRuns.slice(0)))
.mockReturnValueOnce(Promise.resolve(inProgressRuns.slice(1)))
// Finally return just the run that was queued _after_ the "input" run.
.mockReturnValue(
Promise.resolve(inProgressRuns.slice(inProgressRuns.length - 1))
);

const githubClient = {
runs: mockedRunsFunc,
run: jest.fn(),
workflows: async (owner: string, repo: string) =>
Promise.resolve([workflow]),
};

const messages: Array<string> = [];
const waiter = new Waiter(
workflow.id,
githubClient,
input,
(message: string) => {
messages.push(message);
}
);
await waiter.wait();
assert.deepEqual(messages, [
`✋Awaiting run ${input.runId - 1} ...`,
`🔁 Attempt 1, next will be in 1 seconds`,
`✋Awaiting run ${input.runId - 1} ...`,
`🔁 Attempt 2, next will be in 2 seconds`,
]);
});
});
});
});
22 changes: 18 additions & 4 deletions src/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Waiter implements Wait {
private input: Input;
private githubClient: GitHub;
private workflowId: any;
private attempt: number;

constructor(
workflowId: any,
Expand All @@ -22,9 +23,12 @@ export class Waiter implements Wait {
this.input = input;
this.githubClient = githubClient;
this.info = info;
this.attempt = 0;
}

wait = async (secondsSoFar?: number) => {
let pollingInterval = this.input.pollIntervalSeconds;

if (
this.input.continueAfterSeconds &&
(secondsSoFar || 0) >= this.input.continueAfterSeconds
Expand Down Expand Up @@ -60,9 +64,19 @@ export class Waiter implements Wait {

const previousRun = previousRuns[0];
this.info(`✋Awaiting run ${previousRun.html_url} ...`);
await new Promise((resolve) =>
setTimeout(resolve, this.input.pollIntervalSeconds * 1000)
);
return this.wait((secondsSoFar || 0) + this.input.pollIntervalSeconds);

if (this.input.exponentialBackoffRetries) {
pollingInterval =
this.input.pollIntervalSeconds * (2 * this.attempt || 1);
this.info(
`🔁 Attempt ${
this.attempt + 1
}, next will be in ${pollingInterval} seconds`
);
this.attempt++;
}

await new Promise((resolve) => setTimeout(resolve, pollingInterval * 1000));
return this.wait((secondsSoFar || 0) + pollingInterval);
};
}

0 comments on commit 9b2ca6d

Please sign in to comment.