Skip to content

Commit c1888f4

Browse files
committed
Added self refresh for the cache
1 parent 6a7d730 commit c1888f4

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

src/server/services/recruitCRM.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const FormData = require('form-data');
1515
const NodeCache = require('node-cache');
1616

1717
// gigs list caching
18-
const gigsCache = new NodeCache({ stdTTL: config.GIGS_LISTING_CACHE_TIME, checkperiod: 10 });
1918
const CACHE_KEY = 'jobs';
19+
const gigsCache = new NodeCache({ stdTTL: config.GIGS_LISTING_CACHE_TIME, checkperiod: 10 });
2020

2121
const JOB_FIELDS_RESPONSE = [
2222
'id',
@@ -174,6 +174,63 @@ export default class RecruitCRMService {
174174
}
175175
}
176176

177+
/**
178+
* Gets all jobs method.
179+
* @return {Promise}
180+
* @param {Object} query the request query.
181+
*/
182+
async getAll(query) {
183+
try {
184+
const response = await fetch(`${this.private.baseUrl}/v1/jobs/search?${qs.stringify(query)}`, {
185+
method: 'GET',
186+
headers: {
187+
'Content-Type': 'application/json',
188+
Authorization: this.private.authorization,
189+
},
190+
});
191+
if (response.status === 429) {
192+
await new Promise(resolve => setTimeout(resolve, 30000)); // wait 30sec
193+
return this.getAll(query);
194+
}
195+
if (response.status >= 400) {
196+
const error = {
197+
error: true,
198+
status: response.status,
199+
url: `${this.private.baseUrl}/v1/jobs/search?${qs.stringify(query)}`,
200+
errObj: await response.json(),
201+
};
202+
return error;
203+
}
204+
const data = await response.json();
205+
if (data.current_page < data.last_page) {
206+
const pages = _.range(2, data.last_page + 1);
207+
return Promise.all(
208+
pages.map(page => fetch(`${this.private.baseUrl}/v1/jobs/search?${qs.stringify(query)}&page=${page}`, {
209+
method: 'GET',
210+
headers: {
211+
'Content-Type': 'application/json',
212+
Authorization: this.private.authorization,
213+
},
214+
})),
215+
)
216+
.then(async (allPages) => {
217+
// eslint-disable-next-line no-restricted-syntax
218+
for (const pageDataRsp of allPages) {
219+
// eslint-disable-next-line no-await-in-loop
220+
const pageData = await pageDataRsp.json();
221+
data.data = _.flatten(data.data.concat(pageData.data));
222+
}
223+
const toSend = _.map(data.data, j => _.pick(j, JOB_FIELDS_RESPONSE));
224+
return toSend;
225+
});
226+
}
227+
const toSend = _.map(data.data, j => _.pick(j, JOB_FIELDS_RESPONSE));
228+
return toSend;
229+
} catch (err) {
230+
return err;
231+
}
232+
}
233+
177234
/**
178235
* Gets all jobs endpoint.
179236
* @return {Promise}
@@ -694,3 +751,16 @@ export default class RecruitCRMService {
694751
return data.data[0];
695752
}
696753
}
754+
755+
// Self update cache on expire to keep it fresh
756+
gigsCache.on('expired', async (key) => {
757+
if (key === CACHE_KEY) {
758+
const ss = new RecruitCRMService();
759+
const gigs = await ss.getAll({
760+
job_status: 1,
761+
});
762+
if (!gigs.error) {
763+
gigsCache.set(CACHE_KEY, gigs);
764+
}
765+
}
766+
});

0 commit comments

Comments
 (0)