User's Guide 04:01 Scheduled Jobs

Ed Ropple edited this page Jun 24, 2018 · 2 revisions

The TaskBotJS client allows a user to schedule a job some time into the future. There are two variants of scheduling: scheduling at a time and scheduling some duration in the future.

The former is used by passing to taskbot.scheduleAt (or taskbot.scheduleAtWithOptions, similar to the taskbot.performithOptions method) what TaskBotJS calls a DateLike. All a DateLike really is under the hood is anything that'll pass a millisecond timestamp when valueOf() is called. Or, for our TypeScript users:

export type DateLike = Date | DateTime | { valueOf(): number };

This includes the JavaScript Date class as well as the DateTime class out of luxon. (This means that Date | DateTime is thus superfluous, but allows for better autocomplete.) So we'll do something like this:

import { DateTime } from "luxon";

// ...

await clientPool.use(async (taskbot) => {
  // Accepts either a `luxon` Duration object or a collection of named fields.
  const duration = { seconds: 15 };
  await taskbot.scheduleIn(duration, FutureJob);

  // the above is semantically equivalent to:
  const t = DateTime.utc().plus(duration);
  taskbot.scheduleAt(t, FutureJob);
});

We also support scheduling a job some period of time in the future. To this you can pass either luxon.Duration or what we call internally a DurationFields:

export type DurationFields = {
  years?: number,
  quarters?: number,
  months?: number,
  weeks?: number,
  days?: number,
  hours?: number,
  minutes?: number,
  seconds?: number,
  milliseconds?: number
};

And this is used with taskbot.scheduleIn/taskbot.scheduleInWithOptions. As an example:

import { DateTime } from "luxon";

// ...

await clientPool.use(async (taskbot) => {
  const t = DateTime.utc().plus({ minutes: 15 });
  taskbot.scheduleAt(t, FutureJob);
});

Both run the same code paths under the hood; use what feels better to you.

As with normal job enqueueings, you can use Job.setDefaultClientPool in order to enable the use of Job.scheduleAt, Job.scheduleIn, and the related WithOptions methods.

Every TaskBotJS service polls, at a configurable rate, for scheduled jobs; when a job ready to be scheduled is found it will be predictably enqueued.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.