@navios/schedule 0.5.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/metadata/cron.metadata.mts","../src/metadata/schedule.metadata.mts","../src/decorators/cron.decorator.mts","../src/decorators/schedulable.decorator.mts","../src/cron.constants.mts","../src/scheduler.service.mts"],"names":["Schedule","Injectable"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEO,IAAM,eAAA,GAAkB,OAAO,iBAAiB;AAQhD,SAAS,mBACd,OAAA,EACmB;AACnB,EAAA,IAAI,QAAQ,QAAA,EAAU;AACpB,IAAA,MAAM,QAAA,GAAW,OAAA,CAAQ,QAAA,CAAS,eAAe,CAAA;AAGjD,IAAA,IAAI,QAAA,EAAU;AACZ,MAAA,OAAO,QAAA;AAAA,IACT,CAAA,MAAO;AACL,MAAA,OAAA,CAAQ,QAAA,CAAS,eAAe,CAAA,mBAAI,IAAI,GAAA,EAAkB;AAC1D,MAAA,OAAO,OAAA,CAAQ,SAAS,eAAe,CAAA;AAAA,IACzC;AAAA,EACF;AACA,EAAA,MAAM,IAAI,MAAM,sCAAsC,CAAA;AACxD;AAEO,SAAS,eAAA,CACd,QACA,OAAA,EACc;AACd,EAAA,IAAI,QAAQ,QAAA,EAAU;AACpB,IAAA,MAAM,QAAA,GAAW,mBAAmB,OAAO,CAAA;AAC3C,IAAA,IAAI,QAAA,EAAU;AACZ,MAAA,MAAM,gBAAA,GAAmB,KAAA,CAAM,IAAA,CAAK,QAAQ,CAAA,CAAE,IAAA;AAAA,QAC5C,CAAC,IAAA,KAAS,IAAA,CAAK,WAAA,KAAgB,MAAA,CAAO;AAAA,OACxC;AACA,MAAA,IAAI,gBAAA,EAAkB;AACpB,QAAA,OAAO,gBAAA;AAAA,MACT,CAAA,MAAO;AACL,QAAA,MAAM,WAAA,GAA4B;AAAA,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A,MAAW,GAAA,IAAO,IAAA,CAAK,IAAA,CAAK,MAAA,EAAO,EAAG;AACpC,MAAA,IAAI,IAAI,QAAA,EAAU;AAChB,QAAA;AAAA,MACF;AACA,MAAA,GAAA,CAAI,KAAA,EAAM;AAAA,IACZ;AAAA,EACF;AAAA,EAEA,OAAA,GAAU;AACR,IAAA,KAAA,MAAW,GAAA,IAAO,IAAA,CAAK,IAAA,CAAK,MAAA,EAAO,EAAG;AACpC,MAAA,IAAI,CAAC,IAAI,QAAA,EAAU;AACjB,QAAA;AAAA,MACF;AACA,MAAA,GAAA,CAAI,IAAA,EAAK;AAAA,IACX;AAAA,EACF;AACF,CAAA;AAxEO,KAAA,GAAA,gBAAA,CAAA,CAAA;AAAM,iBAAA,GAAN,gDADP,4BAAA,EACa,iBAAA,CAAA;AAAN,iBAAA,CAAA,KAAA,EAAA,CAAA,EAAM,iBAAA,CAAA;AAAN,IAAM,gBAAA,GAAN","file":"index.mjs","sourcesContent":["import type { CronJobParams } from 'cron'\n\nexport const CronMetadataKey = Symbol('CronMetadataKey')\n\nexport interface CronMetadata {\n classMethod: string\n cronTime: CronJobParams['cronTime'] | null\n disabled: boolean\n}\n\nexport function getAllCronMetadata(\n context: ClassMethodDecoratorContext | ClassDecoratorContext,\n): Set<CronMetadata> {\n if (context.metadata) {\n const metadata = context.metadata[CronMetadataKey] as\n | Set<CronMetadata>\n | undefined\n if (metadata) {\n return metadata\n } else {\n context.metadata[CronMetadataKey] = new Set<CronMetadata>()\n return context.metadata[CronMetadataKey] as Set<CronMetadata>\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n\nexport function getCronMetadata(\n target: Function,\n context: ClassMethodDecoratorContext,\n): CronMetadata {\n if (context.metadata) {\n const metadata = getAllCronMetadata(context)\n if (metadata) {\n const endpointMetadata = Array.from(metadata).find(\n (item) => item.classMethod === target.name,\n )\n if (endpointMetadata) {\n return endpointMetadata\n } else {\n const newMetadata: CronMetadata = {\n classMethod: target.name,\n cronTime: null,\n disabled: false,\n }\n metadata.add(newMetadata)\n return newMetadata\n }\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n","import type { ClassType } from '@navios/core'\n\nimport type { CronMetadata } from './cron.metadata.mjs'\n\nimport { getAllCronMetadata } from './cron.metadata.mjs'\n\nexport const ScheduleMetadataKey = Symbol('ControllerMetadataKey')\n\nexport interface ScheduleMetadata {\n name: string\n jobs: Set<CronMetadata>\n}\n\nexport function getScheduleMetadata(\n target: ClassType,\n context: ClassDecoratorContext,\n): ScheduleMetadata {\n if (context.metadata) {\n const metadata = context.metadata[ScheduleMetadataKey] as\n | ScheduleMetadata\n | undefined\n if (metadata) {\n return metadata\n } else {\n const jobsMetadata = getAllCronMetadata(context)\n const newMetadata: ScheduleMetadata = {\n name: target.name,\n jobs: jobsMetadata,\n }\n context.metadata[ScheduleMetadataKey] = newMetadata\n // @ts-expect-error We add a custom metadata key to the target\n target[ScheduleMetadataKey] = newMetadata\n return newMetadata\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n\nexport function extractScheduleMetadata(target: ClassType): ScheduleMetadata {\n // @ts-expect-error We add a custom metadata key to the target\n const metadata = target[ScheduleMetadataKey] as ScheduleMetadata | undefined\n if (!metadata) {\n throw new Error(\n '[Navios-Schedule] Controller metadata not found. Make sure to use @Controller decorator.',\n )\n }\n return metadata\n}\n\nexport function hasScheduleMetadata(target: ClassType): boolean {\n // @ts-expect-error We add a custom metadata key to the target\n const metadata = target[ScheduleMetadataKey] as ScheduleMetadata | undefined\n\n return !!metadata\n}\n","import type { CronJobParams } from 'cron'\n\nimport { getCronMetadata } from '../metadata/index.mjs'\n\nexport interface CronOptions {\n disabled?: boolean\n}\n\nexport function Cron(\n cronTime: CronJobParams['cronTime'],\n options?: CronOptions,\n) {\n return (\n target: () => Promise<void>,\n context: ClassMethodDecoratorContext,\n ) => {\n if (context.kind !== 'method') {\n throw new Error(\n `Cron can only be applied to methods, not ${context.kind}`,\n )\n }\n if (context.metadata) {\n const metadata = getCronMetadata(target, context)\n metadata.cronTime = cronTime\n metadata.disabled = options?.disabled ?? false\n }\n return target\n }\n}\n","import type { ClassType } from '@navios/core'\n\nimport { Injectable } from '@navios/core'\n\nimport { getScheduleMetadata } from '../metadata/index.mjs'\n\nexport function Schedulable() {\n return (target: ClassType, context: ClassDecoratorContext) => {\n if (context.kind !== 'class') {\n throw new Error(\n `SchedulableDecorator can only be applied to classes, not ${context.kind}`,\n )\n }\n if (context.metadata) {\n getScheduleMetadata(target, context)\n }\n return Injectable()(target, context)\n }\n}\n","export enum Schedule {\n EveryMinute = '*/1 * * * *',\n EveryFiveMinutes = '*/5 * * * *',\n EveryTenMinutes = '*/10 * * * *',\n EveryFifteenMinutes = '*/15 * * * *',\n EveryThirtyMinutes = '*/30 * * * *',\n EveryHour = '0 * * * *',\n EveryTwoHours = '0 */2 * * *',\n EveryThreeHours = '0 */3 * * *',\n EveryFourHours = '0 */4 * * *',\n EverySixHours = '0 */6 * * *',\n EveryTwelveHours = '0 */12 * * *',\n EveryDay = '0 0 * * *',\n EveryWeek = '0 0 * * 0',\n EveryMonth = '0 0 1 * *',\n}\n","import type { ClassType } from '@navios/core'\n\nimport { Container, inject, Injectable, Logger } from '@navios/core'\n\nimport { CronJob } from 'cron'\n\nimport type { ScheduleMetadata } from './metadata/index.mjs'\n\nimport {\n extractScheduleMetadata,\n hasScheduleMetadata,\n} from './metadata/index.mjs'\n\n@Injectable()\nexport class SchedulerService {\n private readonly logger = inject(Logger, {\n context: SchedulerService.name,\n })\n private readonly container = inject(Container)\n private readonly jobs: Map<string, CronJob> = new Map()\n\n register(service: ClassType) {\n if (!hasScheduleMetadata(service)) {\n throw new Error(\n `[Navios-Schedule] Service ${service.name} is not schedulable. Make sure to use @Schedulable decorator.`,\n )\n }\n const metadata = extractScheduleMetadata(service)\n this.logger.debug('Scheduling service', metadata.name)\n this.registerJobs(service, metadata)\n }\n\n getJob<T extends ClassType>(\n service: T,\n method: keyof InstanceType<T>,\n ): CronJob | undefined {\n const metadata = extractScheduleMetadata(service)\n const jobName = `${metadata.name}.${method as string}()`\n return this.jobs.get(jobName)\n }\n\n private registerJobs(service: ClassType, metadata: ScheduleMetadata) {\n const jobs = metadata.jobs\n for (const job of jobs) {\n if (!job.cronTime) {\n this.logger.debug('Skipping job', job.classMethod)\n continue\n }\n const name = `${metadata.name}.${job.classMethod}()`\n const self = this\n const defaultDisabled = false\n const cronJob = CronJob.from({\n cronTime: job.cronTime,\n name,\n async onTick() {\n try {\n self.logger.debug('Executing job', name)\n const instance = await self.container.get(service)\n await instance[job.classMethod]()\n } catch (error) {\n self.logger.error('Error executing job', name, error)\n }\n },\n start: !(defaultDisabled || job.disabled),\n })\n this.jobs.set(name, cronJob)\n }\n }\n\n startAll() {\n for (const job of this.jobs.values()) {\n if (job.isActive) {\n continue\n }\n job.start()\n }\n }\n\n stopAll() {\n for (const job of this.jobs.values()) {\n if (!job.isActive) {\n continue\n }\n job.stop()\n }\n }\n}\n"]}
1
+ {"version":3,"file":"index.mjs","names":["newMetadata: CronMetadata","getAllCronMetadata","ScheduleMetadataKey","Symbol","getScheduleMetadata","target","context","metadata","jobsMetadata","newMetadata","name","jobs","Error","extractScheduleMetadata","hasScheduleMetadata","getCronMetadata","Cron","cronTime","options","target","context","kind","Error","metadata","disabled","Injectable","getScheduleMetadata","Schedulable","target","context","kind","Error","metadata","Schedule","Container","inject","Injectable","Logger","CronJob","extractScheduleMetadata","hasScheduleMetadata","SchedulerService","logger","context","name","container","jobs","Map","register","service","Error","metadata","debug","registerJobs","getJob","method","jobName","get","job","cronTime","classMethod","self","defaultDisabled","cronJob","from","onTick","instance","error","start","disabled","set","startAll","values","isActive","stopAll","stop"],"sources":["../src/metadata/cron.metadata.mts","../src/metadata/schedule.metadata.mts","../src/decorators/cron.decorator.mts","../src/decorators/schedulable.decorator.mts","../src/cron.constants.mts","../src/scheduler.service.mts"],"sourcesContent":["import type { CronJobParams } from 'cron'\n\nexport const CronMetadataKey = Symbol('CronMetadataKey')\n\nexport interface CronMetadata {\n classMethod: string\n cronTime: CronJobParams['cronTime'] | null\n disabled: boolean\n}\n\nexport function getAllCronMetadata(\n context: ClassMethodDecoratorContext | ClassDecoratorContext,\n): Set<CronMetadata> {\n if (context.metadata) {\n const metadata = context.metadata[CronMetadataKey] as\n | Set<CronMetadata>\n | undefined\n if (metadata) {\n return metadata\n } else {\n context.metadata[CronMetadataKey] = new Set<CronMetadata>()\n return context.metadata[CronMetadataKey] as Set<CronMetadata>\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n\nexport function getCronMetadata(\n target: Function,\n context: ClassMethodDecoratorContext,\n): CronMetadata {\n if (context.metadata) {\n const metadata = getAllCronMetadata(context)\n if (metadata) {\n const endpointMetadata = Array.from(metadata).find(\n (item) => item.classMethod === target.name,\n )\n if (endpointMetadata) {\n return endpointMetadata\n } else {\n const newMetadata: CronMetadata = {\n classMethod: target.name,\n cronTime: null,\n disabled: false,\n }\n metadata.add(newMetadata)\n return newMetadata\n }\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n","import type { ClassType } from '@navios/core'\n\nimport type { CronMetadata } from './cron.metadata.mjs'\n\nimport { getAllCronMetadata } from './cron.metadata.mjs'\n\nexport const ScheduleMetadataKey = Symbol('ControllerMetadataKey')\n\nexport interface ScheduleMetadata {\n name: string\n jobs: Set<CronMetadata>\n}\n\nexport function getScheduleMetadata(\n target: ClassType,\n context: ClassDecoratorContext,\n): ScheduleMetadata {\n if (context.metadata) {\n const metadata = context.metadata[ScheduleMetadataKey] as\n | ScheduleMetadata\n | undefined\n if (metadata) {\n return metadata\n } else {\n const jobsMetadata = getAllCronMetadata(context)\n const newMetadata: ScheduleMetadata = {\n name: target.name,\n jobs: jobsMetadata,\n }\n context.metadata[ScheduleMetadataKey] = newMetadata\n // @ts-expect-error We add a custom metadata key to the target\n target[ScheduleMetadataKey] = newMetadata\n return newMetadata\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n\nexport function extractScheduleMetadata(target: ClassType): ScheduleMetadata {\n // @ts-expect-error We add a custom metadata key to the target\n const metadata = target[ScheduleMetadataKey] as ScheduleMetadata | undefined\n if (!metadata) {\n throw new Error(\n '[Navios-Schedule] Controller metadata not found. Make sure to use @Controller decorator.',\n )\n }\n return metadata\n}\n\nexport function hasScheduleMetadata(target: ClassType): boolean {\n // @ts-expect-error We add a custom metadata key to the target\n const metadata = target[ScheduleMetadataKey] as ScheduleMetadata | undefined\n\n return !!metadata\n}\n","import type { CronJobParams } from 'cron'\n\nimport { getCronMetadata } from '../metadata/index.mjs'\n\n/**\n * Options for configuring a cron job.\n * \n * @public\n */\nexport interface CronOptions {\n /**\n * Whether the job should be disabled by default.\n * Disabled jobs won't start automatically but can be manually started later.\n * \n * @default false\n */\n disabled?: boolean\n}\n\n/**\n * Decorator that marks a method to run on a cron schedule.\n * \n * The method must be in a class decorated with `@Schedulable()`.\n * The method will be automatically executed according to the provided cron expression.\n * \n * @param cronTime - Cron expression (5 or 6 fields) or a pre-defined Schedule constant\n * @param options - Optional configuration for the cron job\n * \n * @example\n * ```typescript\n * @Schedulable()\n * class TaskService {\n * // Run daily at midnight\n * @Cron('0 0 * * *')\n * async dailyTask() {\n * console.log('Running daily task')\n * }\n * \n * // Use pre-defined schedule\n * @Cron(Schedule.EveryFiveMinutes)\n * async frequentTask() {\n * console.log('Running every 5 minutes')\n * }\n * \n * // Disabled job (won't start automatically)\n * @Cron('0 2 * * *', { disabled: true })\n * async maintenanceTask() {\n * console.log('Maintenance task')\n * }\n * }\n * ```\n * \n * @throws {Error} If applied to something other than a method\n * \n * @public\n */\nexport function Cron(\n cronTime: CronJobParams['cronTime'],\n options?: CronOptions,\n) {\n return (\n target: () => Promise<void>,\n context: ClassMethodDecoratorContext,\n ) => {\n if (context.kind !== 'method') {\n throw new Error(\n `Cron can only be applied to methods, not ${context.kind}`,\n )\n }\n if (context.metadata) {\n const metadata = getCronMetadata(target, context)\n metadata.cronTime = cronTime\n metadata.disabled = options?.disabled ?? false\n }\n return target\n }\n}\n","import type { ClassType } from '@navios/core'\n\nimport { Injectable } from '@navios/core'\n\nimport { getScheduleMetadata } from '../metadata/index.mjs'\n\n/**\n * Decorator that marks a class as schedulable and makes it injectable.\n * \n * Classes decorated with `@Schedulable()` can contain methods decorated with `@Cron()`\n * that will be automatically scheduled and executed. This decorator also applies\n * the `@Injectable()` decorator, making the class available for dependency injection.\n * \n * @example\n * ```typescript\n * @Schedulable()\n * class TaskService {\n * @Cron('0 0 * * *')\n * async dailyTask() {\n * // This will run daily at midnight\n * }\n * }\n * \n * // Register the service\n * schedulerService.register(TaskService)\n * ```\n * \n * @throws {Error} If applied to something other than a class\n * \n * @public\n */\nexport function Schedulable() {\n return (target: ClassType, context: ClassDecoratorContext) => {\n if (context.kind !== 'class') {\n throw new Error(\n `SchedulableDecorator can only be applied to classes, not ${context.kind}`,\n )\n }\n if (context.metadata) {\n getScheduleMetadata(target, context)\n }\n return Injectable()(target, context)\n }\n}\n","/**\n * Pre-defined cron schedule constants for common scheduling patterns.\n *\n * These constants provide convenient shortcuts for frequently used cron expressions,\n * making it easier to schedule jobs without manually writing cron expressions.\n *\n * @example\n * ```typescript\n * import { Schedule } from '@navios/schedule'\n *\n * @Schedulable()\n * class TaskService {\n * @Cron(Schedule.EveryMinute)\n * async everyMinute() {}\n *\n * @Cron(Schedule.EveryHour)\n * async hourly() {}\n *\n * @Cron(Schedule.EveryDay)\n * async daily() {}\n * }\n * ```\n *\n * @public\n */\nexport enum Schedule {\n EveryMinute = '*/1 * * * *',\n EveryFiveMinutes = '*/5 * * * *',\n EveryTenMinutes = '*/10 * * * *',\n EveryFifteenMinutes = '*/15 * * * *',\n EveryThirtyMinutes = '*/30 * * * *',\n EveryHour = '0 * * * *',\n EveryTwoHours = '0 */2 * * *',\n EveryThreeHours = '0 */3 * * *',\n EveryFourHours = '0 */4 * * *',\n EverySixHours = '0 */6 * * *',\n EveryTwelveHours = '0 */12 * * *',\n EveryDay = '0 0 * * *',\n EveryWeek = '0 0 * * 0',\n EveryMonth = '0 0 1 * *',\n}\n","import type { ClassType } from '@navios/core'\n\nimport { Container, inject, Injectable, Logger } from '@navios/core'\n\nimport { CronJob } from 'cron'\n\nimport type { ScheduleMetadata } from './metadata/index.mjs'\n\nimport {\n extractScheduleMetadata,\n hasScheduleMetadata,\n} from './metadata/index.mjs'\n\n/**\n * Service responsible for managing and executing scheduled cron jobs.\n * \n * The SchedulerService registers schedulable services decorated with `@Schedulable()`\n * and automatically starts their cron jobs based on the `@Cron()` decorator configuration.\n * \n * @example\n * ```typescript\n * import { inject, Injectable } from '@navios/core'\n * import { SchedulerService } from '@navios/schedule'\n * \n * @Injectable()\n * class AppModule {\n * private readonly scheduler = inject(SchedulerService)\n * \n * async onModuleInit() {\n * this.scheduler.register(MySchedulableService)\n * }\n * }\n * ```\n * \n * @public\n */\n@Injectable()\nexport class SchedulerService {\n private readonly logger = inject(Logger, {\n context: SchedulerService.name,\n })\n private readonly container = inject(Container)\n private readonly jobs: Map<string, CronJob> = new Map()\n\n /**\n * Registers a schedulable service and starts all its cron jobs.\n * \n * The service must be decorated with `@Schedulable()` and contain methods\n * decorated with `@Cron()` to be registered successfully.\n * \n * @param service - The schedulable service class to register\n * @throws {Error} If the service is not decorated with `@Schedulable()`\n * \n * @example\n * ```typescript\n * @Schedulable()\n * class TaskService {\n * @Cron('0 0 * * *')\n * async dailyTask() {\n * // Runs daily at midnight\n * }\n * }\n * \n * schedulerService.register(TaskService)\n * ```\n * \n * @public\n */\n register(service: ClassType) {\n if (!hasScheduleMetadata(service)) {\n throw new Error(\n `[Navios-Schedule] Service ${service.name} is not schedulable. Make sure to use @Schedulable decorator.`,\n )\n }\n const metadata = extractScheduleMetadata(service)\n this.logger.debug('Scheduling service', metadata.name)\n this.registerJobs(service, metadata)\n }\n\n /**\n * Retrieves a specific cron job instance for a method in a schedulable service.\n * \n * @param service - The schedulable service class\n * @param method - The name of the method decorated with `@Cron()`\n * @returns The CronJob instance if found, undefined otherwise\n * \n * @example\n * ```typescript\n * const job = schedulerService.getJob(TaskService, 'dailyTask')\n * if (job) {\n * console.log('Job is active:', job.isActive)\n * job.start() // Manually start the job\n * job.stop() // Manually stop the job\n * }\n * ```\n * \n * @public\n */\n getJob<T extends ClassType>(\n service: T,\n method: keyof InstanceType<T>,\n ): CronJob | undefined {\n const metadata = extractScheduleMetadata(service)\n const jobName = `${metadata.name}.${method as string}()`\n return this.jobs.get(jobName)\n }\n\n private registerJobs(service: ClassType, metadata: ScheduleMetadata) {\n const jobs = metadata.jobs\n for (const job of jobs) {\n if (!job.cronTime) {\n this.logger.debug('Skipping job', job.classMethod)\n continue\n }\n const name = `${metadata.name}.${job.classMethod}()`\n const self = this\n const defaultDisabled = false\n const cronJob = CronJob.from({\n cronTime: job.cronTime,\n name,\n async onTick() {\n try {\n self.logger.debug('Executing job', name)\n const instance = await self.container.get(service)\n await instance[job.classMethod]()\n } catch (error) {\n self.logger.error('Error executing job', name, error)\n }\n },\n start: !(defaultDisabled || job.disabled),\n })\n this.jobs.set(name, cronJob)\n }\n }\n\n /**\n * Starts all registered cron jobs that are currently inactive.\n * \n * Only jobs that are not already active will be started. This method\n * is useful for resuming all jobs after calling `stopAll()`.\n * \n * @example\n * ```typescript\n * // Stop all jobs\n * schedulerService.stopAll()\n * \n * // Later, resume all jobs\n * schedulerService.startAll()\n * ```\n * \n * @public\n */\n startAll() {\n for (const job of this.jobs.values()) {\n if (job.isActive) {\n continue\n }\n job.start()\n }\n }\n\n /**\n * Stops all registered cron jobs that are currently active.\n * \n * Only jobs that are currently active will be stopped. This method\n * is useful for pausing all scheduled tasks, for example during\n * application shutdown or maintenance.\n * \n * @example\n * ```typescript\n * // Pause all scheduled jobs\n * schedulerService.stopAll()\n * \n * // Jobs can be resumed later with startAll()\n * schedulerService.startAll()\n * ```\n * \n * @public\n */\n stopAll() {\n for (const job of this.jobs.values()) {\n if (!job.isActive) {\n continue\n }\n job.stop()\n }\n }\n}\n"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package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navios/schedule",
3
- "version": "0.5.0",
3
+ "version": "0.7.1",
4
4
  "author": {
5
5
  "name": "Oleksandr Hanzha",
6
6
  "email": "alex@granted.name"
@@ -12,11 +12,11 @@
12
12
  },
13
13
  "license": "MIT",
14
14
  "peerDependencies": {
15
- "@navios/core": "^0.5.0",
15
+ "@navios/core": "^0.7.1",
16
16
  "zod": "^3.25.0 || ^4.0.0"
17
17
  },
18
18
  "typings": "./lib/index.d.mts",
19
- "main": "./lib/index.js",
19
+ "main": "./lib/index.cjs",
20
20
  "module": "./lib/index.mjs",
21
21
  "exports": {
22
22
  ".": {
@@ -25,17 +25,17 @@
25
25
  "default": "./lib/index.mjs"
26
26
  },
27
27
  "require": {
28
- "types": "./lib/index.d.ts",
29
- "default": "./lib/index.js"
28
+ "types": "./lib/index.d.cts",
29
+ "default": "./lib/index.cjs"
30
30
  }
31
31
  }
32
32
  },
33
33
  "devDependencies": {
34
- "@navios/core": "^0.5.0",
35
- "typescript": "^5.9.2",
36
- "zod": "^4.1.8"
34
+ "@navios/core": "^0.7.1",
35
+ "typescript": "^5.9.3",
36
+ "zod": "^4.2.1"
37
37
  },
38
38
  "dependencies": {
39
- "cron": "^4.3.3"
39
+ "cron": "^4.4.0"
40
40
  }
41
41
  }
