Skip to main content

SchedulerStrategy

SchedulerStrategy

This strategy is used to define the mechanism by which scheduled tasks are executed and how they are reported on. The main purpose of this strategy is to ensure that a given task is executed exactly once at the scheduled time, even if there are multiple instances of the worker running.

To do this, the strategy must use some form of shared storage and a method of locking so that only a single worker is allowed to execute the task.

By default, the DefaultSchedulerStrategy will use the database to enable this functionality.

Signature
interface SchedulerStrategy extends InjectableStrategy {
executeTask(task: ScheduledTask): (job: Cron) => Promise<any> | any;
getTasks(): Promise<TaskReport[]>;
getTask(id: string): Promise<TaskReport | undefined>;
updateTask(input: UpdateScheduledTaskInput): Promise<TaskReport>;
}

executeTask

method
(task: ScheduledTask) => (job: Cron) => Promise<any> | any

Execute a scheduled task. This method must also take care of ensuring that the task is executed exactly once at the scheduled time, even if there are multiple instances of the worker running.

For instance, in the DefaultSchedulerStrategy we make use of a dedicated database table and a locking mechansim. If you implement a custom SchedulerStrategy, you must use some other form of shared locking mechanism that could make use of something like Redis etc. to ensure that the task is executed exactly once at the scheduled time.

getTasks

method
() => Promise<TaskReport[]>

Get all scheduled tasks.

getTask

method
(id: string) => Promise<TaskReport | undefined>

Get a single scheduled task by its id.

updateTask

method
(input: UpdateScheduledTaskInput) => Promise<TaskReport>

Update a scheduled task.

TaskReport

A report on the status of a scheduled task.

Signature
interface TaskReport {
id: string;
lastExecutedAt: Date | null;
isRunning: boolean;
lastResult: any;
enabled: boolean;
}

id

property
string

lastExecutedAt

property
Date | null

isRunning

property
boolean

lastResult

property
any

enabled

property
boolean