Skip to content

Commit

Permalink
Update build retry logic
Browse files Browse the repository at this point in the history
When querying a build, TRSS will retry 3 times.
In the case of an error, the build will move back to the queue if the return code from Jenkins API is not 404.
If the return code is 404, x-jenkins header is checked to ensure the error code is from Jenkins (not nginx).
In this case, it means the build has an invalid url or is expired. retry function returns 404 and TRSS will stop processing this build.
Otherwise, the error will be ignored (network issue) and the build will move back to the queue.
return code 404 only if the Jenkins server returns 404 for the build (i.e., invalid url, expired build, etc)

- sleep is removed to increase efficiency and reliability. We will check the header in the response to determine the invalid/expired build case.
- test API getJenkinsBuildInfo is added
- fix log issue

related: #852

Signed-off-by: Lan Xia <[email protected]>
  • Loading branch information
llxia committed Mar 27, 2024
1 parent 67e7a51 commit 255c0ff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
9 changes: 8 additions & 1 deletion TestResultSummaryService/DataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ class DataManager {
}

async updateBuild(data) {
logger.verbose('updateBuild', data.buildName, data.buildNum);
if (data.buildUrl) {
logger.verbose('updateBuild', data.buildUrl);
} else if (data.buildName) {
logger.verbose('updateBuild', data.buildName, data.buildNum);
} else {
logger.verbose('updateBuild', data._id);
}

const { _id, buildName, ...newData } = data;
const criteria = { _id: new ObjectID(_id) };
const testResults = new TestResultsDB();
Expand Down
30 changes: 20 additions & 10 deletions TestResultSummaryService/JenkinsInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,31 @@ const options = { request: { timeout: 30000 } };

// Server connection may drop. If timeout, retry.
const retry = (fn) => {
const promise = Promise.promisify(fn);
return async function () {
for (let i = 0; i < 3; i++) {
try {
return await promise.apply(null, arguments);
return await new Promise((resolve, reject) => {
fn(...arguments, (err, data) => {
if (err) {
logger.warn(err);
reject(data);
} else {
resolve(data);
}
});
});
} catch (e) {
logger.warn(`Try #${i + 1}: connection issue`, arguments);
logger.warn(e);
logger.warn(`Sleep 2 secs...`);
await Promise.delay(2 * 1000);
if (
i === 2 &&
e.toString().includes('unexpected status code: 404')
) {
return { code: 404 };
if (e) {
logger.warn(`status code: ${e.statusCode}`);
logger.warn(`headers: ${JSON.stringify(e.headers)}`);

// return code 404 only if the Jenkins server returns 404 for the build (i.e., invalid url, expired build, etc)
// x-jenkins header is checked to ensure the error code is from Jenkins (not nginx)
// TRSS will stop processing this build once code 404 is returned. See BuildProcessor for details.
if (e.statusCode === 404 && e.headers['x-jenkins']) {
return { code: e.statusCode };
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions TestResultSummaryService/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ app.get('/getData', wrap(require('./getData')));
app.get('/getErrorInOutput', wrap(require('./getErrorInOutput')));
app.get('/getHistoryPerTest', wrap(require('./getHistoryPerTest')));
app.get('/getJenkins', wrap(require('./getJenkins')));
app.get('/getJenkinsBuildInfo', wrap(require('./test/getJenkinsBuildInfo')));
app.get('/getLastBuildInfo', wrap(require('./getLastBuildInfo')));
app.get('/getLogStream', wrap(require('./getLogStream')));
app.get('/getOutputById', wrap(require('./getOutputById')));
Expand Down
14 changes: 14 additions & 0 deletions TestResultSummaryService/routes/test/getJenkinsBuildInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const JenkinsInfo = require('../../JenkinsInfo');

module.exports = async (req, res) => {
const { url, buildName, buildNum } = req.query;
if (req.query.buildNum)
req.query.buildNum = parseInt(req.query.buildNum, 10);
const jenkinsInfo = new JenkinsInfo();
try {
const output = await jenkinsInfo.getBuildInfo(url, buildName, buildNum);
res.send({ output });
} catch (e) {
res.send({ result: e.toString() });
}
};

0 comments on commit 255c0ff

Please sign in to comment.