Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e30a70b

Browse files
authoredSep 29, 2023
Merge pull request #656 from topcoder-platform/dev
Add Retry, Update log
2 parents 5eef69c + 4a9c278 commit e30a70b

File tree

7 files changed

+93
-29
lines changed

7 files changed

+93
-29
lines changed
 

‎app-routes.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
const _ = require("lodash");
66
const config = require("config");
77
const HttpStatus = require("http-status-codes");
8+
const uuid = require("uuid/v4");
89
const helper = require("./src/common/helper");
910
const errors = require("./src/common/errors");
11+
const logger = require("./src/common/logger");
1012
const routes = require("./src/routes");
1113
const authenticator = require("tc-core-library-js").middleware.jwtAuthenticator;
1214

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

2729
const actions = [];
2830
actions.push((req, res, next) => {
29-
req.signature = `${def.controller}#${def.method}`;
31+
if (def.method !== "checkHealth") {
32+
req._id = uuid();
33+
req.signature = `${req._id}-${def.controller}#${def.method}`;
34+
logger.info(`Started request handling, ${req.signature}`);
35+
}
3036
next();
3137
});
3238

@@ -104,7 +110,6 @@ module.exports = (app) => {
104110
}
105111
});
106112
}
107-
108113
actions.push(method);
109114
app[verb](`/${config.API_VERSION}${path}`, helper.autoWrapExpress(actions));
110115
});

