Skip to content

Commit

Permalink
bug fix: #1
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaofeng committed Aug 22, 2018
1 parent 778ed28 commit 5cc07a1
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 66 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ $ npm i --save nest-schedule

```typescript
import { Injectable } from '@nestjs/common';
import { Schedule } from 'nest-schedule';
import { Schedule, NestSchedule } from 'nest-schedule';

@Injectable()
export class ScheduleService {
export class ScheduleService extends NestSchedule {
constructor() {
super();
}

@Schedule({
cron: '0 0 2 * *',
startTime: new Date(),
endTime: new Date(new Date().getTime() + 24 * 60 * 60 * 1000)
})
syncData() {
async syncData() {
console.log('syncing data ...');
}

Expand All @@ -67,7 +71,7 @@ export class ScheduleService {
intervalJob() {
console.log('interval job');

// if you want to cancel the schedule job, you should return true;
// if you want to cancel the job, you should return true;
return true;
}
}
Expand Down
58 changes: 58 additions & 0 deletions lib/NestSchedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { NEST_SCHEDULE_JOB_KEY } from "./constants";
import * as schedule from "node-schedule";

export class NestSchedule {
private readonly jobs;
private readonly timers = {};

constructor() {
this.jobs = Reflect.getMetadata(NEST_SCHEDULE_JOB_KEY, new.target.prototype);
this.init();
}

private init() {
if (this.jobs) {
this.jobs.forEach(job => {
if (job.cron) {
job = schedule.scheduleJob({
startTime: job.startTime,
endTime: job.endTime,
rule: job.cron
}, async () => {
let result = this[job.key]();
if (result instanceof Promise) {
result = await result;
}
if (result && job) {
job.cancel();
}
});
}
if (job.interval) {
this.timers[job.key] = setInterval(async () => {
let result = this[job.key]();
if (result instanceof Promise) {
result = await result;
}
if (result && this.timers[job.key]) {
clearInterval(this.timers[job.key]);
delete this.timers[job.key];
}
}, job.interval);
}
if (job.timeout) {
this.timers[job.key] = setTimeout(async () => {
let result = this[job.key]();
if (result instanceof Promise) {
result = await result;
}
if (result && this.timers[job.key]) {
clearTimeout(this.timers[job.key]);
delete this.timers[job.key];
}
}, job.timeout);
}
});
}
}
}
20 changes: 20 additions & 0 deletions lib/Schedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import "reflect-metadata";
import { NEST_SCHEDULE_JOB_KEY } from "./constants";

export interface Options {
cron?: string;
interval?: number;
timeout?: number;
startTime?: Date;
endTime?: Date;
}

export const Schedule = (options: Options) => (target, key, descriptor) => {
let jobs = Reflect.getMetadata(NEST_SCHEDULE_JOB_KEY, target);
if (!jobs) {
jobs = [];
}

jobs.push({ ...options, key });
Reflect.defineMetadata(NEST_SCHEDULE_JOB_KEY, jobs, target);
};
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NEST_SCHEDULE_JOB_KEY = "nest_schedule_jobs";
59 changes: 0 additions & 59 deletions lib/decorators/Schedule.ts

This file was deleted.

4 changes: 3 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./decorators/Schedule";
export * from "./Schedule";
export * from "./NestSchedule";
export * from "./constants";
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nest-schedule",
"version": "0.1.0",
"version": "0.2.0",
"description": "Nest - modern, fast, powerful node.js web framework (@schedule)",
"author": "Miaowing <[email protected]>",
"license": "MIT",
Expand Down

0 comments on commit 5cc07a1

Please sign in to comment.