Skip to content

TasksProvider

This file defines the TasksProvider class, which provides functionality for listing, retrieving, and manually executing tasks in the @sodacore/core library. It supports both scheduled (cron-based) and manual tasks, integrating with the Runner module to manage task execution.


Features

  • Task Listing: Retrieve all available tasks, filtered by type (scheduled, manual, or any).
  • Task Lookup: Find a specific task by name, whether it is scheduled or manual.
  • Manual Execution: Run a task by name, triggering either its cron job or direct execution method.
  • Dependency Injection: Uses @Inject and @Provide decorators for integration with the DI system.

Usage

typescript
import TasksProvider from './provider/tasks';

const tasksProvider = new TasksProvider();

// List all tasks
const allTasks = tasksProvider.getTasks();

// List only scheduled tasks
const scheduledTasks = tasksProvider.getTasks('scheduled');

// Get a task by name
const task = tasksProvider.getTaskByName('MyTask');

// Run a task by name
await tasksProvider.run('MyTask');

API

Methods

getTasks(type: ITaskType = 'any'): string[]

Returns an array of task names, filtered by type:

  • 'scheduled': Only tasks with a cron schedule.
  • 'manual': Only tasks that are run manually.
  • 'any' (default): All tasks.

getTaskByName(name: string): Cron | BaseTask | null

Returns the task (either a Cron instance or a BaseTask instance) matching the given name, or null if not found.

async run(name: string): Promise<void>

Runs the task with the specified name. If the task is a scheduled (cron) task, it triggers the cron job; if it is a manual task, it calls its run method.


Notes

  • The provider relies on the Runner module to manage the underlying task instances.
  • Tasks can be either scheduled (managed by Croner) or manual (custom logic).
  • Throws an error if a task with the specified name is not found.
  • Designed to be used within the Sodacore dependency injection system.

Released under the Apache-2.0 License.