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.
interface SchedulerStrategy extends InjectableStrategy {
registerTask?(task: ScheduledTask): void;
executeTask(task: ScheduledTask): (job?: Cron) => Promise<any> | any;
getTasks(): Promise<TaskReport[]>;
getTask(id: string): Promise<TaskReport | undefined>;
triggerTask(task: ScheduledTask): Promise<void>;
updateTask(input: UpdateScheduledTaskInput): Promise<TaskReport>;
}
- Extends:
InjectableStrategy
registerTask
(task: ScheduledTask) => void
An optional method that may be used by the strategy to register all the configured tasks ahead of time. This can be useful for keeping an internal reference of all the tasks to aid in the specific strategy implemetation.
executeTask
(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.
The function returned is then called in order to execture the task. The
job
argument is an instance of the croner Cron
class, except when
the task has been manually triggered, in which case it will be undefined.
getTasks
() => Promise<TaskReport[]>
Get all scheduled tasks.
getTask
(id: string) => Promise<TaskReport | undefined>
Get a single scheduled task by its id.
triggerTask
(task: ScheduledTask) => Promise<void>
Manually trigger a given task. This method is not used to actually invoke the task function itself, since that would cause the task to run on the server instance which we typically do not want. Instead, it should be used to signal to the strategy that this specific task needs to be invoked at the soonest opportunity.
For instance, in the DefaultSchedulerStrategy this is done by setting a flag in the database table which is checked periodically and causes those tasks to get immediately invoked.
updateTask
(input: UpdateScheduledTaskInput) => Promise<TaskReport>
Update a scheduled task.
TaskReport
A report on the status of a scheduled task.
interface TaskReport {
id: string;
lastExecutedAt: Date | null;
isRunning: boolean;
lastResult: any;
enabled: boolean;
}