This repository has been archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
f7d61b4
commit c9f0c5f
Showing
7 changed files
with
4,098 additions
and
676 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {ScheduledTask} from 'node-cron'; | ||
|
||
export default class Cron { | ||
public static getCron() { | ||
if (!Cron.instance) { | ||
Cron.instance = new Cron(); | ||
} | ||
return Cron.instance; | ||
} | ||
|
||
public static cleanCron(): void { | ||
Cron.instance = new Cron(); | ||
} | ||
|
||
private static instance: Cron; | ||
|
||
private tasks: { [key: string]: ScheduledTask }; | ||
|
||
private constructor() { | ||
this.tasks = {}; | ||
} | ||
|
||
public add(id: string, job: ScheduledTask) { | ||
this.tasks[id] = job; | ||
} | ||
|
||
public has(id: string): boolean { | ||
return this.tasks.hasOwnProperty(id); | ||
} | ||
|
||
public start(id: string) { | ||
const task = this.tasks[id]; | ||
if (task) { | ||
task.start(); | ||
} | ||
} | ||
|
||
public stop(id: string) { | ||
const job = this.tasks[id]; | ||
if (job) { | ||
job.stop(); | ||
} | ||
} | ||
|
||
public destroy(id: string) { | ||
const job = this.tasks[id]; | ||
if (job) { | ||
job.destroy(); | ||
delete this.tasks[id]; | ||
} | ||
} | ||
} |
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,5 +1,7 @@ | ||
import {Factory, Lair} from 'lair-db/dist'; | ||
import Cron from './cron'; | ||
import Job from './job'; | ||
import Route from './route'; | ||
import Server from './server'; | ||
|
||
export {Factory, Lair, Route, Server}; | ||
export {Factory, Lair, Route, Server, Cron, Job}; |
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,123 @@ | ||
import {ScheduledTask} from 'node-cron'; | ||
import cron = require('node-cron'); | ||
import Cron from './cron'; | ||
import {assert} from './utils'; | ||
|
||
function noop() { | ||
return null; | ||
} | ||
|
||
export type tickCallback = (val: any, indx?: number) => any; | ||
|
||
export interface JobOptions { | ||
/** | ||
* Job identifier | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* Cron-string | ||
*/ | ||
frequency: string; | ||
|
||
/** | ||
* Number of `tick`s | ||
*/ | ||
ticksCount: number; | ||
|
||
/** | ||
* Time in seconds | ||
*/ | ||
ticksDelay: number; | ||
|
||
/** | ||
* When job should be stopped | ||
*/ | ||
endTime?: number; | ||
|
||
immediateStart?: boolean; | ||
|
||
/** | ||
* Start job | ||
*/ | ||
firstTick?: tickCallback; | ||
|
||
/** | ||
* Job-handler for ticks before `firstTick` and `lastTick` | ||
*/ | ||
tick?: tickCallback; | ||
|
||
/** | ||
* Finish job | ||
*/ | ||
lastTick?: tickCallback; | ||
} | ||
|
||
/** | ||
* Execution: | ||
* | ||
* ```text | ||
* firstTick() -> ...tick() -> lastTick() | ||
* ``` | ||
* | ||
* `tick` is called `ticksCount` times | ||
*/ | ||
export default class Job { | ||
public static createJob(options: JobOptions) { | ||
assert('"options.ticksCount" must be greater than 0', options.ticksCount > 0); | ||
const job = new Job(); | ||
const cronInstance = Cron.getCron(); | ||
job.firstTick = options.firstTick || noop; | ||
job.tick = options.tick || noop; | ||
job.lastTick = options.lastTick || noop; | ||
job.internalId = options.id; | ||
job.internalJob = cron.schedule(options.frequency, () => { | ||
const currentTime = new Date().getTime(); | ||
const endTime = options.endTime ? options.endTime : Number.POSITIVE_INFINITY; | ||
if (currentTime > endTime) { | ||
return; | ||
} | ||
let result = null; | ||
if (cronInstance.has(job.id)) { | ||
result = job.firstTick.call(null); | ||
} | ||
for (let i = 0; i < options.ticksCount; i++) { | ||
setTimeout(() => { | ||
if (cronInstance.has(job.id)) { | ||
result = job.tick.call(null, result, i + 1); | ||
} | ||
}, 1000 * options.ticksDelay * (i + 1)); | ||
} | ||
setTimeout(() => { | ||
if (cronInstance.has(job.id)) { | ||
job.lastTick.call(null, result); | ||
} | ||
}, (options.ticksCount + 1) * options.ticksDelay * 1000); | ||
}, options.immediateStart); | ||
cronInstance.add(job.internalId, job.internalJob); | ||
return job; | ||
} | ||
|
||
private internalId: any; | ||
private firstTick: tickCallback; | ||
private tick: tickCallback; | ||
private lastTick: tickCallback; | ||
private internalJob: ScheduledTask; | ||
private constructor() {} | ||
|
||
get id() { | ||
return this.internalId; | ||
} | ||
|
||
public start() { | ||
Cron.getCron().start(this.id); | ||
} | ||
|
||
public stop() { | ||
Cron.getCron().stop(this.id); | ||
} | ||
|
||
public destroy() { | ||
Cron.getCron().destroy(this.id); | ||
} | ||
} |
Oops, something went wrong.