admin管理员组

文章数量:1415484

I was wondering if it was possible to delete or modify data in mongodb collection at any particular time provided.

Think of it as trivial data that has to be deleted/updated at the end of the day. Point me in a direction.

I was wondering if it was possible to delete or modify data in mongodb collection at any particular time provided.

Think of it as trivial data that has to be deleted/updated at the end of the day. Point me in a direction.

Share Improve this question asked Nov 18, 2016 at 23:00 Rishabh KapoorRishabh Kapoor 871 gold badge2 silver badges10 bronze badges 3
  • 1 atmospherejs./percolate/synced-cron – Michel Floyd Commented Nov 18, 2016 at 23:55
  • @Michel I request you to right away right a book on Meteor. We seriously look forward to it, you are an ocean of knowledge. – Ankur Soni Commented May 25, 2017 at 16:56
  • Much obliged @AnkurSoni! – Michel Floyd Commented May 25, 2017 at 21:25
Add a ment  | 

3 Answers 3

Reset to default 3

There are two good options to do cron jobs in Meteor:

  • https://github./percolatestudio/meteor-synced-cron
  • https://github./vsivsi/meteor-job-collection

From my experience, if your use case is simple and you need a fast, lightweight solution meteor-synced-cron should be ok. In case your task is more plicated you need more control on the jobs then go with jobs-collection

The standard npm cron works fine. E.g.

import { CronJob } from 'cron';

Meteor.startup(() => {
  new CronJob({
    cronTime: '00 30 02 * * *',
    // use this wrapper if you want to work with mongo:
    onTick: Meteor.bindEnvironment(() => {
      // stuff happens
    })
    start: true,
    timeZone: 'America/Los_Angeles',
  });
});

With meteor I didn't get standard node packages like cron working properly as meteor plaints and shows a message to use Fibers. For a simple daily task I've created a function which will directly use Meteor.setTimeout(). This way it will keep the Meteor environment available so you can do you daily db cleanup.

It uses the node package later just for the schedule when to fire the 'cron' job. You can replace yourDailyCleanup with the name of your function to be called.

    import { Meteor } from 'meteor/meteor';
    import later from 'later';

    function scheduleTimeout(sched, fn) {
      const nowMilli = Date.now();
      const next = later.schedule(sched).next(1,nowMilli+1001);
      console.log('next schedule',next);
      const diffMile = next.getTime() - nowMilli;

      Meteor.setTimeout( function() {
        scheduleTimeout(sched,fn);
        fn();
      } , diffMile );
    }   

    Meteor.startup(function() {
      console.log('Startup');
      later.date.localTime();
//      scheduleTimeout( later.parse.recur().every(2).minute(), function() { console.log('test job');} );
      scheduleTimeout( later.parse.recur().on('23:00:00').time(), yourDailyCleanup );
    });

The code is based on the package percolatestudio:meteor-synced-cron which you can also use when you need more functionality.

本文标签: javascriptCron jobs in MeteorStack Overflow