jessetane / queue

Asynchronous function queue with adjustable concurrency
MIT License
764 stars 66 forks source link

create Queue with API calls #80

Closed dabibu153 closed 2 years ago

dabibu153 commented 2 years ago

I am trying to implement a operation queue that will handle a heave processing task while the rest of the express application and all the APis keep working normally. But as soon as the queue is called [ queue.start() ], all the APis in the server stop responding and don't work until the queue is done with its work.

how do i make it so both the queue and the rest of the express server work simultaneously.

` const startHeavyQueue = (filters) => {

const newQueue = queue();

const addFunToQueue = (cb) => {
    generatePDF(filters, cb);
}

newQueue.push(addFunToQueue);

newQueue.on('success', function (result, job) {
    console.log('job finished processing:')
})

newQueue.start(function (err) {
    if (err) throw err
    console.log('started queue')
})

};

`

this is the function i call withing the main api. the api simply calls it and returns a 200 response stating when the processing is done, you will get a notification.

now its my first time attempting anything more than a simple express server, so any and all input is appreciated. thanks

jessetane commented 2 years ago

generatePdf is a blocking operation maybe? Remember javascript is single threaded, so to parallelize you will need to fork child processes or talk to another long running process over the network. Queue is probably not very useful for your use case as I understand it so far

jessetane commented 2 years ago

In a browser context you can use web workers to parallelize

dabibu153 commented 2 years ago

thanks for the reply and the clarification. after some RnD, I decided to settle with worker-threads. now the heavy blocking operation (generatePDF) is moved down to a worker thread while the main thread handles the API requests.