UNPKG

@usefulish/smartcron

Version:

Declarative and safe cron decorator for NestJS with UTC offset support.

36 lines (35 loc) 1.61 kB
import { SmartCronOptions } from './smart-cron.interface'; /** * Applies a CRON-based task scheduler with smart defaults. * * Generates a CRON expression based on a validated minute interval, optional hour range, * weekday restrictions, and a UTC offset-based timezone. * * @example * ```ts * @SmartCron({ * intervalInMinutes: 15, * fromHour: 9, * toHour: 17, * weekdaysOnly: true, * timezone: -3 * }) * handleTask() { * // Executes every 15 minutes from 09:00 to 17:00, Mon–Fri, in UTC-3 * } * ``` * * @param options - Scheduling configuration. * @param options.intervalInMinutes - Minute interval for execution. Must be included in `ALLOWED_MINUTES`. * @param options.fromHour - Start hour (inclusive), 0–23. Defaults to 0. * @param options.fromMinute - Start minute (inclusive), 0–59. Defaults to 0. * @param options.toHour - End hour (inclusive), 0–23. Defaults to 23. * @param options.toMinute - End minute (inclusive), 0–59. Defaults to 59. * @param options.weekdaysOnly - If true, limits execution to weekdays (Monday to Friday). Defaults to false. * @param options.timezone - UTC offset applied to scheduling (e.g. `-3` for UTC-3, `0` for UTC). Range: -12 to +14. Defaults to 0 (UTC). * * @returns A method decorator that schedules the task using the generated CRON expression(s). * * @throws Error if `intervalInMinutes` is not allowed. */ export declare function SmartCron(options: SmartCronOptions): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;