Long running meteor method being invoked twice

Hello,
I have a long running meteor method (importLruIcd below) that is invoked from a client page with the below code from within a modal dialog. If I keep any site page open, a second invocation of the long running task occurs. Using the client side (chrome) debugger shows that the second client side invocation is not coming directly from the Meteor.call line. If I close all pages associated with the site, the second invocation doesn’t occur. I’m wondering what may be prompting this second invocation, perhaps some kind of retry since the process is so long running? If so, is there any way to turn this off?

One other note, this only happens when I’m running from the WebStorm debugger. Perhaps it’s because the server method takes even longer to run that way. When I run meteor from the command line I don’t see this behavior.

 'click #import-command': function (e, t) {
        e.preventDefault();
        
        var currState = ServerState.findOne({name: SERVER_STATE_CONSTANTS.ICD_LOAD_PERCENT_STATE_NAME});
        ServerState.update({_id: currState._id}, {$set: {value: 1}}, function (err, dbObj) {
            if (err) {
                console.log("Error updating icd update percentage: " + err.message);
            }
        });

        Meteor.call('importLruIcd', importSelectionTextRv.get(), selectedModelTextRv.get(), selectedLruTextRv.get(), enteredVersionTextRv.get(), function (error, result) {
            if (error)
            {
                alert(error.reason);
            }
            
            var currState = ServerState.findOne({name: SERVER_STATE_CONSTANTS.ICD_LOAD_PERCENT_STATE_NAME});
            ServerState.update({_id: currState._id}, {$set: {value: 100}}, function (err, dbObj) {
                if (err) {
                    console.log("Error updating icd update percentage: " + err.message);
                }
            });
        });
    },
1 Like

I have the same problem right now.
Anybody there with the same issue?

http://docs.meteor.com/#/full/meteor_methods

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

2 Likes

I’ve run across the same problem

There is an issue for this.


In my opinion, this is a “bug”. Because if you have a method that takes long but you unblock it… that shouldn’t happen. So we need either a way to set up a timeout or a way to avoid the retry.

Nothing new about this issue?
Maybe you can help me in a particular case:
I have a method that builds an excel file processing a lot of data. That takes a while.
This method is being called twice. Even if I add a unique id the method returns before the excel is actually created, then tries to call it again but the problem is that I cannot get the excel.

We ran into a similar issue and couldn’t find any particular solution for this. Currently we’re doing something like this:

  • Create a collection to record method, its run count and its response :
    Logger collection -
    key: {
    type: String,
    optional: true
    },
    errors: {
    type: String,
    optional: true
    },
    count: {
    type: Number,
    defaultValue: 1
    }

The key can be method name or any auto generated one. Make sure its unique.

  • Before calling the method from client, create a key and pass it to method call:
    ` let logkey = Logger.insert({
    key: “mykey”
    });

      Meteor.call('myMethod', payload, logkey, function(err, data) {
        Logger.remove(logkey);
    });
    
  • Remember to delete the logger key once it has been used.

  • On server check for the logkey:
    let logkey = Logger.findOne({
    key: “mykey”
    });
    if(logkey.count===1 && Meteor.isServer){
    //run method using Meteor.wrapAsync and once method finishes save the log in the logger document
    }
    //Note that the return is outside if else. The return wont work until server block finishes (sync)
    return Logger.findOne({
    key: “mykey”
    });

`

1 Like

Please fix this problem of method code being executed twice. It should only run once. I’ve came across this problem before and have just come across it again today in different code.

1 Like

Any news on this bug ?

I’ve struggled with this for while, and it all depends on your environment, unfortunately. If you’re using a special debugger built into your editor, for example, or running your Meteor app behind a proxy, you need to make sure your connection timeout is set to something higher than normal.

For the longest time, I thought this was a Meteor bug, but really it was my method running longer than 30s and tripping a default (undocumented) timeout on my SSL proxy.

If you don’t want to increase your timeout threshold to compensate for long-running method executions, you can use the noRetry flag that is detailed here: https://github.com/meteor/meteor/pull/6180

Try the noRetry option in https://docs.meteor.com/api/methods.html#Meteor-apply