TinyTick logoTinyTick

scheduleTaskRun

The scheduleTaskRun method schedules a specific task to run in the future.

scheduleTaskRun(
  taskId: string,
  arg?: string,
  startAfter?: number,
  config?: TaskRunConfig,
): undefined | string
TypeDescription
taskIdstring

The Id of the task to run.

arg?string

An optional string argument to pass to the Task.

startAfter?number

A timestamp at, or duration after which, the task should run.

config?TaskRunConfig

An optional TaskRunConfig to set for this run.

returnsundefined | string

A new unique Id of the scheduled task run, or undefined if unsuccessful.

The task will be executed with the optional string argument provided, and can be configured to start at a specific time (TimestampMs), or after a specific delay (DurationMs).

You can also provide a custom TaskRunConfig which lets you indicate the duration of the task run and its retry behaviors, for example.

The complete configuration of the task run will be created by merging the properties of this TaskRunConfig with those registered with the Task, and in turn with its category, if any. Any properties not provided will be defaulted by TinyTick. This resolution of the run's configuration is done at the moment the run starts (not when it is scheduled), but after that, it will not change subsequently, even if, say, the task or category are reconfigured after it has started.

This method returns a unique Id for the scheduled task run. However, if the Manager is in the stopping state, new task runs can not be scheduled, and in that case, the method will return undefined.

Examples

This example registers a task that is then scheduled to run.

import {createManager} from 'tinytick';

const manager = createManager();
manager.setTask('ping', async () => await fetch('https://example.org'));

const taskRunId = manager.scheduleTaskRun('ping');
console.log(manager.getScheduledTaskRunIds().length);
// -> 1

console.log(manager.getTaskRunConfig(taskRunId, true));
// -> {maxDuration: 1000, maxRetries: 0, retryDelay: 1000, repeatDelay: null}

This example registers a task that takes a string parameter and that is then scheduled to run with an argument.

import {createManager} from 'tinytick';

const manager = createManager();
manager.setTask('ping', async (url) => await fetch(url));

const taskRunId = manager.scheduleTaskRun('ping', 'https://example.org');
console.log(manager.getScheduledTaskRunIds().length);
// -> 1

console.log(manager.getTaskRunInfo(taskRunId).arg);
// -> 'https://example.org'

This example registers a task that has a category and that is then scheduled to run. Configuration is provided, for the category, the task, and this specific run.

import {createManager} from 'tinytick';

const manager = createManager();
manager.setCategory('network', {maxDuration: 5000});
manager.setTask(
  'ping',
  async () => await fetch('https://example.org'),
  'network',
  {maxRetries: 3},
);
const taskRunId = manager.scheduleTaskRun('ping', '', 0, {retryDelay: 10});

console.log(manager.getTaskRunConfig(taskRunId, true));
// -> {maxDuration: 5000, maxRetries: 3, retryDelay: 10, repeatDelay: null}

Since

v1.0.0

When called with an object as the first argument, the scheduleTaskRun method destructures it to make it easier to skip optional parameters.

scheduleTaskRun(args: {
  taskId: string;
  arg?: string;
  startAfter?: number;
  config?: TaskRunConfig;
}): undefined | string
TypeDescription
args{ taskId: string; arg?: string; startAfter?: number; config?: TaskRunConfig; }

An object containing the Id of the task to run, an optional string argument to pass to the Task, an optional timestamp at, or duration after which, the task should run; and an optional TaskRunConfig to set for this run.

returnsundefined | string

A new unique Id of the scheduled task run, or undefined if unsuccessful.

Example

This example registers a task that is then scheduled to run.

import {createManager} from 'tinytick';

const manager = createManager();
manager.setTask('ping', async () => await fetch('https://example.org'));

const taskRunId = manager.scheduleTaskRun({taskId: 'ping', startAfter: 1});
console.log(manager.getScheduledTaskRunIds().length);
// -> 1

console.log(manager.getTaskRunInfo(taskRunId).taskId);
// -> 'ping'

Since

v1.2.5