-
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
Showing
7 changed files
with
127 additions
and
67 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,73 @@ | ||
import * as schedule from "node-schedule"; | ||
import { NEST_SCHEDULE_JOB_KEY } from "./constants"; | ||
|
||
export abstract class NestDistributedSchedule { | ||
private readonly jobs; | ||
private readonly timers = {}; | ||
|
||
protected constructor() { | ||
this.jobs = Reflect.getMetadata(NEST_SCHEDULE_JOB_KEY, new.target.prototype); | ||
this.init(); | ||
} | ||
|
||
abstract async tryLock(method: string): Promise<() => void>; | ||
|
||
private async do(invoked) { | ||
if (invoked instanceof Promise) { | ||
return await invoked; | ||
} | ||
return invoked; | ||
} | ||
|
||
private init() { | ||
if (this.jobs) { | ||
this.jobs.forEach(async job => { | ||
if (job.cron) { | ||
job = schedule.scheduleJob({ | ||
startTime: job.startTime, | ||
endTime: job.endTime, | ||
rule: job.cron | ||
}, async () => { | ||
try { | ||
const release = await this.do(this.tryLock(job.key)); | ||
const result = await this.do(this[job.key]()); | ||
if (result && job) { | ||
job.cancel(); | ||
} | ||
typeof release === "function" ? release() : void 0; | ||
} catch (e) { | ||
} | ||
}); | ||
} | ||
if (job.interval) { | ||
this.timers[job.key] = setInterval(async () => { | ||
try { | ||
const release = await this.do(this.tryLock(job.key)); | ||
const result = await this.do(this[job.key]()); | ||
if (result && this.timers[job.key]) { | ||
clearInterval(this.timers[job.key]); | ||
delete this.timers[job.key]; | ||
} | ||
typeof release === "function" ? release() : void 0; | ||
} catch (e) { | ||
} | ||
}, job.interval); | ||
} | ||
if (job.timeout) { | ||
this.timers[job.key] = setTimeout(async () => { | ||
try { | ||
const release = await this.do(this.tryLock(job.key)); | ||
const result = await this.do(this[job.key]()); | ||
if (result && this.timers[job.key]) { | ||
clearTimeout(this.timers[job.key]); | ||
delete this.timers[job.key]; | ||
} | ||
typeof release === "function" ? release() : void 0; | ||
} catch (e) { | ||
} | ||
}, 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 |
---|---|---|
@@ -1,59 +1,7 @@ | ||
import "reflect-metadata"; | ||
import { NEST_SCHEDULE_JOB_KEY } from "./constants"; | ||
import * as schedule from "node-schedule"; | ||
import { NestDistributedSchedule } from "./NestDistributedSchedule"; | ||
|
||
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); | ||
} | ||
}); | ||
} | ||
export class NestSchedule extends NestDistributedSchedule { | ||
async tryLock(method: string): Promise<any> { | ||
return false; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./Schedule"; | ||
export * from "./NestSchedule"; | ||
export * from "./constants"; | ||
export * from "./constants"; | ||
export * from "./NestDistributedSchedule"; |
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.2.1", | ||
"version": "0.3.0", | ||
"description": "Nest - modern, fast, powerful node.js web framework (@schedule)", | ||
"author": "Miaowing <[email protected]>", | ||
"license": "MIT", | ||
|