TaskDefinition
TaskDefinition
Defines a task with its execution logic and scheduling.
Tasks can have optional preparation logic that determines whether the task should run, and required execution logic that performs the actual task work.
Signature
interface TaskDefinition<T extends Record<string, any> = Record<string, any>> {
name: string;
schedule?: TaskSchedule;
timezone?: string;
immediate?: boolean;
prepare?: (ctx: TaskContext<T>) => Promise<boolean>;
execute: (ctx: TaskContext<T>) => Promise<void>;
}
name
property
string
Unique identifier for the task
schedule
property
Optional schedule configuration for recurring or delayed execution
timezone
property
string
Optional timezone for the schedule
immediate
property
boolean
Whether the task should run immediately when created (only for cron tasks)
prepare
property
(ctx: TaskContext<T>) => Promise<boolean>
Optional preparation function that determines if the task should execute. Return false to skip execution for this run.
execute
property
(ctx: TaskContext<T>) => Promise<void>
The main execution function that performs the task work. This is called when the task is scheduled to run.