Schedule a task to happen at a specific time

I am making a very enrollment based service, where accounts are “invitation only”. Luckily, Meteor makes this very easy. The problem I am having is updating status.

I have 4 statuses:

  • User profile is created (‘new’)
  • User has been invited (‘invited’)
  • User has accepted the invitation (‘enrolled’)
  • User has declined the invitation, deactivated their account, or their email token has expired (‘unenrolled’)

The problem I am having is the unenrolled status, particularly, when their token has expired. On invitation, I do the following:

var id = Accounts.createUser({
  email: doc.email,
  profile: {
    profileId: doc._id
  }
});

// ... send email and whatnot

var inviteSent = Meteor.users.findOne(id).resume.loginTokens[0].when;
Students.update(doc._id, {
  $set: {
    userId: id,
    status: 'invited'
  },
  $push: {
    invitiations: {
      sentAt: inviteSent,
      expiresAt: Accounts._tokenExpiration(inviteSent),
      accepted: false
    }
  }
})

Now the problem I am having is setting some sort of event when their token expires to set their enrollment status differently in some sort of efficient manner. I was thinking of adding some sort of pre-find hook, but it seems excessive work to do that on every find.

Ideally, I’d like to schedule an event for expiresAt to update the status to unenrolled. Is this possible?

You could set up an hourly running task that checks for expiresAt dates in the past and have not yet been processed, processes them, and ticks a mark somewhere indicating that they have. Percolate:syncedcron is very straightforward for such tasks.

1 Like

I was going to have to do scheduling for an app (never got to it though) and I book marked this for later. It looks very promising. Hope it helps!

I ask a sort-of-related question here: Scheduling Emails and http://richsilv.github.io/meteor/scheduling-events-in-the-future-with-meteor/ came up there too. GL

Since this is the top Google result for “meteor scheduler” and SyncedCron is deprecated, I thought I’d mention that the new Steve Jobs package is designed just for Meteor and supports this kind of use case (and others too)

1 Like

Awesome package! Just gave it a go and your documentation plus the features are fantastic. While perhaps obvious, doing:

Jobs.run('doSomething', someArg, {
    in: {
       minutes: 1
    }
});

against a timer showed it did the job in just about 60 seconds. :slight_smile:

Question: if i configure months: 1, will that account for months with different lengths (28, 29 (leap year), 30, 31 days)?

Just want to mention that meteor-synced-cron is not deprecated - it’s just being maintained by a different organisation.

The Atmosphere package is now named littledata:syncedcron (previously percolate:synced-cron).

Its GitHub page is:

Thanks man! IIRC, it will add one month using the standard operations available on the JavaScript Date object. IIRC, if that ends up setting something like February 30, it will be adjusted to February 27 or 28.

1 Like

Awesome. I’ll continue with more tests and will report anything wonky, though given the quality of your packages I suspect I won’t need to report anything.

1 Like