‎package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"dependencies": {
4343
"@grpc/grpc-js": "^1.8.12",
4444
"@opensearch-project/opensearch": "^2.2.0",
45-
"@topcoder-framework/domain-challenge": "^0.22.0",
46-
"@topcoder-framework/lib-common": "^0.22.0",
45+
"@topcoder-framework/domain-challenge": "^0.23.0",
46+
"@topcoder-framework/lib-common": "^0.23.0",
4747
"aws-sdk": "^2.1145.0",
4848
"axios": "^0.19.0",
4949
"axios-retry": "^3.4.0",

‎src/common/helper.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,22 @@ async function getChallengeResources(challengeId, roleId = null) {
253253
return result;
254254
}
255255

256+
/**
257+
* Get challenge resources count
258+
* @param {String} challengeId the challenge id
259+
* @returns {Promise<Object>} the challenge resources count
260+
*/
261+
async function getChallengeResourcesCount(challengeId, roleId = null) {
262+
const token = await m2mHelper.getM2MToken();
263+
const url = `${config.RESOURCES_API_URL}/count?challengeId=${challengeId}${
264+
roleId ? `&roleId=${roleId}` : ""
265+
}`;
266+
const res = await axios.get(url, {
267+
headers: { Authorization: `Bearer ${token}` },
268+
});
269+
return res.data;
270+
}
271+
256272
/**
257273
* Create challenge resources
258274
* @param {String} challengeId the challenge id
@@ -1053,6 +1069,19 @@ async function getChallengeSubmissions(challengeId) {
10531069
return allSubmissions;
10541070
}
10551071

1072+
/**
1073+
* Get challenge submissions count
1074+
* @param {String} challengeId the challenge id
1075+
* @returns {Promise<Object>} the submission counts
1076+
*/
1077+
async function getChallengeSubmissionsCount(challengeId) {
1078+
const token = await m2mHelper.getM2MToken();
1079+
const res = await axios.get(`${config.SUBMISSIONS_API_URL}/${challengeId}/count`, {
1080+
headers: { Authorization: `Bearer ${token}` },
1081+
});
1082+
return res.data;
1083+
}
1084+
10561085
/**
10571086
* Get member by ID
10581087
* @param {String} userId the user ID
@@ -1183,6 +1212,7 @@ module.exports = {
11831212
downloadFromS3,
11841213
deleteFromS3,
11851214
getChallengeResources,
1215+
getChallengeResourcesCount,
11861216
createResource,
11871217
getUserGroups,
11881218
ensureNoDuplicateOrNullElements,
@@ -1206,6 +1236,7 @@ module.exports = {
12061236
getProjectIdByRoundId,
12071237
getGroupById,
12081238
getChallengeSubmissions,
1239+
getChallengeSubmissionsCount,
12091240
getMemberById,
12101241
createSelfServiceProject,
12111242
activateProject,

‎src/common/logger.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ logger.logFullError = (err, signature) => {
3030
if (signature) {
3131
logger.error(`Error happened in ${signature}`);
3232
}
33-
logger.error(util.inspect(err));
34-
if (!err.logged) {
35-
logger.error(err.stack);
36-
err.logged = true;
33+
if (err.isJoi) {
34+
logger.error(
35+
`${err.name} details: ${JSON.stringify(err.details)} input:${JSON.stringify(err._object)}`
36+
);
37+
} else if (err.isAxiosError) {
38+
logger.error(`${err.message} - ${err.response.data}`);
39+
} else if (err.httpStatus) {
40+
logger.error(err.message);
41+
} else {
42+
logger.error(util.inspect(err));
3743
}
3844
};
3945

‎src/phase-management/PhaseAdvancer.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,18 @@ class PhaseAdvancer {
274274
async #getRegistrantCount(challengeId) {
275275
console.log(`Getting registrant count for challenge ${challengeId}`);
276276
// 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]
277-
const submitters = await helper.getChallengeResources(challengeId, config.SUBMITTER_ROLE_ID);
278-
return submitters.length;
277+
const submitters = await helper.getChallengeResourcesCount(
278+
challengeId,
279+
config.SUBMITTER_ROLE_ID
280+
);
281+
return submitters[config.SUBMITTER_ROLE_ID] || 0;
279282
}
280283

281284
async #getSubmissionCount(challengeId) {
282285
console.log(`Getting submission count for challenge ${challengeId}`);
283286
// 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]
284-
const submissions = await helper.getChallengeSubmissions(challengeId);
285-
return submissions.length;
287+
const submissions = await helper.getChallengeSubmissionsCount(challengeId);
288+
return submissions["Contest Submission"] || 0;
286289
}
287290

288291
async #areAllSubmissionsReviewed(challengeId) {

‎src/services/ChallengeService.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,26 @@ const {
4646
const deepEqual = require("deep-equal");
4747
const { getM2MToken } = require("../common/m2m-helper");
4848

49-
const challengeDomain = new ChallengeDomain(GRPC_CHALLENGE_SERVER_HOST, GRPC_CHALLENGE_SERVER_PORT);
49+
const challengeDomain = new ChallengeDomain(
50+
GRPC_CHALLENGE_SERVER_HOST,
51+
GRPC_CHALLENGE_SERVER_PORT,
52+
{
53+
"grpc.service_config": JSON.stringify({
54+
methodConfig: [
55+
{
56+
name: [{ service: "" }],
57+
retryPolicy: {
58+
maxAttempts: 5,
59+
initialBackoff: "0.5s",
60+
maxBackoff: "30s",
61+
backoffMultiplier: 2,
62+
retryableStatusCodes: ["UNAVAILABLE", "DEADLINE_EXCEEDED", "INTERNAL"],
63+
},
64+
},
65+
],
66+
}),
67+
}
68+
);
5069
const phaseAdvancer = new PhaseAdvancer(challengeDomain);
5170

5271
/**
@@ -1420,7 +1439,7 @@ async function updateChallenge(currentUser, challengeId, data) {
14201439

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

14251444
await validateChallengeUpdateRequest(currentUser, challenge, data);
14261445

‎yarn.lock

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,31 +255,31 @@
255255
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf"
256256
integrity sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A==
257257

258-
"@topcoder-framework/client-relational@^0.22.0":
259-
version "0.22.0"
260-
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"
261-
integrity sha512-We0sb8pdxOZfzX8WzKxczhXl16jmZ6cN/eBgDv5jR8qpVoXhLTa2iaTLqiRYUWi9ZvHCN6vmNQ607w0IU/iRFQ==
258+
"@topcoder-framework/client-relational@^0.23.0":
259+
version "0.23.0"
260+
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"
261+
integrity sha512-/0K4TRTHCe3bRUM5H1wYlPbKZazg/PJNfHkb07i1tyi77YiH4YT8zgW9A23SAoW4/k3xsveSkI8U+YbmIqnALw==
262262
dependencies:
263263
"@grpc/grpc-js" "^1.8.0"
264-
"@topcoder-framework/lib-common" "^0.22.0"
264+
"@topcoder-framework/lib-common" "^0.23.0"
265265
topcoder-interface "github:topcoder-platform/plat-interface-definition#v0.0.58-beta-1"
266266
tslib "^2.4.1"
267267

268-
"@topcoder-framework/domain-challenge@^0.22.0":
269-
version "0.22.0"
270-
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"
271-
integrity sha512-PT2Zts56QKtntSJQxjH8slRjrYISuUGCZdYmyQcy+ak0nQL0COhQ0puqJ6mfIA9Ml3Ggi8Vmk/G9Ti12h1YNDg==
268+
"@topcoder-framework/domain-challenge@^0.23.0":
269+
version "0.23.0"
270+
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"
271+
integrity sha512-IRLM18PCekatQsea1pzfZWGiiO3+8vxQrM3BmWGGpls2BAOIrS+/y1MLJDM23DasXEmMOz+zFQe939VQyrZ7Qw==
272272
dependencies:
273273
"@grpc/grpc-js" "^1.8.0"
274-
"@topcoder-framework/client-relational" "^0.22.0"
275-
"@topcoder-framework/lib-common" "^0.22.0"
274+
"@topcoder-framework/client-relational" "^0.23.0"
275+
"@topcoder-framework/lib-common" "^0.23.0"
276276
topcoder-interface "github:topcoder-platform/plat-interface-definition#v0.0.58-beta-1"
277277
tslib "^2.4.1"
278278

279-
"@topcoder-framework/lib-common@^0.22.0":
280-
version "0.22.0"
281-
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"
282-
integrity sha512-sHdOAyCGcNaDT9esc9Q3sNaqvVAwHPv6NCTlTAt5O9dcSpdz2AyEur8mS5WccFclKhF5ZB9BM1bbWxO8i9WXGQ==
279+
"@topcoder-framework/lib-common@^0.23.0":
280+
version "0.23.0"
281+
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"
282+
integrity sha512-burWJaxo/rt1KtqxTlXRenG4qUK1QioSq8S7ouq6pUlhC1vyJSiBCum7QWR6E9H/sWit8wRl+gZBMEVVJVHbHQ==
283283
dependencies:
284284
"@grpc/grpc-js" "^1.8.0"
285285
rimraf "^3.0.2"

0 commit comments

Comments
 (0)
Please sign in to comment.