package/project.json CHANGED
@@ -25,11 +25,11 @@
25
25
  },
26
26
  "build": {
27
27
  "executor": "nx:run-commands",
28
- "inputs": ["projectSources"],
28
+ "inputs": ["projectSources", "{projectRoot}/tsdown.config.mts"],
29
29
  "outputs": ["{projectRoot}/lib"],
30
30
  "dependsOn": ["check", "test:ci"],
31
31
  "options": {
32
- "command": "tsup",
32
+ "command": "tsdown",
33
33
  "cwd": "packages/schedule"
34
34
  }
35
35
  },
@@ -1,3 +1,28 @@
1
+ /**
2
+ * Pre-defined cron schedule constants for common scheduling patterns.
3
+ *
4
+ * These constants provide convenient shortcuts for frequently used cron expressions,
5
+ * making it easier to schedule jobs without manually writing cron expressions.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { Schedule } from '@navios/schedule'
10
+ *
11
+ * @Schedulable()
12
+ * class TaskService {
13
+ * @Cron(Schedule.EveryMinute)
14
+ * async everyMinute() {}
15
+ *
16
+ * @Cron(Schedule.EveryHour)
17
+ * async hourly() {}
18
+ *
19
+ * @Cron(Schedule.EveryDay)
20
+ * async daily() {}
21
+ * }
22
+ * ```
23
+ *
24
+ * @public
25
+ */
1
26
  export enum Schedule {
2
27
  EveryMinute = '*/1 * * * *',
3
28
  EveryFiveMinutes = '*/5 * * * *',
@@ -2,10 +2,58 @@ import type { CronJobParams } from 'cron'
2
2
 
3
3
  import { getCronMetadata } from '../metadata/index.mjs'
4
4
 
5
+ /**
6
+ * Options for configuring a cron job.
7
+ *
8
+ * @public
9
+ */
5
10
  export interface CronOptions {
11
+ /**
12
+ * Whether the job should be disabled by default.
13
+ * Disabled jobs won't start automatically but can be manually started later.
14
+ *
15
+ * @default false
16
+ */
6
17
  disabled?: boolean
7
18
  }
8
19
 
20
+ /**
21
+ * Decorator that marks a method to run on a cron schedule.
22
+ *
23
+ * The method must be in a class decorated with `@Schedulable()`.
24
+ * The method will be automatically executed according to the provided cron expression.
25
+ *
26
+ * @param cronTime - Cron expression (5 or 6 fields) or a pre-defined Schedule constant
27
+ * @param options - Optional configuration for the cron job
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * @Schedulable()
32
+ * class TaskService {
33
+ * // Run daily at midnight
34
+ * @Cron('0 0 * * *')
35
+ * async dailyTask() {
36
+ * console.log('Running daily task')
37
+ * }
38
+ *
39
+ * // Use pre-defined schedule
40
+ * @Cron(Schedule.EveryFiveMinutes)
41
+ * async frequentTask() {
42
+ * console.log('Running every 5 minutes')
43
+ * }
44
+ *
45
+ * // Disabled job (won't start automatically)
46
+ * @Cron('0 2 * * *', { disabled: true })
47
+ * async maintenanceTask() {
48
+ * console.log('Maintenance task')
49
+ * }
50
+ * }
51
+ * ```
52
+ *
53
+ * @throws {Error} If applied to something other than a method
54
+ *
55
+ * @public
56
+ */
9
57
  export function Cron(
10
58
  cronTime: CronJobParams['cronTime'],
11
59
  options?: CronOptions,
@@ -4,6 +4,31 @@ import { Injectable } from '@navios/core'
4
4
 
5
5
  import { getScheduleMetadata } from '../metadata/index.mjs'
6
6
 
7
+ /**
8
+ * Decorator that marks a class as schedulable and makes it injectable.
9
+ *
10
+ * Classes decorated with `@Schedulable()` can contain methods decorated with `@Cron()`
11
+ * that will be automatically scheduled and executed. This decorator also applies
12
+ * the `@Injectable()` decorator, making the class available for dependency injection.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * @Schedulable()
17
+ * class TaskService {
18
+ * @Cron('0 0 * * *')
19
+ * async dailyTask() {
20
+ * // This will run daily at midnight
21
+ * }
22
+ * }
23
+ *
24
+ * // Register the service
25
+ * schedulerService.register(TaskService)
26
+ * ```
27
+ *
28
+ * @throws {Error} If applied to something other than a class
29
+ *
30
+ * @public
31
+ */
7
32
  export function Schedulable() {
8
33
  return (target: ClassType, context: ClassDecoratorContext) => {
9
34
  if (context.kind !== 'class') {
@@ -11,6 +11,29 @@ import {
11
11
  hasScheduleMetadata,
12
12
  } from './metadata/index.mjs'
13
13
 
14
+ /**
15
+ * Service responsible for managing and executing scheduled cron jobs.
16
+ *
17
+ * The SchedulerService registers schedulable services decorated with `@Schedulable()`
18
+ * and automatically starts their cron jobs based on the `@Cron()` decorator configuration.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { inject, Injectable } from '@navios/core'
23
+ * import { SchedulerService } from '@navios/schedule'
24
+ *
25
+ * @Injectable()
26
+ * class AppModule {
27
+ * private readonly scheduler = inject(SchedulerService)
28
+ *
29
+ * async onModuleInit() {
30
+ * this.scheduler.register(MySchedulableService)
31
+ * }
32
+ * }
33
+ * ```
34
+ *
35
+ * @public
36
+ */
14
37
  @Injectable()
15
38
  export class SchedulerService {
16
39
  private readonly logger = inject(Logger, {
@@ -19,6 +42,30 @@ export class SchedulerService {
19
42
  private readonly container = inject(Container)
20
43
  private readonly jobs: Map<string, CronJob> = new Map()
21
44
 
45
+ /**
46
+ * Registers a schedulable service and starts all its cron jobs.
47
+ *
48
+ * The service must be decorated with `@Schedulable()` and contain methods
49
+ * decorated with `@Cron()` to be registered successfully.
50
+ *
51
+ * @param service - The schedulable service class to register
52
+ * @throws {Error} If the service is not decorated with `@Schedulable()`
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * @Schedulable()
57
+ * class TaskService {
58
+ * @Cron('0 0 * * *')
59
+ * async dailyTask() {
60
+ * // Runs daily at midnight
61
+ * }
62
+ * }
63
+ *
64
+ * schedulerService.register(TaskService)
65
+ * ```
66
+ *
67
+ * @public
68
+ */
22
69
  register(service: ClassType) {
23
70
  if (!hasScheduleMetadata(service)) {
24
71
  throw new Error(
@@ -30,6 +77,25 @@ export class SchedulerService {
30
77
  this.registerJobs(service, metadata)
31
78
  }
32
79
 
80
+ /**
81
+ * Retrieves a specific cron job instance for a method in a schedulable service.
82
+ *
83
+ * @param service - The schedulable service class
84
+ * @param method - The name of the method decorated with `@Cron()`
85
+ * @returns The CronJob instance if found, undefined otherwise
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * const job = schedulerService.getJob(TaskService, 'dailyTask')
90
+ * if (job) {
91
+ * console.log('Job is active:', job.isActive)
92
+ * job.start() // Manually start the job
93
+ * job.stop() // Manually stop the job
94
+ * }
95
+ * ```
96
+ *
97
+ * @public
98
+ */
33
99
  getJob<T extends ClassType>(
34
100
  service: T,
35
101
  method: keyof InstanceType<T>,
@@ -67,6 +133,23 @@ export class SchedulerService {
67
133
  }
68
134
  }
69
135
 
136
+ /**
137
+ * Starts all registered cron jobs that are currently inactive.
138
+ *
139
+ * Only jobs that are not already active will be started. This method
140
+ * is useful for resuming all jobs after calling `stopAll()`.
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * // Stop all jobs
145
+ * schedulerService.stopAll()
146
+ *
147
+ * // Later, resume all jobs
148
+ * schedulerService.startAll()
149
+ * ```
150
+ *
151
+ * @public
152
+ */
70
153
  startAll() {
71
154
  for (const job of this.jobs.values()) {
72
155
  if (job.isActive) {
@@ -76,6 +159,24 @@ export class SchedulerService {
76
159
  }
77
160
  }
78
161
 
162
+ /**
163
+ * Stops all registered cron jobs that are currently active.
164
+ *
165
+ * Only jobs that are currently active will be stopped. This method
166
+ * is useful for pausing all scheduled tasks, for example during
167
+ * application shutdown or maintenance.
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * // Pause all scheduled jobs
172
+ * schedulerService.stopAll()
173
+ *
174
+ * // Jobs can be resumed later with startAll()
175
+ * schedulerService.startAll()
176
+ * ```
177
+ *
178
+ * @public
179
+ */
79
180
  stopAll() {
80
181
  for (const job of this.jobs.values()) {
81
182
  if (!job.isActive) {
@@ -0,0 +1,33 @@
1
+ import { withFilter } from 'rolldown/filter'
2
+ import { defineConfig } from 'tsdown'
3
+ import swc from 'unplugin-swc'
4
+
5
+ export default defineConfig({
6
+ entry: ['src/index.mts'],
7
+ outDir: 'lib',
8
+ format: ['esm', 'cjs'],
9
+ clean: true,
10
+ treeshake: true,
11
+ sourcemap: true,
12
+ platform: 'node',
13
+ dts: true,
14
+ target: 'es2022',
15
+ plugins: [
16
+ withFilter(
17
+ swc.rolldown({
18
+ jsc: {
19
+ target: 'es2022',
20
+ parser: {
21
+ syntax: 'typescript',
22
+ decorators: true,
23
+ },
24
+ transform: {
25
+ decoratorVersion: '2022-03',
26
+ },
27
+ },
28
+ }),
29
+ // Only run this transform if the file contains a decorator.
30
+ { transform: { code: '@' } },
31
+ ),
32
+ ],
33
+ })
@@ -1,106 +0,0 @@
1
- import type { ClassType } from '@navios/core';
2
- import { CronJob } from 'cron';
3
- import type { CronJobParams } from 'cron';
4
-
5
- declare function Cron(cronTime: CronJobParams['cronTime'], options?: CronOptions): (target: () => Promise<void>, context: ClassMethodDecoratorContext) => () => Promise<void>;
6
- export { Cron }
7
- export { Cron as Cron_alias_1 }
8
- export { Cron as Cron_alias_2 }
9
-
10
- declare interface CronMetadata {
11
- classMethod: string;
12
- cronTime: CronJobParams['cronTime'] | null;
13
- disabled: boolean;
14
- }
15
- export { CronMetadata }
16
- export { CronMetadata as CronMetadata_alias_1 }
17
- export { CronMetadata as CronMetadata_alias_2 }
18
-
19
- declare const CronMetadataKey: unique symbol;
20
- export { CronMetadataKey }
21
- export { CronMetadataKey as CronMetadataKey_alias_1 }
22
- export { CronMetadataKey as CronMetadataKey_alias_2 }
23
-
24
- declare interface CronOptions {
25
- disabled?: boolean;
26
- }
27
- export { CronOptions }
28
- export { CronOptions as CronOptions_alias_1 }
29
- export { CronOptions as CronOptions_alias_2 }
30
-
31
- declare function extractScheduleMetadata(target: ClassType): ScheduleMetadata;
32
- export { extractScheduleMetadata }
33
- export { extractScheduleMetadata as extractScheduleMetadata_alias_1 }
34
- export { extractScheduleMetadata as extractScheduleMetadata_alias_2 }
35
-
36
- declare function getAllCronMetadata(context: ClassMethodDecoratorContext | ClassDecoratorContext): Set<CronMetadata>;
37
- export { getAllCronMetadata }
38
- export { getAllCronMetadata as getAllCronMetadata_alias_1 }
39
- export { getAllCronMetadata as getAllCronMetadata_alias_2 }
40
-
41
- declare function getCronMetadata(target: Function, context: ClassMethodDecoratorContext): CronMetadata;
42
- export { getCronMetadata }
43
- export { getCronMetadata as getCronMetadata_alias_1 }
44
- export { getCronMetadata as getCronMetadata_alias_2 }
45
-
46
- declare function getScheduleMetadata(target: ClassType, context: ClassDecoratorContext): ScheduleMetadata;
47
- export { getScheduleMetadata }
48
- export { getScheduleMetadata as getScheduleMetadata_alias_1 }
49
- export { getScheduleMetadata as getScheduleMetadata_alias_2 }
50
-
51
- declare function hasScheduleMetadata(target: ClassType): boolean;
52
- export { hasScheduleMetadata }
53
- export { hasScheduleMetadata as hasScheduleMetadata_alias_1 }
54
- export { hasScheduleMetadata as hasScheduleMetadata_alias_2 }
55
-
56
- declare function Schedulable(): (target: ClassType, context: ClassDecoratorContext) => ClassType;
57
- export { Schedulable }
58
- export { Schedulable as Schedulable_alias_1 }
59
- export { Schedulable as Schedulable_alias_2 }
60
-
61
- declare enum Schedule {
62
- EveryMinute = "*/1 * * * *",
63
- EveryFiveMinutes = "*/5 * * * *",
64
- EveryTenMinutes = "*/10 * * * *",
65
- EveryFifteenMinutes = "*/15 * * * *",
66
- EveryThirtyMinutes = "*/30 * * * *",
67
- EveryHour = "0 * * * *",
68
- EveryTwoHours = "0 */2 * * *",
69
- EveryThreeHours = "0 */3 * * *",
70
- EveryFourHours = "0 */4 * * *",
71
- EverySixHours = "0 */6 * * *",
72
- EveryTwelveHours = "0 */12 * * *",
73
- EveryDay = "0 0 * * *",
74
- EveryWeek = "0 0 * * 0",
75
- EveryMonth = "0 0 1 * *"
76
- }
77
- export { Schedule }
78
- export { Schedule as Schedule_alias_1 }
79
-
80
- declare interface ScheduleMetadata {
81
- name: string;
82
- jobs: Set<CronMetadata>;
83
- }
84
- export { ScheduleMetadata }
85
- export { ScheduleMetadata as ScheduleMetadata_alias_1 }
86
- export { ScheduleMetadata as ScheduleMetadata_alias_2 }
87
-
88
- declare const ScheduleMetadataKey: unique symbol;
89
- export { ScheduleMetadataKey }
90
- export { ScheduleMetadataKey as ScheduleMetadataKey_alias_1 }
91
- export { ScheduleMetadataKey as ScheduleMetadataKey_alias_2 }
92
-
93
- declare class SchedulerService {
94
- private readonly logger;
95
- private readonly container;
96
- private readonly jobs;
97
- register(service: ClassType): void;
98
- getJob<T extends ClassType>(service: T, method: keyof InstanceType<T>): CronJob | undefined;
99
- private registerJobs;
100
- startAll(): void;
101
- stopAll(): void;
102
- }
103
- export { SchedulerService }
104
- export { SchedulerService as SchedulerService_alias_1 }
105
-
106
- export { }
@@ -1,106 +0,0 @@
1
- import type { ClassType } from '@navios/core';
2
- import { CronJob } from 'cron';
3
- import type { CronJobParams } from 'cron';
4
-
5
- declare function Cron(cronTime: CronJobParams['cronTime'], options?: CronOptions): (target: () => Promise<void>, context: ClassMethodDecoratorContext) => () => Promise<void>;
6
- export { Cron }
7
- export { Cron as Cron_alias_1 }
8
- export { Cron as Cron_alias_2 }
9
-
10
- declare interface CronMetadata {
11
- classMethod: string;
12
- cronTime: CronJobParams['cronTime'] | null;
13
- disabled: boolean;
14
- }
15
- export { CronMetadata }
16
- export { CronMetadata as CronMetadata_alias_1 }
17
- export { CronMetadata as CronMetadata_alias_2 }
18
-
19
- declare const CronMetadataKey: unique symbol;
20
- export { CronMetadataKey }
21
- export { CronMetadataKey as CronMetadataKey_alias_1 }
22
- export { CronMetadataKey as CronMetadataKey_alias_2 }
23
-
24
- declare interface CronOptions {
25
- disabled?: boolean;
26
- }
27
- export { CronOptions }
28
- export { CronOptions as CronOptions_alias_1 }
29
- export { CronOptions as CronOptions_alias_2 }
30
-
31
- declare function extractScheduleMetadata(target: ClassType): ScheduleMetadata;
32
- export { extractScheduleMetadata }
33
- export { extractScheduleMetadata as extractScheduleMetadata_alias_1 }
34
- export { extractScheduleMetadata as extractScheduleMetadata_alias_2 }
35
-
36
- declare function getAllCronMetadata(context: ClassMethodDecoratorContext | ClassDecoratorContext): Set<CronMetadata>;
37
- export { getAllCronMetadata }
38
- export { getAllCronMetadata as getAllCronMetadata_alias_1 }
39
- export { getAllCronMetadata as getAllCronMetadata_alias_2 }
40
-
41
- declare function getCronMetadata(target: Function, context: ClassMethodDecoratorContext): CronMetadata;
42
- export { getCronMetadata }
43
- export { getCronMetadata as getCronMetadata_alias_1 }
44
- export { getCronMetadata as getCronMetadata_alias_2 }
45
-
46
- declare function getScheduleMetadata(target: ClassType, context: ClassDecoratorContext): ScheduleMetadata;
47
- export { getScheduleMetadata }
48
- export { getScheduleMetadata as getScheduleMetadata_alias_1 }
49
- export { getScheduleMetadata as getScheduleMetadata_alias_2 }
50
-
51
- declare function hasScheduleMetadata(target: ClassType): boolean;
52
- export { hasScheduleMetadata }
53
- export { hasScheduleMetadata as hasScheduleMetadata_alias_1 }
54
- export { hasScheduleMetadata as hasScheduleMetadata_alias_2 }
55
-
56
- declare function Schedulable(): (target: ClassType, context: ClassDecoratorContext) => ClassType;
57
- export { Schedulable }
58
- export { Schedulable as Schedulable_alias_1 }
59
- export { Schedulable as Schedulable_alias_2 }
60
-
61
- declare enum Schedule {
62
- EveryMinute = "*/1 * * * *",
63
- EveryFiveMinutes = "*/5 * * * *",
64
- EveryTenMinutes = "*/10 * * * *",
65
- EveryFifteenMinutes = "*/15 * * * *",
66
- EveryThirtyMinutes = "*/30 * * * *",
67
- EveryHour = "0 * * * *",
68
- EveryTwoHours = "0 */2 * * *",
69
- EveryThreeHours = "0 */3 * * *",
70
- EveryFourHours = "0 */4 * * *",
71
- EverySixHours = "0 */6 * * *",
72
- EveryTwelveHours = "0 */12 * * *",
73
- EveryDay = "0 0 * * *",
74
- EveryWeek = "0 0 * * 0",
75
- EveryMonth = "0 0 1 * *"
76
- }
77
- export { Schedule }
78
- export { Schedule as Schedule_alias_1 }
79
-
80
- declare interface ScheduleMetadata {
81
- name: string;
82
- jobs: Set<CronMetadata>;
83
- }
84
- export { ScheduleMetadata }
85
- export { ScheduleMetadata as ScheduleMetadata_alias_1 }
86
- export { ScheduleMetadata as ScheduleMetadata_alias_2 }
87
-
88
- declare const ScheduleMetadataKey: unique symbol;
89
- export { ScheduleMetadataKey }
90
- export { ScheduleMetadataKey as ScheduleMetadataKey_alias_1 }
91
- export { ScheduleMetadataKey as ScheduleMetadataKey_alias_2 }
92
-
93
- declare class SchedulerService {
94
- private readonly logger;
95
- private readonly container;
96
- private readonly jobs;
97
- register(service: ClassType): void;
98
- getJob<T extends ClassType>(service: T, method: keyof InstanceType<T>): CronJob | undefined;
99
- private registerJobs;
100
- startAll(): void;
101
- stopAll(): void;
102
- }
103
- export { SchedulerService }
104
- export { SchedulerService as SchedulerService_alias_1 }
105
-
106
- export { }
package/lib/index.d.ts DELETED
@@ -1,14 +0,0 @@
1
- export { Cron } from './_tsup-dts-rollup.js';
2
- export { CronOptions } from './_tsup-dts-rollup.js';
3
- export { Schedulable } from './_tsup-dts-rollup.js';
4
- export { getAllCronMetadata } from './_tsup-dts-rollup.js';
5
- export { getCronMetadata } from './_tsup-dts-rollup.js';
6
- export { CronMetadataKey } from './_tsup-dts-rollup.js';
7
- export { CronMetadata } from './_tsup-dts-rollup.js';
8
- export { getScheduleMetadata } from './_tsup-dts-rollup.js';
9
- export { extractScheduleMetadata } from './_tsup-dts-rollup.js';
10
- export { hasScheduleMetadata } from './_tsup-dts-rollup.js';
11
- export { ScheduleMetadataKey } from './_tsup-dts-rollup.js';
12
- export { ScheduleMetadata } from './_tsup-dts-rollup.js';
13
- export { Schedule_alias_1 as Schedule } from './_tsup-dts-rollup.js';
14
- export { SchedulerService } from './_tsup-dts-rollup.js';