Meteor.setInterval(()=>{

Hi all!
I’m trying periodically fetch exchange rates by:

Meteor.setInterval(()=>{
    let cur = HTTP.get(url, options);
..............
}, 21600000);

and in production I noticed, that app in 100% CPU frozen and i get errors:

Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnv$
    at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor.js:1075:11)
    at Object.Meteor.bindEnvironment (packages/meteor.js:1151:10)
    at Object.call (packages/meteor.js:211:27)
    at Object.HTTP.get (meteor:// ^ ^  app/packages/http/httpcall_common.js:50:20)
    at null._repeat (meteor:// ^ ^  app/lib/collections.js:24:20)
    at null.wrapper [as _onTimeout] (timers.js:279:11)
    at Timer.listOnTimeout (timers.js:92:15)

How to do Meteor.bindEnvironment correctly in this case?
Is it correct place Meteor.setInterval in lib/server folder?

Why not use a cronjob?

There are several npm packages, and even an old school Atmosphere package.

2 Likes

I would have said the same as @eleventy suggested, Syncd Cron.

I’ve got an app which polls for share prices at set intervals.
Syncd Cron is amazing at how simple it is and how great it works.

1 Like

Can i be a little forward/pushy and ask for a super simple code sample. i.e. do this once a day or once a week or once a month please? Like not so much the exact code, but structure etc.

Like I’ve never used it, and I don’t understand what happens in the event of server shutdown. Can you conceptually explain this to me. I think it uses the db, but then why would it not work well on a server instance where the app shuts down like galaxy free tier [ according to description]. Isn’t this the whole point?

Whenever someone says something is super simple, i know there are edge cases which will trip me up [sigh].

It would be most appreciated. Thanks so much.

Tat

Simple example:


import { SyncedCron } from 'meteor/percolate:synced-cron';

SyncedCron.add({
  name: 'Runs Once A Day',
  schedule: function(parser) {

    // Run at 6am only on weekdays.
    return parser.recur().on('6:00').time().onWeekday();

  },
  job: function() {
    // This is where you put your function code you want to run at each interval
  }
});

SyncedCron.start();


Server shutdown will mean the job wont run if the server is off during the time it is due …
However once the server starts up again, all the jobs will queue as expected.
From my example above, if the server is off for 10 minutes from 5:55am until 6:05 am- this function won’t run because the server will reboot and it will add the event to the queue for 6am the following morning…

There is no galaxy free tier anymore - but the reason why it didn’t work well on that was because unless someone was using the app the server would shut down. As with my example, scheduled tasks don’t run if the server is off. So given the server is constantly being shut down and restarted it will not work. This would apply if you deployed to now.sh or to the free tier of heroku hosting.

Hosting this on any normal server will be fine.

4 Likes

I did’t know about it!
big thanx! :+1:

legend… much appreciated.

Looks like OP is solved, I’ll leave it here as alternative solution, we’re using - JoSk: