/** * Enhanced Schedule Decorators for Worker Mode * * This module provides decorators for scheduled tasks that: * - Only run in Worker or Hybrid mode (not in HTTP-only mode) * - Support distributed locking via Redis * - Support retry logic for failed tasks * - Provide automatic lock key generation * - Support task execution logging and error handling */ import type { CronOptions } from '@nestjs/schedule'; import type { MonitorConfig } from '@sentry/core'; export type SentryCronOptions = Omit & { /** * Enables or disables Sentry Cron check-ins for this task. * The monitor slug, schedule, and timezone are derived from WorkerCron inputs. */ enabled?: boolean; }; /** * Options for schedule decorators with lock support */ export interface ScheduleWithLockOptions { /** * Lock TTL in milliseconds * For Cron: defaults to 3600000 (1 hour) * For Interval: defaults to 80% of interval duration * For Timeout: defaults to 300000 (5 minutes) */ lockTtl?: number; /** * Number of retry attempts if task fails. * This does not control distributed lock acquisition retries; scheduled * lock acquisition is intentionally a single immediate attempt. * * @deprecated Prefer taskRetryCount for clarity. * @default 0 */ retryCount?: number; /** * Number of retry attempts if task execution fails. * This does not control distributed lock acquisition retries; scheduled * lock acquisition is intentionally a single immediate attempt. * @default 0 */ taskRetryCount?: number; /** * Delay between retry attempts in milliseconds * @default 1000 */ retryDelay?: number; /** * Whether to use exponential backoff for retries * @default false */ useExponentialBackoff?: boolean; /** * Maximum retry delay in milliseconds (for exponential backoff) * @default 60000 (1 minute) */ maxRetryDelay?: number; /** * Whether to log task execution * @default true */ logExecution?: boolean; /** * Custom lock key prefix * @default 'schedule' */ lockKeyPrefix?: string; /** * Automatic lock extension interval in milliseconds * If set, the lock will be automatically extended at this interval * @default 0 (no automatic extension) */ autoExtendLock?: number; /** * Callback function when task execution succeeds */ onSuccess?: (duration: number) => void | Promise; /** * Callback function when task execution fails */ onError?: (error: Error) => void | Promise; /** * Whether to skip execution if lock cannot be acquired * @default true */ skipIfLocked?: boolean; /** * TTL for the successful-execution marker in milliseconds. * While this marker exists, later workers skip the same scheduled tick even * after the execution lock has been released. * Set to 0 to disable successful-execution dedupe. */ dedupeTtl?: number; /** * Sentry Cron monitoring options. * * When enabled, monitorSlug is derived from the generated lock key * (service + method), schedule is derived from cronTime, and timezone is * derived from CronOptions.timeZone. These fields are intentionally not * configurable here to avoid split-brain schedule definitions. */ sentryCron?: boolean | SentryCronOptions; } /** * Cron decorator that only executes in Worker or Hybrid mode * * @param cronTime - Cron expression * @param options - Cron options * * @example * ```typescript * @WorkerCron('0 0 * * *') // Run daily at midnight * async dailyTask() { * // Task logic * } * ``` */ export declare function WorkerCron(cronTime: string | Date, options?: CronOptions): MethodDecorator; /** * Interval decorator that only executes in Worker or Hybrid mode * * @param timeout - Interval in milliseconds * @param name - Optional name for the interval * * @example * ```typescript * @WorkerInterval(60000) // Run every minute * async minutelyTask() { * // Task logic * } * ``` */ export declare function WorkerInterval(timeout: number, name?: string): MethodDecorator; /** * Timeout decorator that only executes in Worker or Hybrid mode * * @param timeout - Timeout in milliseconds * @param name - Optional name for the timeout * * @example * ```typescript * @WorkerTimeout(5000) // Run once after 5 seconds * async startupTask() { * // Task logic * } * ``` */ export declare function WorkerTimeout(timeout: number, name?: string): MethodDecorator; /** * Cron decorator with distributed lock support * Automatically generates lock key from class and method name * * @param cronTime - Cron expression * @param lockKeyOrOptions - Lock key (string) or options object * @param lockTtl - Lock TTL in milliseconds (only if lockKeyOrOptions is a string) * * @param cronOptions * @example * ```typescript * // Using string lock key * @WorkerCronWithLock('0 * * * *', 'hourly-task', 3600000) * async hourlyTask() { * // Task logic * } * * // Using options object (auto-generates lock key) * @WorkerCronWithLock('0 * * * *', { lockTtl: 3600000, retryCount: 3 }) * async hourlyTask() { * // Task logic * } * ``` */ export declare function WorkerCronWithLock(cronTime: string | Date, lockKeyOrOptions: string | ScheduleWithLockOptions, lockTtl?: number, cronOptions?: CronOptions): MethodDecorator; /** * Interval decorator with distributed lock support * * @param timeout - Interval in milliseconds * @param lockKeyOrOptions - Lock key (string) or options object * @param lockTtl - Lock TTL in milliseconds (only if lockKeyOrOptions is a string) * * @example * ```typescript * // Using string lock key * @WorkerIntervalWithLock(60000, 'status-check', 50000) * async checkStatus() { * // Task logic * } * * // Using options object (auto-generates lock key) * @WorkerIntervalWithLock(60000, { retryCount: 2 }) * async checkStatus() { * // Task logic * } * ``` */ export declare function WorkerIntervalWithLock(timeout: number, lockKeyOrOptions: string | ScheduleWithLockOptions, lockTtl?: number): MethodDecorator; /** * Timeout decorator with distributed lock support * * @param timeout - Timeout in milliseconds * @param lockKeyOrOptions - Lock key (string) or options object * @param lockTtl - Lock TTL in milliseconds (only if lockKeyOrOptions is a string) * * @example * ```typescript * // Using string lock key * @WorkerTimeoutWithLock(5000, 'init-task', 10000) * async initializeSystem() { * // Task logic * } * * // Using options object (auto-generates lock key) * @WorkerTimeoutWithLock(5000, { lockTtl: 10000 }) * async initializeSystem() { * // Task logic * } * ``` */ export declare function WorkerTimeoutWithLock(timeout: number, lockKeyOrOptions: string | ScheduleWithLockOptions, lockTtl?: number): MethodDecorator; /** * Smart Cron decorator with auto-generated lock key and intelligent defaults * Recommended for most use cases * * @param cronTime - Cron expression * @param options - Schedule options * * @example * ```typescript * @WorkerCronSmart('0 * * * *') // Run hourly with auto-generated lock key * async hourlyTask() { * // Task logic * } * * @WorkerCronSmart('0 0 * * *', { * lockTtl: 7200000, // 2 hours * retryCount: 3, * onError: (error) => console.error('Task failed:', error), * }) * async dailyTask() { * // Task logic * } * ``` */ export declare function WorkerCronSmart(cronTime: string | Date, options?: ScheduleWithLockOptions, cronOptions?: CronOptions): MethodDecorator; /** * Smart Interval decorator with auto-generated lock key and intelligent defaults * Lock TTL is automatically set to 80% of interval duration * * @param timeout - Interval in milliseconds * @param options - Schedule options * * @example * ```typescript * @WorkerIntervalSmart(120000) // Run every 2 minutes, lock for ~1.6 minutes * async checkStatus() { * // Task logic * } * ``` */ export declare function WorkerIntervalSmart(timeout: number, options?: ScheduleWithLockOptions): MethodDecorator; /** * Advanced Cron decorator with full control * Alias for WorkerCronSmart with explicit lock key parameter * * @param cronTime - Cron expression * @param lockKeyOrOptions - Lock key (string) or options object * @param options - Schedule options (only if lockKeyOrOptions is a string) * * @example * ```typescript * @WorkerCronAdvanced('0 0 * * *', 'daily-report', { * lockTtl: 7200000, * retryCount: 5, * useExponentialBackoff: true, * }) * async generateDailyReport() { * // Task logic * } * ``` */ export declare function WorkerCronAdvanced(cronTime: string | Date, lockKeyOrOptions: string | ScheduleWithLockOptions, options?: ScheduleWithLockOptions, cronOptions?: CronOptions): MethodDecorator;