Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Add jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
onechiporenko committed Mar 22, 2018
1 parent f7d61b4 commit c9f0c5f
Show file tree
Hide file tree
Showing 7 changed files with 4,098 additions and 676 deletions.
52 changes: 52 additions & 0 deletions lib/cron.ts
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];
}
}
}
4 changes: 3 additions & 1 deletion lib/index.ts
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};
123 changes: 123 additions & 0 deletions lib/job.ts
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);
}
}

0 comments on commit c9f0c5f

Please sign in to comment.