Skip to content

Add Retry, Update log #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
const _ = require("lodash");
const config = require("config");
const HttpStatus = require("http-status-codes");
const uuid = require("uuid/v4");
const helper = require("./src/common/helper");
const errors = require("./src/common/errors");
const logger = require("./src/common/logger");
const routes = require("./src/routes");
const authenticator = require("tc-core-library-js").middleware.jwtAuthenticator;

Expand All @@ -26,7 +28,11 @@ module.exports = (app) => {

const actions = [];
actions.push((req, res, next) => {
req.signature = `${def.controller}#${def.method}`;
if (def.method !== "checkHealth") {
req._id = uuid();
req.signature = `${req._id}-${def.controller}#${def.method}`;
logger.info(`Started request handling, ${req.signature}`);
}
next();
});

Expand Down Expand Up @@ -104,7 +110,6 @@ module.exports = (app) => {
}
});
}

actions.push(method);
app[verb](`/${config.API_VERSION}${path}`, helper.autoWrapExpress(actions));
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"dependencies": {
"@grpc/grpc-js": "^1.8.12",
"@opensearch-project/opensearch": "^2.2.0",
"@topcoder-framework/domain-challenge": "^0.22.0",
"@topcoder-framework/lib-common": "^0.22.0",
"@topcoder-framework/domain-challenge": "^0.23.0",
"@topcoder-framework/lib-common": "^0.23.0",
"aws-sdk": "^2.1145.0",
"axios": "^0.19.0",
"axios-retry": "^3.4.0",
Expand Down
31 changes: 31 additions & 0 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ async function getChallengeResources(challengeId, roleId = null) {
return result;
}

/**
* Get challenge resources count
* @param {String} challengeId the challenge id
* @returns {Promise<Object>} the challenge resources count
*/
async function getChallengeResourcesCount(challengeId, roleId = null) {
const token = await m2mHelper.getM2MToken();
const url = `${config.RESOURCES_API_URL}/count?challengeId=${challengeId}${
roleId ? `&roleId=${roleId}` : ""
}`;
const res = await axios.get(url, {
headers: { Authorization: `Bearer ${token}` },
});
return res.data;
}

/**
* Create challenge resources
* @param {String} challengeId the challenge id
Expand Down Expand Up @@ -1053,6 +1069,19 @@ async function getChallengeSubmissions(challengeId) {
return allSubmissions;
}

/**
* Get challenge submissions count
* @param {String} challengeId the challenge id
* @returns {Promise<Object>} the submission counts
*/
async function getChallengeSubmissionsCount(challengeId) {
const token = await m2mHelper.getM2MToken();
const res = await axios.get(`${config.SUBMISSIONS_API_URL}/${challengeId}/count`, {
headers: { Authorization: `Bearer ${token}` },
});
return res.data;
}

/**
* Get member by ID
* @param {String} userId the user ID
Expand Down Expand Up @@ -1183,6 +1212,7 @@ module.exports = {
downloadFromS3,
deleteFromS3,
getChallengeResources,
getChallengeResourcesCount,
createResource,
getUserGroups,
ensureNoDuplicateOrNullElements,
Expand All @@ -1206,6 +1236,7 @@ module.exports = {
getProjectIdByRoundId,
getGroupById,
getChallengeSubmissions,
getChallengeSubmissionsCount,
getMemberById,
createSelfServiceProject,
activateProject,
Expand Down
14 changes: 10 additions & 4 deletions src/common/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ logger.logFullError = (err, signature) => {
if (signature) {
logger.error(`Error happened in ${signature}`);
}
logger.error(util.inspect(err));
if (!err.logged) {
logger.error(err.stack);
err.logged = true;
if (err.isJoi) {
logger.error(
`${err.name} details: ${JSON.stringify(err.details)} input:${JSON.stringify(err._object)}`
);
} else if (err.isAxiosError) {
logger.error(`${err.message} - ${err.response.data}`);
} else if (err.httpStatus) {
logger.error(err.message);
} else {
logger.error(util.inspect(err));
}
};

Expand Down
11 changes: 7 additions & 4 deletions src/phase-management/PhaseAdvancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,18 @@ class PhaseAdvancer {
async #getRegistrantCount(challengeId) {
console.log(`Getting registrant count for challenge ${challengeId}`);
// TODO: getChallengeResources loops through all pages, which is not necessary here, we can just get the first page and total count from header[X-Total]
const submitters = await helper.getChallengeResources(challengeId, config.SUBMITTER_ROLE_ID);
return submitters.length;
const submitters = await helper.getChallengeResourcesCount(
challengeId,
config.SUBMITTER_ROLE_ID
);
return submitters[config.SUBMITTER_ROLE_ID] || 0;
}

async #getSubmissionCount(challengeId) {
console.log(`Getting submission count for challenge ${challengeId}`);
// TODO: getChallengeSubmissions loops through all pages, which is not necessary here, we can just get the first page and total count from header[X-Total]
const submissions = await helper.getChallengeSubmissions(challengeId);
return submissions.length;
const submissions = await helper.getChallengeSubmissionsCount(challengeId);
return submissions["Contest Submission"] || 0;
}

async #areAllSubmissionsReviewed(challengeId) {
Expand Down
23 changes: 21 additions & 2 deletions src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,26 @@ const {
const deepEqual = require("deep-equal");
const { getM2MToken } = require("../common/m2m-helper");

const challengeDomain = new ChallengeDomain(GRPC_CHALLENGE_SERVER_HOST, GRPC_CHALLENGE_SERVER_PORT);
const challengeDomain = new ChallengeDomain(
GRPC_CHALLENGE_SERVER_HOST,
GRPC_CHALLENGE_SERVER_PORT,
{
"grpc.service_config": JSON.stringify({
methodConfig: [
{
name: [{ service: "" }],
retryPolicy: {
maxAttempts: 5,
initialBackoff: "0.5s",
maxBackoff: "30s",
backoffMultiplier: 2,
retryableStatusCodes: ["UNAVAILABLE", "DEADLINE_EXCEEDED", "INTERNAL"],
},
},
],
}),
}
);
const phaseAdvancer = new PhaseAdvancer(challengeDomain);

/**
Expand Down Expand Up @@ -1420,7 +1439,7 @@ async function updateChallenge(currentUser, challengeId, data) {

// Remove fields from data that are not allowed to be updated and that match the existing challenge
data = sanitizeData(sanitizeChallenge(data), challenge);
console.debug("Sanitized Data:", data);
logger.debug(`Sanitized Data: ${JSON.stringify(data)}`);

await validateChallengeUpdateRequest(currentUser, challenge, data);

Expand Down
30 changes: 15 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -255,31 +255,31 @@
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf"
integrity sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A==

"@topcoder-framework/client-relational@^0.22.0":
version "0.22.0"
resolved "https://topcoder-409275337247.d.codeartifact.us-east-1.amazonaws.com/npm/topcoder-framework/@topcoder-framework/client-relational/-/client-relational-0.22.0.tgz#0e096758ffd8c9d0eb986b2f9328ed247930abfe"
integrity sha512-We0sb8pdxOZfzX8WzKxczhXl16jmZ6cN/eBgDv5jR8qpVoXhLTa2iaTLqiRYUWi9ZvHCN6vmNQ607w0IU/iRFQ==
"@topcoder-framework/client-relational@^0.23.0":
version "0.23.0"
resolved "https://topcoder-409275337247.d.codeartifact.us-east-1.amazonaws.com/npm/topcoder-framework/@topcoder-framework/client-relational/-/client-relational-0.23.0.tgz#c15d102c47044e23df0d1823b5eec898d8a8bb15"
integrity sha512-/0K4TRTHCe3bRUM5H1wYlPbKZazg/PJNfHkb07i1tyi77YiH4YT8zgW9A23SAoW4/k3xsveSkI8U+YbmIqnALw==
dependencies:
"@grpc/grpc-js" "^1.8.0"
"@topcoder-framework/lib-common" "^0.22.0"
"@topcoder-framework/lib-common" "^0.23.0"
topcoder-interface "github:topcoder-platform/plat-interface-definition#v0.0.58-beta-1"
tslib "^2.4.1"

"@topcoder-framework/domain-challenge@^0.22.0":
version "0.22.0"
resolved "https://topcoder-409275337247.d.codeartifact.us-east-1.amazonaws.com/npm/topcoder-framework/@topcoder-framework/domain-challenge/-/domain-challenge-0.22.0.tgz#bcb7f7a602e424d9932fd0693935aa5f1f2439a4"
integrity sha512-PT2Zts56QKtntSJQxjH8slRjrYISuUGCZdYmyQcy+ak0nQL0COhQ0puqJ6mfIA9Ml3Ggi8Vmk/G9Ti12h1YNDg==
"@topcoder-framework/domain-challenge@^0.23.0":
version "0.23.0"
resolved "https://topcoder-409275337247.d.codeartifact.us-east-1.amazonaws.com/npm/topcoder-framework/@topcoder-framework/domain-challenge/-/domain-challenge-0.23.0.tgz#31964a09864afb0f4969ea22addeb89ba47d9689"
integrity sha512-IRLM18PCekatQsea1pzfZWGiiO3+8vxQrM3BmWGGpls2BAOIrS+/y1MLJDM23DasXEmMOz+zFQe939VQyrZ7Qw==
dependencies:
"@grpc/grpc-js" "^1.8.0"
"@topcoder-framework/client-relational" "^0.22.0"
"@topcoder-framework/lib-common" "^0.22.0"
"@topcoder-framework/client-relational" "^0.23.0"
"@topcoder-framework/lib-common" "^0.23.0"
topcoder-interface "github:topcoder-platform/plat-interface-definition#v0.0.58-beta-1"
tslib "^2.4.1"

"@topcoder-framework/lib-common@^0.22.0":
version "0.22.0"
resolved "https://topcoder-409275337247.d.codeartifact.us-east-1.amazonaws.com/npm/topcoder-framework/@topcoder-framework/lib-common/-/lib-common-0.22.0.tgz#bd3428b0199410a5151326d1d9731c404c255fb5"
integrity sha512-sHdOAyCGcNaDT9esc9Q3sNaqvVAwHPv6NCTlTAt5O9dcSpdz2AyEur8mS5WccFclKhF5ZB9BM1bbWxO8i9WXGQ==
"@topcoder-framework/lib-common@^0.23.0":
version "0.23.0"
resolved "https://topcoder-409275337247.d.codeartifact.us-east-1.amazonaws.com/npm/topcoder-framework/@topcoder-framework/lib-common/-/lib-common-0.23.0.tgz#01fb1e9c2a32760387f3444ce991b9164c75f577"
integrity sha512-burWJaxo/rt1KtqxTlXRenG4qUK1QioSq8S7ouq6pUlhC1vyJSiBCum7QWR6E9H/sWit8wRl+gZBMEVVJVHbHQ==
dependencies:
"@grpc/grpc-js" "^1.8.0"
rimraf "^3.0.2"
Expand Down