Skip to content

Commit 368f825

Browse files
authored
Merge pull request #444 from actions/yoannchaudet-deployment-polling-backoff
Add backoff and jitter to deployment polling
2 parents 5e98f10 + 7e97763 commit 368f825

6 files changed

Lines changed: 211 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
| `token` | `true` | `${{ github.token }}` | The GitHub token used to create an authenticated client - Provided for you by default! |
5252
| `timeout` | `false` | `"600000"` | Time in milliseconds after which to timeout and cancel the deployment (default: 10 minutes) |
5353
| `error_count` | `false` | `"10"` | Maximum number of status report errors before cancelling a deployment (default: 10) |
54-
| `reporting_interval` | `false` | `"5000"` | Time in milliseconds between two deployment status reports (default: 5 seconds) |
54+
| `reporting_interval` | `false` | `"5000"` | Initial time in milliseconds between deployment status reports. Successful non-terminal polls use exponential backoff up to 30 seconds, or the configured interval when higher, with ±20% jitter. Error backoff is added separately (default: 5 seconds). |
5555
| `artifact_name` | `false` | `"github-pages"` | The name of the artifact to deploy |
5656
| `preview` | `false` | `"false"` | Is this attempting to deploy a pull request as a GitHub Pages preview site? (NOTE: This feature is only in alpha currently and is not available to the public!) |
5757

‎action.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ inputs:
1818
required: false
1919
default: '10'
2020
reporting_interval:
21-
description: 'Time in milliseconds between two deployment status report (default: 5 seconds)'
21+
description: 'Initial time between deployment status reports; successful polls use capped backoff and jitter, with error backoff added separately (default: 5 seconds)'
2222
required: false
2323
default: '5000'
2424
artifact_name:

‎dist/index.js‎

Lines changed: 30 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/__tests__/internal/deployment.test.js‎

Lines changed: 148 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ const nock = require('nock')
44
// For mocking network calls with native Fetch (octokit)
55
const { MockAgent, setGlobalDispatcher } = require('undici')
66

7-
const { Deployment, MAX_TIMEOUT, ONE_GIGABYTE, SIZE_LIMIT_DESCRIPTION } = require('../../internal/deployment')
7+
const {
8+
Deployment,
9+
MAX_TIMEOUT,
10+
DEFAULT_REPORTING_INTERVAL,
11+
MAX_REPORTING_INTERVAL,
12+
ONE_GIGABYTE,
13+
SIZE_LIMIT_DESCRIPTION
14+
} = require('../../internal/deployment')
815

