Skip to content

Commit

Permalink
Create rescanBuild API (#853)
Browse files Browse the repository at this point in the history
- add 2 secs sleep between each retry
- add AuditLogs when the error code is 404 (for debugging purposes)
- create rescanBuild API to rescan

resovles: #852

Signed-off-by: Lan Xia <[email protected]>
  • Loading branch information
llxia authored Mar 21, 2024
1 parent cb803f2 commit 67e7a51
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
4 changes: 4 additions & 0 deletions TestResultSummaryService/BuildProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class BuildProcessor {
*/
task.status = 'Done';
await new DataManager().updateBuild(task);
await new AuditLogsDB().insertAuditLogs({
action: `[exception] buildInfo.code: ${buildInfo.code}`,
...task,
});
return;
}
task.buildParams = jenkinsInfo.getBuildParams(buildInfo);
Expand Down
7 changes: 6 additions & 1 deletion TestResultSummaryService/JenkinsInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ const retry = (fn) => {
} catch (e) {
logger.warn(`Try #${i + 1}: connection issue`, arguments);
logger.warn(e);
if (e.toString().includes('unexpected status code: 404')) {
logger.warn(`Sleep 2 secs...`);
await Promise.delay(2 * 1000);
if (
i === 2 &&
e.toString().includes('unexpected status code: 404')
) {
return { code: 404 };
}
}
Expand Down
17 changes: 11 additions & 6 deletions TestResultSummaryService/routes/deleteBuildsAndChildrenByFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ async function deleteBuilds(req, res) {
}
}

async function deleteBuildsAndChildrenByFields(query) {
async function deleteBuildsAndChildrenByFields(
query,
deleteChildrenOnly = false
) {
if (Object.keys(query).length > 0) {
if (query.buildNum) query.buildNum = parseInt(query.buildNum, 10);
if (query._id) query._id = new ObjectID(query._id);
Expand All @@ -22,14 +25,14 @@ async function deleteBuildsAndChildrenByFields(query) {
'deleteBuildsAndChildrenByFields: ',
result[i].buildUrl
);
await deleteBuild(result[i]);
await deleteBuild(result[i], deleteChildrenOnly);
}
return result;
}
return null;
}

async function deleteBuild(build) {
async function deleteBuild(build, deleteChildrenOnly = false) {
const testResultsDB = new TestResultsDB();
const children = await testResultsDB
.getData({ parentId: build._id })
Expand All @@ -44,10 +47,12 @@ async function deleteBuild(build) {
await outputDB.deleteOne({ _id: build.tests[j].testOutputId });
}
}
if (build.buildOutputId) {
await outputDB.deleteOne({ _id: build.buildOutputId });
if (!deleteChildrenOnly) {
if (build.buildOutputId) {
await outputDB.deleteOne({ _id: build.buildOutputId });
}
await testResultsDB.deleteOne({ _id: build._id });
}
await testResultsDB.deleteOne({ _id: build._id });
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions TestResultSummaryService/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ app.get('/getTotals', wrap(require('./getTotals')));
app.get('/parseJenkinsUrl', wrap(require('./parseJenkinsUrl')));
app.get('/populateDB', wrap(require('./populateDB')));
app.get('/getFeedbackUrl', wrap(require('./getFeedbackUrl')));
app.get('/rescanBuild', wrap(require('./rescanBuild')));
app.get('/testParserViaFile', wrap(require('./test/testParserViaFile')));
app.get(
'/testParserViaLogStream',
Expand Down
33 changes: 33 additions & 0 deletions TestResultSummaryService/routes/rescanBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { TestResultsDB, ObjectID } = require('../Database');
const {
deleteBuildsAndChildrenByFields,
} = require('./deleteBuildsAndChildrenByFields');
/*
* rescanBuild API updates status field to NotDone based on _id.
*/
module.exports = async (req, res) => {
const { _id } = req.query;
try {
if (_id) {
const db = new TestResultsDB();
const options = { upsert: true };
const criteria = { _id: new ObjectID(_id) };
// delete all child builds if any
const deleteChildrenOnly = true;
await deleteBuildsAndChildrenByFields(criteria, deleteChildrenOnly);
// set build status
const result = await db.update(
criteria,
{ $set: { status: 'NotDone' } },
options
);
res.send(result);
} else {
res.json({
error: 'Please provide _id',
});
}
} catch (err) {
res.send({ result: err.toString() });
}
};

0 comments on commit 67e7a51

Please sign in to comment.