Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 83d0251

Browse files
Sleep after request to give es processor time to breathe
1 parent 7375898 commit 83d0251

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

config/default.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424

2525
TOPCODER_USERS_API: process.env.TOPCODER_USERS_API || 'https://api.topcoder-dev.com/v3/users',
2626

27-
PROCESS_CONCURRENCY_COUNT: process.env.PROCESS_CONCURRENCY_COUNT ? parseInt(process.env.PROCESS_CONCURRENCY_COUNT, 10) : 25,
27+
PROCESS_CONCURRENCY_COUNT: process.env.PROCESS_CONCURRENCY_COUNT ? parseInt(process.env.PROCESS_CONCURRENCY_COUNT, 10) : 1,
2828

2929
// When a user is not found, should we proceed to create the user or treat it as error
3030
CREATE_MISSING_USER_FLAG: process.env.CREATE_MISSING_USER_FLAG === 'true',
@@ -35,5 +35,7 @@ module.exports = {
3535
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME,
3636
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
3737
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET,
38-
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL
38+
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL,
39+
40+
SLEEP_TIME: process.env.SLEEP_TIME ? parseInt(process.env.SLEEP_TIME, 10) : 1000
3941
}

src/common/helper.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ const topcoderM2MConfig = _.pick(config, ['AUTH0_URL', 'AUTH0_TOPCODER_AUDIENCE'
1818
const ubahnM2M = m2mAuth({ ...ubahnM2MConfig, AUTH0_AUDIENCE: ubahnM2MConfig.AUTH0_UBAHN_AUDIENCE })
1919
const topcoderM2M = m2mAuth({ ...topcoderM2MConfig, AUTH0_AUDIENCE: topcoderM2MConfig.AUTH0_TOPCODER_AUDIENCE })
2020

21+
/**
22+
* Use this function to halt execution
23+
* js version of sleep()
24+
* @param {Number} ms Timeout in ms
25+
*/
26+
async function sleep (ms) {
27+
if (!ms) {
28+
ms = config.SLEEP_TIME
29+
}
30+
31+
logger.debug(`Sleeping for ${ms} ms`)
32+
33+
return new Promise(resolve => setTimeout(resolve, ms))
34+
}
35+
2136
/* Function to get M2M token
2237
* (U-Bahn APIs only)
2338
* @returns {Promise}
@@ -118,6 +133,9 @@ async function getUbahnSingleRecord (path, params, isOptionRecord) {
118133
return status >= 200 && status < 300
119134
}
120135
})
136+
137+
await sleep()
138+
121139
if (_.isArray(res.data)) {
122140
if (res.data.length === 1) {
123141
return res.data[0]
@@ -163,6 +181,9 @@ async function createUbahnRecord (path, data) {
163181
logger.debug(`request POST ${path} with data: ${JSON.stringify(data)}`)
164182
try {
165183
const res = await axios.post(`${config.UBAHN_API_URL}${path}`, data, { headers: { Authorization: `Bearer ${token}` } })
184+
185+
await sleep()
186+
166187
return res.data
167188
} catch (err) {
168189
logger.error(err)
@@ -182,6 +203,9 @@ async function updateUBahnRecord (path, data) {
182203
logger.debug(`request PATCH ${path} with data: ${JSON.stringify(data)}`)
183204
try {
184205
const res = await axios.patch(`${config.UBAHN_API_URL}${path}`, data, { headers: { Authorization: `Bearer ${token}` } })
206+
207+
await sleep()
208+
185209
return res.data
186210
} catch (err) {
187211
logger.error(err)
@@ -210,6 +234,9 @@ async function getUserInTopcoder (handle) {
210234

211235
try {
212236
const res = await axios.get(url, { headers: { Authorization: `Bearer ${token}` }, params })
237+
238+
await sleep()
239+
213240
return res.data
214241
} catch (err) {
215242
logger.error(err)
@@ -236,6 +263,9 @@ async function createUserInTopcoder (user) {
236263
logger.debug(`request POST ${url} with data: ${JSON.stringify(user)}`)
237264
try {
238265
const res = await axios.post(url, requestBody, { headers: { Authorization: `Bearer ${token}` } })
266+
267+
await sleep()
268+
239269
return res.data
240270
} catch (err) {
241271
logger.error(err)
@@ -252,6 +282,9 @@ async function createUserInTopcoder (user) {
252282
async function updateProcessStatus (id, data) {
253283
const token = await getUbahnM2Mtoken()
254284
const res = await axios.patch(`${config.UBAHN_SEARCH_UI_API_URL}/uploads/${id}`, data, { headers: { Authorization: `Bearer ${token}` } })
285+
286+
await sleep()
287+
255288
return res.data
256289
}
257290

src/services/ProcessorService.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ async function processCreateRecord (record, failedRecord, organizationId) {
241241
async function processCreate (message) {
242242
const { status } = message.payload
243243

244+
logger.info(`Concurrency count set at ${config.PROCESS_CONCURRENCY_COUNT} with type ${typeof config.process.env.PAUSE_AFTER_REQUEST}`)
245+
244246
if (status === 'pending') {
245247
try {
246248
const file = await helper.downloadFile(message.payload.objectKey)

0 commit comments

Comments
 (0)