916
const fakeJwt =
1017
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiNjllMWIxOC1jOGFiLTRhZGQtOGYxOC03MzVlMzVjZGJhZjAiLCJzdWIiOiJyZXBvOnBhcGVyLXNwYS9taW55aTplbnZpcm9ubWVudDpQcm9kdWN0aW9uIiwiYXVkIjoiaHR0cHM6Ly9naXRodWIuY29tL3BhcGVyLXNwYSIsInJlZiI6InJlZnMvaGVhZHMvbWFpbiIsInNoYSI6ImEyODU1MWJmODdiZDk3NTFiMzdiMmM0YjM3M2MxZjU3NjFmYWM2MjYiLCJyZXBvc2l0b3J5IjoicGFwZXItc3BhL21pbnlpIiwicmVwb3NpdG9yeV9vd25lciI6InBhcGVyLXNwYSIsInJ1bl9pZCI6IjE1NDY0NTkzNjQiLCJydW5fbnVtYmVyIjoiMzQiLCJydW5fYXR0ZW1wdCI6IjIiLCJhY3RvciI6IllpTXlzdHkiLCJ3b3JrZmxvdyI6IkNJIiwiaGVhZF9yZWYiOiIiLCJiYXNlX3JlZiI6IiIsImV2ZW50X25hbWUiOiJwdXNoIiwicmVmX3R5cGUiOiJicmFuY2giLCJlbnZpcm9ubWVudCI6IlByb2R1Y3Rpb24iLCJqb2Jfd29ya2Zsb3dfcmVmIjoicGFwZXItc3BhL21pbnlpLy5naXRodWIvd29ya2Zsb3dzL2JsYW5rLnltbEByZWZzL2hlYWRzL21haW4iLCJpc3MiOiJodHRwczovL3Rva2VuLmFjdGlvbnMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwibmJmIjoxNjM4ODI4MDI4LCJleHAiOjE2Mzg4Mjg5MjgsImlhdCI6MTYzODgyODYyOH0.1wyupfxu1HGoTyIqatYg0hIxy2-0bMO-yVlmLSMuu2w'
@@ -35,7 +42,7 @@ describe('Deployment', () => {
3542
case 'token':
3643
return process.env.GITHUB_TOKEN
3744
case 'reporting_interval':
38-
return 50 // Lower reporting interval to speed up test
45+
return process.env.INPUT_REPORTING_INTERVAL || 50 // Lower reporting interval to speed up test
3946
default:
4047
return process.env[`INPUT_${param.toUpperCase()}`] || ''
4148
}
@@ -608,6 +615,42 @@ describe('Deployment', () => {
608615
})
609616

610617
describe('#check', () => {
618+
afterEach(() => {
619+
jest.restoreAllMocks()
620+
delete process.env.INPUT_ERROR_COUNT
621+
delete process.env.INPUT_REPORTING_INTERVAL
622+
})
623+
624+
const mockDeploymentStatus = (status, times = 1) => {
625+
mockPool
626+
.intercept({
627+
path: `/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments/${process.env.GITHUB_SHA}`,
628+
method: 'GET'
629+
})
630+
.reply(200, { status }, { headers: { 'content-type': 'application/json' } })
631+
.times(times)
632+
}
633+
634+
const createPendingDeployment = () => {
635+
const deployment = new Deployment()
636+
deployment.deploymentInfo = {
637+
id: process.env.GITHUB_SHA,
638+
pending: true
639+
}
640+
deployment.startTime = Date.now()
641+
return deployment
642+
}
643+
644+
const runWithoutWaiting = async deployment => {
645+
const timeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation(resolve => {
646+
resolve()
647+
return 0
648+
})
649+
650+
await deployment.check()
651+
return timeoutSpy
652+
}
653+
611654
it('sets output to success when deployment is successful', async () => {
612655
process.env.GITHUB_SHA = 'valid-build-version'
613656

@@ -964,7 +1007,7 @@ describe('Deployment', () => {
9641007
case 'error_count':
9651008
return 10
9661009
case 'reporting_interval':
967-
return 0 // The default of 5000 is too long for the test
1010+
return 1 // The default of 5000 is too long for the test
9681011
case 'timeout':
9691012
return 42
9701013
default:
@@ -990,6 +1033,108 @@ describe('Deployment', () => {
9901033
expect(core.info).toHaveBeenLastCalledWith('Reported success!')
9911034
twirpScope.done()
9921035
})
1036+
1037+
it('backs off successful non-terminal status checks', async () => {
1038+
process.env.GITHUB_SHA = 'valid-build-version'
1039+
process.env.INPUT_ERROR_COUNT = '10'
1040+
mockDeploymentStatus('deployment_in_progress', 2)
1041+
mockDeploymentStatus('succeed')
1042+
const randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0.5)
1043+
1044+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1045+
1046+
expect(timeoutSpy).toHaveBeenNthCalledWith(1, expect.any(Function), 50)
1047+
expect(timeoutSpy).toHaveBeenNthCalledWith(2, expect.any(Function), 75)
1048+
expect(timeoutSpy).toHaveBeenNthCalledWith(3, expect.any(Function), 113)
1049+
1050+
timeoutSpy.mockRestore()
1051+
randomSpy.mockRestore()
1052+
delete process.env.INPUT_ERROR_COUNT
1053+
})
1054+
1055+
it('caps the successful status check backoff', async () => {
1056+
process.env.GITHUB_SHA = 'valid-build-version'
1057+
process.env.INPUT_ERROR_COUNT = '10'
1058+
process.env.INPUT_REPORTING_INTERVAL = '20000'
1059+
mockDeploymentStatus('deployment_in_progress')
1060+
mockDeploymentStatus('succeed')
1061+
const randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0.5)
1062+
1063+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1064+
1065+
expect(timeoutSpy.mock.calls.map(([, interval]) => interval)).toEqual([20000, MAX_REPORTING_INTERVAL])
1066+
1067+
timeoutSpy.mockRestore()
1068+
randomSpy.mockRestore()
1069+
})
1070+
1071+
it('does not reduce a configured interval above the backoff cap', async () => {
1072+
process.env.GITHUB_SHA = 'valid-build-version'
1073+
process.env.INPUT_ERROR_COUNT = '10'
1074+
process.env.INPUT_REPORTING_INTERVAL = '45000'
1075+
mockDeploymentStatus('deployment_in_progress')
1076+
mockDeploymentStatus('succeed')
1077+
jest.spyOn(Math, 'random').mockReturnValue(0.5)
1078+
1079+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1080+
1081+
expect(timeoutSpy.mock.calls.map(([, interval]) => interval)).toEqual([45000, 45000])
1082+
})
1083+
1084+
it.each(['not-a-number', '0', '-1'])(
1085+
'uses the default reporting interval for invalid input %s',
1086+
async reportingInterval => {
1087+
process.env.GITHUB_SHA = 'valid-build-version'
1088+
process.env.INPUT_ERROR_COUNT = '10'
1089+
process.env.INPUT_REPORTING_INTERVAL = reportingInterval
1090+
mockDeploymentStatus('succeed')
1091+
jest.spyOn(Math, 'random').mockReturnValue(0.5)
1092+
1093+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1094+
1095+
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), DEFAULT_REPORTING_INTERVAL)
1096+
expect(core.warning).toHaveBeenCalledWith(
1097+
`Invalid reporting_interval value; using the default of ${DEFAULT_REPORTING_INTERVAL} milliseconds.`
1098+
)
1099+
}
1100+
)
1101+
1102+
it('jitters status check intervals by up to twenty percent', async () => {
1103+
process.env.GITHUB_SHA = 'valid-build-version'
1104+
process.env.INPUT_ERROR_COUNT = '10'
1105+
mockDeploymentStatus('deployment_in_progress')
1106+
mockDeploymentStatus('succeed')
1107+
const randomSpy = jest.spyOn(Math, 'random').mockReturnValueOnce(0).mockReturnValueOnce(1)
1108+
1109+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1110+
1111+
expect(timeoutSpy).toHaveBeenNthCalledWith(1, expect.any(Function), 40)
1112+
expect(timeoutSpy).toHaveBeenNthCalledWith(2, expect.any(Function), 90)
1113+
1114+
timeoutSpy.mockRestore()
1115+
randomSpy.mockRestore()
1116+
})
1117+
1118+
it('keeps success backoff separate from error backoff', async () => {
1119+
process.env.GITHUB_SHA = 'valid-build-version'
1120+
process.env.INPUT_ERROR_COUNT = '10'
1121+
mockPool
1122+
.intercept({
1123+
path: `/repos/${process.env.GITHUB_REPOSITORY}/pages/deployments/${process.env.GITHUB_SHA}`,
1124+
method: 'GET'
1125+
})
1126+
.reply(500, {}, { headers: { 'content-type': 'application/json' } })
1127+
mockDeploymentStatus('deployment_in_progress')
1128+
mockDeploymentStatus('succeed')
1129+
const randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0.5)
1130+
1131+
const timeoutSpy = await runWithoutWaiting(createPendingDeployment())
1132+
1133+
expect(timeoutSpy.mock.calls.map(([, interval]) => interval)).toEqual([50, 51, 75])
1134+
1135+
timeoutSpy.mockRestore()
1136+
randomSpy.mockRestore()
1137+
})
9931138
})
9941139

