Skip to content

Commit fbdd94b

Browse files
committed
travis: reduce number of retry and make it configurable
1 parent af20ad8 commit fbdd94b

File tree

4 files changed

+590
-16
lines changed

4 files changed

+590
-16
lines changed

lib/pollTravis.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ function pollByCommitThenStatus (options) {
3333
const optsWithCommit = Object.assign({ lastCommit }, options)
3434

3535
// attach last commit sha to all subsequent logs
36-
optsWithCommit.logger = options.logger.child({ commit: lastCommit.sha }, true)
36+
optsWithCommit.logger = options.logger.child({
37+
commit: lastCommit.sha,
38+
retry: options.retry,
39+
interval: options.interval
40+
}, true)
3741

3842
pollTravisBuildBySha(optsWithCommit)
3943
optsWithCommit.logger.info('Started polling Travis for build by commit')
@@ -48,7 +52,7 @@ function pollTravisBuildBySha (options, checkNumber = 1) {
4852
pr: options.pr
4953
}
5054

51-
if (checkNumber > 100) {
55+
if (checkNumber > options.retry) {
5256
options.logger.warn('Was not able to find matching build for PR, stopping poll now :(')
5357
return
5458
}
@@ -65,9 +69,10 @@ function pollTravisBuildBySha (options, checkNumber = 1) {
6569
return matchesCommitDate && matchesShaOrPr
6670
})
6771

72+
const interval = options.interval
6873
if (!matchingCommit) {
69-
options.logger.warn(`Travis hasn't picked up last commit yet, will do check #${checkNumber + 1} in 30 seconds`)
70-
return setTimeout(pollTravisBuildBySha, 30 * 1000, options, checkNumber + 1)
74+
options.logger.warn(`Travis hasn't picked up last commit yet, will do check #${checkNumber + 1} in ${interval} seconds`)
75+
return setTimeout(pollTravisBuildBySha, interval * 1000, options, checkNumber + 1)
7176
}
7277

7378
const lastBuildForCommit = res.builds.find((build) => build.commit_id === matchingCommit.id)
@@ -79,16 +84,16 @@ function pollTravisBuildBySha (options, checkNumber = 1) {
7984
} else if (lastState === 'failed') {
8085
return createGhStatus('failure', lastBuildForCommit.id, 'build failure')
8186
} else if (~['created', 'started'].indexOf(lastState)) {
82-
options.logger.info(`"${lastState}" build found, will do check #${checkNumber + 1} in 30 seconds`)
87+
options.logger.info(`"${lastState}" build found, will do check #${checkNumber + 1} in ${interval} seconds`)
8388
createGhStatus('pending', lastBuildForCommit.id, 'build in progress')
8489
} else {
8590
return options.logger.info(`Unknown build state: "${lastState}", stopping polling`)
8691
}
8792
} else {
88-
options.logger.warn(`Was not able to find matching build by last commit, will do check #${checkNumber + 1} in 30 seconds`)
93+
options.logger.warn(`Was not able to find matching build by last commit, will do check #${checkNumber + 1} in ${interval} seconds`)
8994
}
9095

91-
setTimeout(pollTravisBuildBySha, 30 * 1000, options, checkNumber + 1)
96+
setTimeout(pollTravisBuildBySha, interval * 1000, options, checkNumber + 1)
9297
})
9398
}
9499

scripts/display-travis-status.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,39 @@
22

33
const debug = require('debug')('display_travis_status')
44
const pollTravis = require('../lib/pollTravis')
5-
const enabledRepos = [
6-
'citgm', 'readable-stream', 'nodejs.org', 'docker-node',
7-
'llnode', 'nan', 'node-core-utils', 'core-validate-commit'
8-
]
5+
6+
// Default is 20 retries, 30 seconds per retry, which is reasonable for
7+
// smaller projects that finish testing within 10 minutes.
8+
const DEFAULT_POLL_RETRY = process.env.TRAVIS_POLL_RETRY || 20
9+
const DEFAULT_POLL_INTERVAL = process.env.TRAVIS_POLL_INTERVAL || 30
10+
11+
// For projects that take longer to build e.g. projects that actually use
12+
// Travis to build, increase the interval so there will be fewer useless
13+
// polls
14+
const enabledRepos = {
15+
'citgm': { retry: 40 },
16+
'readable-stream': { retry: 60, interval: 60 },
17+
'nodejs.org': { },
18+
'docker-node': { retry: 70, interval: 120 },
19+
'llnode': { retry: 60, interval: 60 },
20+
'nan': { retry: 60, interval: 60 },
21+
'node-core-utils': { },
22+
'core-validate-commit': { }
23+
}
924

1025
module.exports = function (app) {
1126
app.on('pull_request.opened', handlePrUpdate)
1227
// Pull Request updates
1328
app.on('pull_request.synchronize', handlePrUpdate)
1429

1530
function handlePrUpdate (event, owner, repo) {
16-
if (!~enabledRepos.indexOf(repo)) return
31+
const config = enabledRepos[repo]
32+
if (!config) return
1733

1834
const pr = event.number
19-
const options = { owner, repo, pr, logger: event.logger }
35+
const retry = config.retry || DEFAULT_POLL_RETRY
36+
const interval = config.interval || DEFAULT_POLL_INTERVAL
37+
const options = { owner, repo, pr, retry, interval, logger: event.logger }
2038

2139
debug(`/${owner}/${repo}/pull/${pr} opened`)
2240
pollTravis.pollThenStatus(options)
@@ -27,10 +45,15 @@ module.exports = function (app) {
2745
const owner = req.params.owner
2846
const repo = req.params.repo
2947
const pr = parseInt(req.params.id, 10)
30-
const options = { owner, repo, pr, logger: req.log }
31-
32-
if (~enabledRepos.indexOf(repo)) {
48+
const config = enabledRepos[repo]
49+
if (config) {
50+
const retry = config.retry || DEFAULT_POLL_RETRY
51+
const interval = config.interval || DEFAULT_POLL_INTERVAL
52+
const options = { owner, repo, pr, retry, interval, logger: req.log }
53+
res.writeHead(201)
3354
pollTravis.pollThenStatus(options)
55+
} else {
56+
res.writeHead(404, 'Repo not enabled')
3457
}
3558
res.end()
3659
})

0 commit comments

Comments
 (0)