@@ -18,6 +18,21 @@ const topcoderM2MConfig = _.pick(config, ['AUTH0_URL', 'AUTH0_TOPCODER_AUDIENCE'
18
18
const ubahnM2M = m2mAuth ( { ...ubahnM2MConfig , AUTH0_AUDIENCE : ubahnM2MConfig . AUTH0_UBAHN_AUDIENCE } )
19
19
const topcoderM2M = m2mAuth ( { ...topcoderM2MConfig , AUTH0_AUDIENCE : topcoderM2MConfig . AUTH0_TOPCODER_AUDIENCE } )
20
20
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
+
21
36
/* Function to get M2M token
22
37
* (U-Bahn APIs only)
23
38
* @returns {Promise }
@@ -118,6 +133,9 @@ async function getUbahnSingleRecord (path, params, isOptionRecord) {
118
133
return status >= 200 && status < 300
119
134
}
120
135
} )
136
+
137
+ await sleep ( )
138
+
121
139
if ( _ . isArray ( res . data ) ) {
122
140
if ( res . data . length === 1 ) {
123
141
return res . data [ 0 ]
@@ -163,6 +181,9 @@ async function createUbahnRecord (path, data) {
163
181
logger . debug ( `request POST ${ path } with data: ${ JSON . stringify ( data ) } ` )
164
182
try {
165
183
const res = await axios . post ( `${ config . UBAHN_API_URL } ${ path } ` , data , { headers : { Authorization : `Bearer ${ token } ` } } )
184
+
185
+ await sleep ( )
186
+
166
187
return res . data
167
188
} catch ( err ) {
168
189
logger . error ( err )
@@ -182,6 +203,9 @@ async function updateUBahnRecord (path, data) {
182
203
logger . debug ( `request PATCH ${ path } with data: ${ JSON . stringify ( data ) } ` )
183
204
try {
184
205
const res = await axios . patch ( `${ config . UBAHN_API_URL } ${ path } ` , data , { headers : { Authorization : `Bearer ${ token } ` } } )
206
+
207
+ await sleep ( )
208
+
185
209
return res . data
186
210
} catch ( err ) {
187
211
logger . error ( err )
@@ -210,6 +234,9 @@ async function getUserInTopcoder (handle) {
210
234
211
235
try {
212
236
const res = await axios . get ( url , { headers : { Authorization : `Bearer ${ token } ` } , params } )
237
+
238
+ await sleep ( )
239
+
213
240
return res . data
214
241
} catch ( err ) {
215
242
logger . error ( err )
@@ -236,6 +263,9 @@ async function createUserInTopcoder (user) {
236
263
logger . debug ( `request POST ${ url } with data: ${ JSON . stringify ( user ) } ` )
237
264
try {
238
265
const res = await axios . post ( url , requestBody , { headers : { Authorization : `Bearer ${ token } ` } } )
266
+
267
+ await sleep ( )
268
+
239
269
return res . data
240
270
} catch ( err ) {
241
271
logger . error ( err )
@@ -252,6 +282,9 @@ async function createUserInTopcoder (user) {
252
282
async function updateProcessStatus ( id , data ) {
253
283
const token = await getUbahnM2Mtoken ( )
254
284
const res = await axios . patch ( `${ config . UBAHN_SEARCH_UI_API_URL } /uploads/${ id } ` , data , { headers : { Authorization : `Bearer ${ token } ` } } )
285
+
286
+ await sleep ( )
287
+
255
288
return res . data
256
289
}
257
290
0 commit comments