9951140
describe('#cancel', () => {

‎src/internal/deployment.js‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ const finalErrorStatus = {
2424
}
2525

2626
const MAX_TIMEOUT = 600000
27+
const DEFAULT_REPORTING_INTERVAL = 5000
28+
const MAX_REPORTING_INTERVAL = 30000
29+
const REPORTING_BACKOFF_MULTIPLIER = 1.5
30+
const REPORTING_JITTER_FACTOR = 0.2
2731
const ONE_GIGABYTE = 1073741824
2832
const SIZE_LIMIT_DESCRIPTION = '1 GB'
2933

34+
function getJitteredInterval(interval) {
35+
const jitter = interval * REPORTING_JITTER_FACTOR
36+
return Math.round(interval - jitter + Math.random() * jitter * 2)
37+
}
38+
3039
class Deployment {
3140
constructor() {
3241
const context = getContext()
@@ -138,9 +147,19 @@ class Deployment {
138147
}
139148

140149
const deploymentId = this.deploymentInfo.id || this.buildVersion
141-
const reportingInterval = Number(core.getInput('reporting_interval'))
150+
const reportingIntervalInput = Number(core.getInput('reporting_interval'))
151+
const initialReportingInterval =
152+
Number.isFinite(reportingIntervalInput) && reportingIntervalInput > 0
153+
? reportingIntervalInput
154+
: DEFAULT_REPORTING_INTERVAL
155+
const maxReportingInterval = Math.max(MAX_REPORTING_INTERVAL, initialReportingInterval)
142156
const maxErrorCount = Number(core.getInput('error_count'))
143157

158+
if (initialReportingInterval !== reportingIntervalInput) {
159+
core.warning(`Invalid reporting_interval value; using the default of ${DEFAULT_REPORTING_INTERVAL} milliseconds.`)
160+
}
161+
162+
let reportingInterval = initialReportingInterval
144163
let errorCount = 0
145164

146165
// Time in milliseconds between two deployment status report when status errored, default 0.
@@ -151,7 +170,7 @@ class Deployment {
151170
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
152171
while (true) {
153172
// Handle reporting interval
154-
await new Promise(resolve => setTimeout(resolve, reportingInterval + errorReportingInterval))
173+
await new Promise(resolve => setTimeout(resolve, getJitteredInterval(reportingInterval + errorReportingInterval)))
155174

156175
// Check status
157176
try {
@@ -179,6 +198,7 @@ class Deployment {
179198

180199
// reset the error reporting interval once get the proper status back.
181200
errorReportingInterval = 0
201+
reportingInterval = Math.min(Math.round(reportingInterval * REPORTING_BACKOFF_MULTIPLIER), maxReportingInterval)
182202
} catch (error) {
183203
core.error(error.stack)
184204

@@ -242,4 +262,11 @@ class Deployment {
242262
}
243263
}
244264

245-
module.exports = { Deployment, MAX_TIMEOUT, ONE_GIGABYTE, SIZE_LIMIT_DESCRIPTION }
265+
module.exports = {
266+
Deployment,
267+
MAX_TIMEOUT,
268+
DEFAULT_REPORTING_INTERVAL,
269+
MAX_REPORTING_INTERVAL,
270+
ONE_GIGABYTE,
271+
SIZE_LIMIT_DESCRIPTION
272+
}

0 commit comments

Comments
 (0)