Strange problem coming from Meteor + Passenger

I’m running Meteor through the Apache and Passenger on production server. Meteor has a method for sending SMS messages. After user takes some action, SMS will be send.

Strange problem is, that after user take the action, sometimes are send more same SMS messages. After small research, it seems to be, that more Meteor instances running through the passenger takes the same action together (on localhost in development mode, there is no any problem, only in production). Same problem I have with more methods (not only with sending SMS), especially, when the methods do something longer time.

…only an idea, I don’t have defined “MONGO_OPLOG_URL”, can this be the cause of this problem?

Thanks a lot.

1 Like

You need to pass the processing to a job or queue handler.

Example of a queue handler with all the bells and whistles: GitHub - taskforcesh/bullmq: BullMQ - Premium Message Queue for NodeJS based on Redis

One of the simplest queue project out there: GitHub - bee-queue/bee-queue: A simple, fast, robust job/task queue for Node.js, backed by Redis.

1 Like

Recommended the agenda job scheduler here. But then read the post above again more carefully and realized that you actually indeed need a queue handler like @rjdavid referenced!

@rjdavid @vooteles I’m sure, that your recommendation will be better solution. But still I don’t understand, when I make for this method only single time and Meteor.method is also called only single time, the code inside the method is executed sometimes more times (not two times, but sometimes ten times or more, randomly).

// Call the method single time'testMethod');
    // Execute test method single time
    'testMethod'() {
       const users = Meteor.users.find({});

       users.forEach(user=> {
           // Do something for the user, which takes about 4 seconds 
           // (calculations, mongo operations, ...)

           // Then finally call function for send SMS...

           // BUT: Code in this cycle for the same user is sometimes executed more times
           // and I don't know why. Behavior is similar like when I restart Apache/Meteor during execution
           // of the method and after start, the Meteor start execution of the method again by self

I’m really appreciate your help.
I need to solve this problem ASAP.
Thanks a lot.

Check your client. It might be calling the method multiple times. If it is a client issue, check for re-renders or anything that might cause multiple calls. As a shortcut, search about javascript debouncing and use a debounce function to prevent multiple function calls

You might want to look at the docs as well: Methods | Meteor API Docs

The last section there reads:

If a client calls a method and is disconnected before it receives a response, it will re-call the method when it reconnects. This means that a client may call a method multiple times when it only means to call it once. If this behavior is problematic for your method, consider attaching a unique ID to each method call on the client, and checking on the server whether a call with this ID has already been made. Alternatively, you can use Meteor.apply with the noRetry option set to true.

If something causes your client to lose connection, then the method will be called again.