Created
July 7, 2019 07:05
-
-
Save tuhuynh27/67285477b680a26111d8063e070611d6 to your computer and use it in GitHub Desktop.
Job Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create the message queue | |
const importQueue = new Queue("importDB"); | |
// Watch and Run job queue | |
// When have new job in queue (I mean when someone put some job in Redis), | |
// this will watch and do the job by call jobCallback() | |
importQueue.process(jobCallback); | |
// Describe what to do in the job | |
async function doJob(id) { | |
const axiosConfig = { | |
headers: { | |
Authorization: | |
"Bearer " + jwtToken | |
} | |
}; | |
try { | |
const { data } = await axios.get( | |
"https://lapi.fptu.tech/api/v1/bookdetails/" + id, | |
axiosConfig | |
); | |
// Refactor book_detail entity, everything must be snake_case in output | |
const bookDetail = { | |
id: data.id, | |
name: data.name.trim(), | |
categories: data.categories.name, | |
authors: data.authors.map(e => e.name), | |
libol: data.libol, | |
subject_codes: data.parseedSubjectCode, | |
preview_link: data.previewLink, | |
thumbnail: data.thumbnail, | |
published_date: data.publishedDate, | |
description: data.description, | |
isbn: data.isbn, | |
amount: data.book_amount | |
}; | |
// Run query | |
const collection = db.collection("book_detail"); | |
await collection.insertMany([bookDetail]); | |
console.log(`Success inserted book_detail ${id}!`); | |
return id; | |
} catch (err) { | |
if ( | |
// Check if the legacy server return 404 (id not found) | |
err.response && | |
err.response.status && | |
err.response.status === 404 | |
) { | |
console.log(`Instance ${id} not found!`); | |
} else { | |
console.log(`Something failed: ${err}`); | |
} | |
} | |
} | |
async function jobCallback(job, done) { | |
await doJob(job.data.id); | |
done(); | |
} | |
async function createJob() { | |
// There are 2151 rows in db so we will create 2151 jobs :) | |
const loopTime = 2151; | |
// For loop write in declarative way, we will create a array that have 2151 element | |
const loop = Array.from(Array(loopTime)); | |
// Create job promise | |
loop.map((_, i) => { | |
const index = i + 1; | |
// This wil return a promise | |
return importQueue.add({ | |
id: index | |
}); | |
}); | |
// Waiting for add all job (2151 jobs) is done, and return | |
await Promise.all(loop); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment