A package like synced-cron

Hi,

I need to update some documents in my collection in specific time and with specific id given by the client ; but it seems that I can’t do it with synced-cron.

Is there a package like percolate:synced-cron that can be called from the client-side and tacke id and time like arguments??

Use later.js. Pretty easy library.

@corvid, thank’s but it seems not taking arguments also; I need if it possible something that can be called from the client and take argument

Can you shed a little more light on why you want something like this? Triggering scheduled activities from the client side sounds like a recipe for disaster. First of all you can’t guarantee your scheduling code stays active on the client side long enough to be run (for example, your users could shut their browser down before your scheduled task is run at a specific time). Also, driving scheduled tasks from the client side means you’re dependent on your users local clock. They could have their time set to anything, which could mess up the triggering schedule.

1 Like

@hwillson; Hi, I 'll explain you what I want exactly;

In my reac – meteor app ; the user

  • can do so many shooping lists he wants
  • can put a reminder for one list or for more
  • Each list has its own id ;

For this , I need to send the hour and the date set by the user in client side , and also the id of the list; and pass the all to a package like cron ( in server side ) ; so that when the time came , it(cron or other package) sent a reminder of each lists(id) to the user.

I hop that the idea is more clear now;

Did you look at job-collection?

Also some potentially useful ideas discussed here: https://crater.io/posts/cE6hFfXXiGfT2FmBw/package-design-task-scheduling

@hluz Thank’s for link

To keep things simple, you could consider doing something like:

  • When a user adds a reminder on the client side, store the hour, date and list ID in a Reminders Collection (I’d recommend calling an addReminder Method that then updates the Reminders Collection).
  • On the server side setup a synched cron job that monitors the Reminders collection every few minutes. When it finds a reminder that needs to be fired (based on the reminder hour/date), you can then kick off whatever reminder processing you need to handle (by leveraging the stored list ID).

@hwillson; Thank’s I like this concept;
How can Know if the cron is finished or no, is there some cron mothed that can do that ??

@corvid can you explain how to use later.js in Meteor 1.3 ?

If you’re using percolate:synced-cron it logs all job details in a Collection called cronHistory. You can get details like intendedAt, startedAt, finishedAt, etc. for any job from this Collection.

@hwillson, wooow, it seems there is no example using that to remove job when it’s finished (when I search on google.)
have you an example using that, please? or how to remove job after the job being finished??

You can remove any job using SyncedCron.remove('some job name'). You could have a cleanup job that queries the cronHistory Collection looking for jobs that have a finishedAt timestamp that’s older than the current timestamp, then remove those jobs by name.

You can also schedule jobs to only run once (non-reoccuring) by doing something like:

SyncedCron.add({
  name: 'Do something awesome, but only once!',
  schedule(parser) {
    return parser.recur().on(someDate).fullDate();
  },
  job() {
    // Do something ...
  },
});

@hwillson That’s what I’m looking for, how to query the cronHistory Collection ??

cronHistory is just a Mongo collection, so you can query it like you would any other Mongo Collection in Meteor:

...
const cronHistory = new Mongo.Collection('cronHistory');
...
const someJob = cronHistory.findOne();
console.log(someJob.finishedAt);
...

@hwillson

this code break my app

const cronHistory = new Mongo.Collection('cronHistory');

it said that this collection exists,

Is there something wrong?

Okay - in that case try using the cronHistory Collection directly from SyncedCron. So:

...
const cronHistory = SyncedCron._collection;
...
const someJob = cronHistory.findOne();
console.log(someJob.finishedAt);
...

@hwillson prefect, Thank’s

i am facing a strange behavior , so i was wondering do the Roles or the simple schema affect the synced-cron in a way ?
updating collections using synced-cron is functioning well only on some particular collections … any idea ?

@mokaid sorry for late answer, to be honset I don’t if they affect it or no :frowning:

Sorry