From c4d438d01edc932817de02b4e2cdfba697b392e4 Mon Sep 17 00:00:00 2001 From: Mike Hardy Date: Thu, 1 Apr 2021 12:43:16 -0500 Subject: [PATCH] perf: cancel workflow runs in parallel This was suggested by @Gisleburt in https://github.com/styfle/cancel-workflow-action/pull/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." --- dist/index.js | 10 ++++++++-- src/index.ts | 19 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/dist/index.js b/dist/index.js index a0b1cd7b..b8a3bf29 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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.')) diff --git a/src/index.ts b/src/index.ts index ba9eb5f7..7f927e9a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -96,16 +96,21 @@ async function cancelWorkflowRuns( token: string ): Promise { 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()