Skip to content

Task Decorator

This file defines the Task decorator, which is used to mark classes as scheduled tasks (cron jobs) in the @sodacore/core library. It attaches scheduling metadata to the class, allowing it to be run automatically based on a cron pattern using the Croner library.


Features

  • Scheduled Tasks: Turns a class into a scheduled cron job by specifying a schedule and optional settings.
  • Metadata Management: Stores the task type, schedule, and settings as metadata for later discovery and execution.

Usage

typescript
import Task from './decorator/task';

@Task('0 0 * * *', { timezone: 'UTC' })
export class MyDailyTask extends BaseTask {
	async run() {
		// Task logic here
	}
}
  • The schedule parameter uses standard cron syntax.
  • The settings parameter allows for additional scheduling options (see Croner documentation).

API

Task(schedule?: string, settings?: ITaskSettings): ClassDecorator

  • schedule: (optional) Cron pattern string. Defaults to 'manual' if not provided.
  • settings: (optional) Additional scheduling options as an ITaskSettings object.
  • Returns: A class decorator that registers the class as a scheduled task.

Notes

  • Use this decorator on classes that extend BaseTask to enable scheduled execution.
  • The decorator sets the following metadata:
    • type (under autowire): 'task'
    • schedule (under task): The cron pattern or 'manual'
    • settings (under task): The provided settings object or an empty object
  • The scheduling is powered by the Croner library.

Released under the Apache-2.0 License.