Skip to content
Max Savin edited this page Nov 5, 2017 · 17 revisions

These primary features are all you need to know to start scheduling background tasks.

Jobs.register

This function allows you to define your background jobs. Whenever a job is called to run, it will run the function that you describe. If you have a job scheduled, but the job code changes, the scheduled job will run with the latest code.

Jobs.register({
    sendReminderEmail: function (to, content) {
        Email.send({
            to: to,
            from: "[email protected]",
            subject: "Your Reminder",
            content: content,
        })
    },
    insertRecord: function (data) {
        Collection.insert({
            date: new Date(),
            data: data
        });
    }
});

Jobs.run

This function allows you to run a background job like you would run a Method.

Jobs.run("sendReminderEmail", "[email protected]", "Don't forget about the launch!");

The snippet above will add the item to queue, with a due time of the current time. This means it will run as soon as the queue is able to run it. However, you can pass in a special configuration package to change that and more.

Jobs.run("sendReminderEmail", "[email protected]", "The future is here!", {
    in: {
        days: 1,
        hours: 13
    }, 
    on: {
        minute: 13,
        year: 2037
    },
    priority: 1000
});

About in and on

The in parameter will schedule the job at a later time, using the current time and your inputs to calculate the due time. The on parameter override the current time with your inputs.

The supported fields for in and on can be used in singular and/or plural versions:

  • millisecond, second, minute, hour, day, month, and year
  • milliseconds, seconds, minutes, hours, days, months, and years

The date object will be updated in the order that is specified. This means that if it is year 2017, and you set in one year, but on 2019, the year 2019 will be the final result. However, if you set on 2019 and in one year, then the year 2020 will be the final result.

  • About priority
    • The default priority for each job is 0. If you set it to a positive integer, it will run ahead of other jobs. If you set it to a negative integer, it will only run after all the zero or positive jobs have completed.

Jobs.repeat

Coming soon.

Clone this wiki locally