Skip to content
Max Savin edited this page Jan 12, 2018 · 17 revisions

Thank you for your interest in this package. Here are the three primary features you need to get familiar with to start scheduling background tasks. The rest will take care of itself.

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 at a future date, but the job code changes before that, the scheduled job will run with the latest code.

Jobs.register({
    "sendReminder": function (to, message) {
        var self = this;

        var call = HTTP.put("http://www.mocky.io/v2/5a58d79c2d00006a29d2e66a/?mocky-delay=2000ms", {
            to: to,
            message: message
        })

        if (call.statusCode === 200) {
            self.success(call);
            return;
        } else {
            self.failure(call);
            return;
        }

        var cantTouchThis = "$99999999999"
    }
});

Each function is binded with a set of operators to give you maximum control over how your job runs:

  • this.doc() - access job document
  • this.set(key, value) - set a persistent key/value pair
  • this.get(key) - get a persistent value from key
  • this.success(result) - tell the queue the job is completed, and attach an optional result
  • this.failure(result) - tell the queue the job failed, and attach an optional result
  • this.reschedule(config) - tell the queue to schedule the job for a future date

Each job must be resolved with success, failure, or reschedule, otherwise it will loop infinitely. After your mark your job, its probably a good idea to put a return statement somewhere.

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
});

All the inputs for the configuration package are optional. The current time is used to schedule the job's due date if no other date information is specified.

Configuration Options

  • in
    • The in parameter will schedule the job at a later time, using the current time and your inputs to calculate the due time.
  • on
    • The on parameter override the current time with your inputs.
  • in and on
    • 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.
  • 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.
  • date
    • Provide your own date. This stacks with the in and on operator, and will be applied before they run.
Clone this wiki locally