-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhaofeng
committed
Aug 22, 2018
1 parent
778ed28
commit 5cc07a1
Showing
8 changed files
with
92 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const NEST_SCHEDULE_JOB_KEY = "nest_schedule_jobs"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|