setTask
The setTask
method registers a task with the Manager
, optionally associating it with a category and/or a custom configuration.
setTask(
taskId: string,
task: Task,
categoryId?: string,
config?: TaskRunConfig,
): Manager
Type | Description | |
---|---|---|
taskId | string | The |
task | Task | The task function to register. |
categoryId? | string | The optional |
config? | TaskRunConfig | An optional |
returns | Manager | A reference to the |
The task is an asynchronous function that is passed an optional string when it is scheduled. This setTask
method simply registers it with an Id
so a run can be scheduled in the future.
The configuration has properties which let you indicate the duration of task runs and their retry behaviors, for example.
If this method is called on a task Id
that already exists, its function, category, and configuration will be updated.
Examples
This example registers a task called ping
that fetches content from a website.
import {createManager} from 'tinytick';
const manager = createManager();
manager.setTask('ping', async () => await fetch('https://example.org'));
console.log(manager.getTaskIds());
// -> ['ping']
This example registers a task called ping
that fetches content from a website. It has some task-specific configuration.
import {createManager} from 'tinytick';
const manager = createManager();
manager.setTask(
'ping',
async () => await fetch('https://example.org'),
undefined,
{maxRetries: 3},
);
console.log(manager.getTaskConfig('ping', true));
// -> {maxDuration: 1000, maxRetries: 3, retryDelay: 1000, repeatDelay: null}
This example registers a task called ping
that fetches content from a website. It is given the category of 'network', which has been given some specific configuration.
import {createManager} from 'tinytick';
const manager = createManager();
manager.setCategory('network', {maxDuration: 5000});
manager.setTask(
'ping',
async () => await fetch('https://example.org'),
'network',
);
console.log(manager.getTaskConfig('ping', true));
// -> {maxDuration: 5000, maxRetries: 0, retryDelay: 1000, repeatDelay: null}
Since
v1.0.0