The goal of this assignment is to create an in-memory queue library in JavaScript. Here's the minimum API to implement:
const qyu = require('qyu');
const q = qyu({
rateLimit: 50, // maximum number of jobs being processed by second
statsInterval: 300 // When stat event is sent, in ms
});
q.on('done', ({jobId, jobResult, res}) => {
console.log(`Job done ${jobId}`); // `jobId` is generated by `qyu`
});
q.on('error', ({jobId, error}) => {
console.log(`Job ${jobId} threw an error: ${error.message}`);
});
q.on('drain', () => {
console.log('No more jobs to do');
});
q.on('stats', ({nbJobsPerSecond}) => {
console.log(`${nbJobsPerSecond} jobs/s processed`)
});
q.push(job, { // job is a function returning a promise to indicate when the job is done
priority: 1, // from 1 to 10, 1 being the highest priority
}); // returns a promise (which resolves with {jobId, jobResult})
q.pause(); // returns a promise resolved when `q` has paused (no jobs being processed)
q.start(); // returns a promise resolved when `q` has started (first time) or unpaused
// example job:
async function job() {
await wait(30);
return {Hello: 'world!'} // That's the `jobResult`
}
function wait(ms) {
return new Promise(resolve) {
setTimeout(resolve, ms)
}
}
APIs you can use in your implementation: the Node.js API, not external modules. APIs you can use for tooling (tests, build..): Any module you want.
You can implement more if you have more ideas.
Please provide tests using any library you want.
There are a lot of queue like libraries nowadays on npm
, we trust you to not copy-paste their implementation style.
It's good to get inspired by them, not to copy them.
If you have any question please send them anytime, we are here to answer them.
Please push your code to a public or private repository and send it back via email. We'll evaluate:
- the architecture of your code
- the approach used
- the overall quality of your code
- the instructions and explanations you might want to add in the README
Good luck and have fun!