简体   繁体   中英

Using this.unblock() with Meteor percolate:synced-cron

Is it possible to use this.unblock() with percolate:synced-cron? I expect PJ2_1, PJ2_2, PJ2_3 to all print out rapidly rather than wait for the Meteor.call() to finish in 10 seconds, but this is not the case.

Example job:

SyncedCron.add({
  name: 'pj2',
  schedule: function(parser) {
    return parser.text('every 30 seconds');
  },
  job: function() {
    console.log("PJ2_1:");

    Meteor.call('fa2', function(err, res) {
      if (err) {
        console.log("FA2: ERROR");
      }
    });

    console.log("PJ2_2:");

    Meteor.call('fa2', function(err, res) {
      if (err) {
        console.log("FA2: ERROR");
      }
    });

    console.log("PJ2_3:");
  }
}); 

Example Meteor.methods():

if (Meteor.isServer) {
  Meteor.methods({
    'fa2': function() {
      this.unblock();
      const data = HTTP.get('http://www.fakeresponse.com/api/?data={%22Hello%22:%22World%22}&meta=false&sleep=10');
      return data;
    }
  })
}

this.unblock doesn't work the way you expect when the server is calling methods. According to the source, unblock on the server is a no-op. This makes sense, because on the server, there is no connection (you're not doing a DDP-based invocation), so there's nothing to unblock per-se.

Instead of calling methods, call HTTP.get directly with a callback specified and you'll observe the behaviour that you want.

Alternatively, define a function that is shared by both your method code and called in your SyncedCron job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM