A small hack to figure out our code problem with Async Queue

I used the Async Queue several times. It was always easy peasy. Recently when I am using for one use case faced a problem. I searched to find the solution didn’t find anything useful so finally figured out the problem through a dirty hack and solved it. I thought let me share so that I might help someone who is struggling like this or I will get suggestions from the world how we can tackle this in a better way.

Before we discuss the problem and hack let me explain a bit about the Async Queue. Many times we need to run background tasks due to our application customers actions/requests, for reporting purpose and etc. Sometimes we get a lot of background tasks and sometimes we won’t have any. If we try to process everything as we are getting because of that we might put a lot of load on our systems like DB, other microservices and etc. So it’s better to process a certain number of tasks at a time. In these cases, the Async Queue comes as a handy tool.

If you want to use Async queue in your application then install this package using below command

npm install async --save

A small example for Async Queue

Basically, the Async Queue is kind of a resources pool. For example, You have 3 self-driving cabs and you will provide the cab as per customer requirement. If you get more than 3 customers at a time they need to wait, if not your cabs will be idle.
Here the most important thing is a callback function. It’s like once your customer is done with it they need to give it back or else you will feel they are using you will never know the cab is free or not.

If you miss calling callback function, then things get ugly. So we need to make sure we call callback function in any case. Here I got the problem basically my task is doing several things based on the requirements and somewhere I missed calling callback function so things got bad. I don’t where I missed.

Basically what this code is doing is we know usually each task take around 500ms on average. So Every minute we are checking currently that tasks is running. We will compare in the last minute who are those and now who if we compare both we will get to know the culprits.