OptimalBits / bull

Premium Queue package for handling distributed jobs and messages in NodeJS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sometimes proccess callback is calling sometimed dont

RedCod12 opened this issue · comments

Hi! @manast I am new in bull, I am adding a new task in the code below
`

const sendWelcomeMessage = async (user, subscription, bot) => {   
  const welcomeMessage = subscription.w_message.find(w_message => w_message.city.id === user.cities[0].id);
    if (welcomeMessage && welcomeMessage.msg_obj) {
      try {
        const jobConfigs = {
          jobId: uuid(),
          removeOnComplete: true,
          removeOnFail: true,
          backoff: 5,
          attempts: 5,
          delay: 100,
          timeout: 1000000
        }
        console.log(jobConfigs)
        await strapi.$bull.subscriptionsTelegramQueue.add({
          chatID: user.UserTelegamID,
          message: welcomeMessage.msg_obj,
          botID: bot.id
        }, jobConfigs);
        console.log('Welcome message sent successfully');
      } catch (err) {
        console.log('Error sending welcome message:', err);
      }
    }
  };

`

Еvery time it is output to the console 'Welcome message sent successfully'. Code below it is handler

`

strapi.$bull.subscriptionsTelegramQueue.process(function (job, done) {
   console.log('job', job.data)
        const { chatID, message, botID } = job.data;
        try {
          for (const bot of bots) {
            if (bot.id === botID) {
              bot.sendMessageToUsers([chatID], message);
              console.log('Welcome message sent successfully bull');
              break;
            }
          }
          done();
        } catch (error) {
          done(error);
          console.error('Error sending welcome message:', error);
        }
      });

`

but some times callback is calling and console.log('job) is output sometimes dont
Can you tell what I'm doing wrong

I am not sure which console.log('job') you mean, but maybe sometimes this statement never is true?

if (bot.id === botID) {
              bot.sendMessageToUsers([chatID], message);
              console.log('Welcome message sent successfully bull');
              break;
            }

If you are new to Bull, it is probably better to use BullMQ -> https://bullmq.io

Also, this is what chatGPT has to say about this:
It looks like you're encountering an issue where the callback function in your job processor for the Bull queue is not consistently being called or logging the message as expected. Let's go through your code and see what might be causing this issue.

  1. Adding the Task to the Queue: In your first code snippet, you are adding a job to the subscriptionsTelegramQueue with specific configurations. This part seems fine, as you're seeing the 'Welcome message sent successfully' log, which indicates that this part of the code is working correctly.

  2. Processing the Task: In the second snippet, you process the job from the subscriptionsTelegramQueue. The issue might be here. Let's analyze it:

    • The process function is correctly set up to take a job and a done callback.
    • Inside the process function, you log the job data, which is good for debugging.
    • You're looping through bots and sending a message if the bot.id matches botID from the job data.
    • You call done() after successfully processing the job or in the catch block in case of an error.
  3. Potential Issues and Solutions:

    • Asynchronous Processing: If bot.sendMessageToUsers is an asynchronous operation and doesn't return a promise, it may not be awaiting properly, leading to unpredictable behavior. Ensure that this function either uses async/await or returns a promise that can be awaited.
    • Job Processing Errors: There might be errors in processing some jobs, which could be why the log statement doesn't always execute. Make sure that bot.sendMessageToUsers handles all exceptions properly.
    • Concurrency and Job Configuration: Check if there are any issues with the job configuration that might cause the job to be marked as failed or completed before it's actually processed.
    • Logging: Enhance logging to catch more detailed information about each step of the process to understand where it might be failing or not executing as expected.
    • Queue Health: Ensure that the Bull queue is healthy and not overloaded with jobs, which can sometimes cause issues with job processing.
  4. Debugging Tips:

    • Add more log statements within the process function to track the execution flow.
    • Log any errors caught in the catch block.
    • Test with a minimal setup (e.g., a simple job that logs a message) to ensure that the basic queue setup is working.

Remember, debugging asynchronous operations, especially with job queues, can be tricky because of their nature of not executing linearly. Careful logging and error handling are key to identifying the root cause of such issues.