Skip to content

Commit

Permalink
Better and simpler handling of operational errors (#40730)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Aug 14, 2023
1 parent be3bdb1 commit 6efc911
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
34 changes: 26 additions & 8 deletions .github/actions-scripts/purge-old-workflow-runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ import fs from 'fs'
import assert from 'node:assert/strict'

import { getOctokit } from '@actions/github'
import { RequestError } from '@octokit/request-error'

main()
async function main() {
const DRY_RUN = Boolean(JSON.parse(process.env.DRY_RUN || 'false'))
const MAX_DELETIONS = parseInt(JSON.parse(process.env.MAX_DELETIONS || '100'))
const MIN_AGE_DAYS = parseInt(process.env.MIN_AGE_DAYS || '90', 10)

const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
const [owner, repo] = (process.env.GITHUB_REPOSITORY || 'github/docs-internal').split('/')
if (!owner || !repo) {
throw new Error('GITHUB_REPOSITORY environment variable not set')
}
Expand All @@ -55,13 +54,16 @@ async function main() {
repo,
})
} catch (error) {
console.log('Error happened when getting workflows')
console.warn('Status: %O', error.status)
console.warn('Message: %O', error.message)

// Generally, if it fails, it's because of a network error or
// because busy servers. It's not our fault, but considering that
// this script is supposed to run on frequent schedule, we don't
// need to fret. We'll just try again next time.
if (error instanceof RequestError && error.status >= 500) {
console.log(`RequestError: ${error.message}`)
console.log(` status: ${error.status}`)
if (isOperationalError(error.status, error.message)) {
return
} else {
throw error
}
Expand Down Expand Up @@ -90,13 +92,15 @@ async function main() {
maxDeletions: MAX_DELETIONS - deletions,
})
} catch (error) {
console.log("Error happened when calling 'deleteWorkflowRuns'")
console.warn('Status: %O', error.status)
console.warn('Message: %O', error.message)

// Generally, if it fails, it's because of a network error or
// because busy servers. It's not our fault, but considering that
// this script is supposed to run on frequent schedule, we don't
// need to fret. We'll just try again next time.
if (error instanceof RequestError && error.status >= 500) {
console.log(`RequestError: ${error.message}`)
console.log(` status: ${error.status}`)
if (isOperationalError(error.status, error.message)) {
break
} else {
throw error
Expand All @@ -111,6 +115,20 @@ async function main() {
console.log(`Deleted ${deletions} runs in total`)
}

function isOperationalError(status, message) {
if (status && status >= 500) {
return true
}
if (/Unable to delete logs while the workflow is running/.test(message)) {
return true
}
if (status === 403 && /API rate limit exceeded/.test(message)) {
return true
}

return false
}

async function deleteWorkflowRuns(
github,
owner,
Expand Down
28 changes: 1 addition & 27 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"dependencies": {
"@elastic/elasticsearch": "7.11.0",
"@github/failbot": "0.8.3",
"@octokit/request-error": "5.0.0",
"@primer/behaviors": "^1.3.3",
"@primer/css": "^21.0.1",
"@primer/octicons": "^19.1.0",
Expand Down

0 comments on commit 6efc911

Please sign in to comment.