Skip to content

Commit

Permalink
perf: cancel workflow runs in parallel
Browse files Browse the repository at this point in the history
This was suggested by @Gisleburt in styfle#59

I quote:

"The next problem we has is that we have multiple jobs starting at once, downloading a list of other jobs and trying to cancel them one at a time. GitHub doesn't wait for the job to be cancelled before returning a 202 response so its possible for two jobs to cancel each other. In order to reduce the chance of this happening we decided to send all of the cancellations in one go, and wait for the 202s in one lump at the end. This change will improve the speed of the task for everyone with more than one workflow to cancel."
  • Loading branch information
mikehardy committed Apr 1, 2021
1 parent 66e9135 commit c4d438d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
10 changes: 8 additions & 2 deletions dist/index.js
Expand Up @@ -5908,15 +5908,21 @@ async function main() {
}
async function cancelWorkflowRuns(runningWorkflows, owner, repo, token) {
const octokit = github.getOctokit(token);
const promises = [];
for (const { id, head_sha, status, html_url } of runningWorkflows) {
console.log('Canceling run: ', { id, head_sha, status, html_url });
const res = await octokit.actions.cancelWorkflowRun({
const current_promise = octokit.actions
.cancelWorkflowRun({
owner,
repo,
run_id: id
})
.then(res => {
console.log(`Cancel run ${id} responded with status ${res.status}`);
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
promises.push(current_promise);
}
await Promise.all(promises);
}
main()
.then(() => core.info('Cancel Complete.'))
Expand Down
19 changes: 12 additions & 7 deletions src/index.ts
Expand Up @@ -96,16 +96,21 @@ async function cancelWorkflowRuns(
token: string
): Promise<void> {
const octokit = github.getOctokit(token);

const promises = [];
for (const { id, head_sha, status, html_url } of runningWorkflows) {
console.log('Canceling run: ', { id, head_sha, status, html_url });
const res = await octokit.actions.cancelWorkflowRun({
owner,
repo,
run_id: id
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
const current_promise = octokit.actions
.cancelWorkflowRun({
owner,
repo,
run_id: id
})
.then(res => {
console.log(`Cancel run ${id} responded with status ${res.status}`);
});
promises.push(current_promise);
}
await Promise.all(promises);
}

main()
Expand Down

0 comments on commit c4d438d

Please sign in to comment.