TaskDriver
TaskDriver
Interface for task persistence and scheduling drivers.
Drivers handle the actual scheduling, persistence, and execution timing of tasks. Different drivers can provide different persistence mechanisms (in-memory, database, etc.) and scheduling capabilities.
Example
import { TaskDriver } from '@commandkit/tasks';
class CustomDriver implements TaskDriver {
async create(task: TaskData): Promise<string> {
// Schedule the task in your system
const id = await this.scheduler.schedule(task);
return id;
}
async delete(task: string): Promise<void> {
// Remove the task from your system
await this.scheduler.cancel(task);
}
async setTaskRunner(runner: TaskRunner): Promise<void> {
// Set up the execution handler
this.runner = runner;
}
}
interface TaskDriver {
create(task: TaskData): Promise<string>;
delete(task: string): Promise<void>;
setTaskRunner(runner: TaskRunner): Promise<void>;
}
create
(task: TaskData) => Promise<string>
Creates a new scheduled task.
This method should schedule the task according to its schedule configuration and return a unique identifier that can be used to delete the task later. Multiple tasks may be created with the same name.
delete
(task: string) => Promise<void>
Deletes a scheduled task by its identifier.
This method should remove the task from the scheduling system and cancel any pending executions. If the task doesn't exist, this method should complete successfully without throwing an error.
setTaskRunner
(runner: TaskRunner) => Promise<void>
Sets the task execution runner function.
This method should store the provided runner function and call it whenever a task is due for execution. The runner function receives the task execution data and should handle any errors that occur during execution.