Skip to content

Commit

Permalink
build(deps): replace dependency cron with croner
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Feb 13, 2023
1 parent 9da8f45 commit 35dd905
Show file tree
Hide file tree
Showing 7 changed files with 946 additions and 3,027 deletions.
7 changes: 4 additions & 3 deletions lib/decorators/cron.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ export interface CronOptions {
name?: string;

/**
* Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at [Moment Timezone Website](http://momentjs.com/timezone/). Probably don't use both ```timeZone``` and ```utcOffset``` together or weird things may happen.
* Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at [Moment Timezone Website](http://momentjs.com/timezone/). Code will throw if trying to use both ```timeZone``` and ```utcOffset``` together.
*/
timeZone?: string;

/**
* This allows you to specify the offset of your timezone rather than using the ```timeZone``` param. Probably don't use both ```timeZone``` and ```utcOffset``` together or weird things may happen.
* This allows you to specify the offset of your timezone rather than using the ```timeZone``` param. Code will throw if trying to use both ```timeZone``` and ```utcOffset``` together.
*/
utcOffset?: string | number;
utcOffset?: number;

/**
* If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at [timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref) from the NodeJS docs.
* @default false
*/
unrefTimeout?: boolean;

Expand Down
27 changes: 15 additions & 12 deletions lib/scheduler.orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import {
Injectable,
OnApplicationBootstrap,
OnApplicationShutdown,
Logger
} from '@nestjs/common';
import { CronJob } from 'cron';
import { Cron as Croner } from 'croner';
import { v4 } from 'uuid';
import { CronOptions } from './decorators/cron.decorator';
import { SchedulerRegistry } from './scheduler.registry';
Expand All @@ -18,7 +19,7 @@ type CronOptionsHost = {

type IntervalOptions = TargetHost & TimeoutHost & RefHost<number>;
type TimeoutOptions = TargetHost & TimeoutHost & RefHost<number>;
type CronJobOptions = TargetHost & CronOptionsHost & RefHost<CronJob>;
type CronJobOptions = TargetHost & CronOptionsHost & RefHost<Croner>;

@Injectable()
export class SchedulerOrchestrator
Expand All @@ -27,6 +28,8 @@ export class SchedulerOrchestrator
private readonly timeouts: Record<string, TimeoutOptions> = {};
private readonly intervals: Record<string, IntervalOptions> = {};

private readonly logger = new Logger(SchedulerRegistry.name);

constructor(private readonly schedulerRegistry: SchedulerRegistry) {}

onApplicationBootstrap() {
Expand Down Expand Up @@ -72,18 +75,18 @@ export class SchedulerOrchestrator
return;
}

const cronJob = new CronJob(
const cronJob = Croner(
options.cronTime,
target as any,
undefined,
false,
options.timeZone,
undefined,
false,
options.utcOffset,
options.unrefTimeout,
{
timezone: options.timeZone,
unref: options.unrefTimeout,
utcOffset: options.utcOffset,
catch: (error) => {
this.logger.error(error)
}
},
target as any
);
cronJob.start();

this.cronJobs[key].ref = cronJob;
this.schedulerRegistry.addCronJob(key, cronJob);
Expand Down
23 changes: 5 additions & 18 deletions lib/scheduler.registry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Injectable, Logger } from '@nestjs/common';
import { CronJob } from 'cron';
import { Injectable } from '@nestjs/common';
import { Cron as Croner } from 'croner';
import { DUPLICATE_SCHEDULER, NO_SCHEDULER_FOUND } from './schedule.messages';

@Injectable()
export class SchedulerRegistry {
private readonly logger = new Logger(SchedulerRegistry.name);

private readonly cronJobs = new Map<string, CronJob>();
private readonly cronJobs = new Map<string, Croner>();
private readonly timeouts = new Map<string, any>();
private readonly intervals = new Map<string, any>();

Expand Down Expand Up @@ -52,13 +50,11 @@ export class SchedulerRegistry {
return ref;
}

addCronJob(name: string, job: CronJob) {
addCronJob(name: string, job: Croner) {
const ref = this.cronJobs.get(name);
if (ref) {
throw new Error(DUPLICATE_SCHEDULER('Cron Job', name));
}

job.fireOnTick = this.wrapFunctionInTryCatchBlocks(job.fireOnTick, job);
this.cronJobs.set(name, job);
}

Expand All @@ -78,7 +74,7 @@ export class SchedulerRegistry {
this.timeouts.set(name, timeoutId);
}

getCronJobs(): Map<string, CronJob> {
getCronJobs(): Map<string, Croner> {
return this.cronJobs;
}

Expand Down Expand Up @@ -108,13 +104,4 @@ export class SchedulerRegistry {
this.timeouts.delete(name);
}

private wrapFunctionInTryCatchBlocks(methodRef: Function, instance: object): Function {
return async (...args: unknown[]) => {
try {
await methodRef.call(instance, ...args);
} catch (error) {
this.logger.error(error);
}
};
}
}
Loading

0 comments on commit 35dd905

Please sign in to comment.