User's Guide 04:04 Pro: Time Limited Jobs

Ed Ropple edited this page May 22, 2018 · 1 revision

This is a feature of TaskBotJS Pro.

There are some cases where you'll want a job to not fire if it's past a certain point in time. If you use TaskBotJS to send out a daily email, for example, it may be the case that you'd want May 22nd's email to not send if for some reason it's backlogged to the point where it isn't started until May 23rd.

TaskBotJS Pro adds to the basic job a new function, calculateExpiry, which is given the current time (as a luxon.DateTime) and is expected to return one. Here's an example:

import Chance from "chance";
import sleepAsync from "sleep-promise";

import { Job, IDependencies, ExpiryCalculationFunction } from "@taskbotjs-pro/client";

import { NoDeps } from "../NoDeps";
import { DateTime } from "luxon";

const chance = new Chance();

export class ExpiringJob extends Job<NoDeps> {
  static readonly jobName: string = "taskbot.expiring";
  static readonly calculateExpiry: ExpiryCalculationFunction = (now: DateTime) => now.plus({ seconds: 5 });

  async perform(): Promise<void> {
    const interval = Math.max(25, Math.round(chance.normal({mean: 300, dev: 250})));
    await sleepAsync(interval);
    this.logger.info({ interval }, "I'm a job that would've expired. BUT I DIDN'T!");
  }
}

You can specify this in taskbot.performAsyncWithOptions as well. Technically, you can do so with taskbot.performAt and taskbot.performAtWithOptions, but I have a hard time coming up with many jobs that should be performed at a given time and given an expiry. (That doesn't mean one doesn't exist, of course.)

If calculateExpiry returns null or undefined, the job has no expiry.

On top of all this, expiring jobs are included in both basic and dated metrics and exposed in the Web UI for TaskBotJS Pro. You can add a plugin that reacts to the expiring of a job by attaching a function to Server.onJobExpired.

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.