Skip to content

Commit

Permalink
build(schedule): replace dependency cron with croner
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Feb 12, 2023
1 parent 9da8f45 commit 360d1d3
Show file tree
Hide file tree
Showing 7 changed files with 15,549 additions and 10,748 deletions.
5 changes: 0 additions & 5 deletions lib/decorators/cron.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ export interface CronOptions {
*/
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.
*/
utcOffset?: string | 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.
*/
Expand Down
26 changes: 14 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,17 @@ 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,
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 360d1d3

Please sign in to comment.