-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (102 loc) · 3.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const EventEmitter = require('events');
class Scheduler extends EventEmitter {
constructor() {
super();
this.jobs = {};
this.schedules = {};
}
/**
* Create a new Schedulable job
*
* @param {string} name The name of your Job
* @param {function} executor The executor function, it must return a promise
* @param {number} concurrency The number of concurrent instances of the job that might be run
* @throws {Error} Throws an Error if the name is already taken
*/
createJob(name, executor, concurrency = 1) {
if (this.jobs[name]) {
throw new Error(`Name ${name} already taken`);
} else {
this.jobs[name] = {
current: 0,
concurrency: concurrency,
executor: executor,
}
}
}
/**
* Remove a Schedulable job from the Scheduler instance
*
* @param {string} name The name of your Job
* @throws {Error} Throws an Error if there is no Job registered by that name
*/
removeJob(name) {
if (!this.jobs[name]) {
throw new Error(`No job named ${name} found`);
} else {
delete this.jobs[name];
}
}
/**
* Trigger a Schedulable job
*
* @param {string} name The name of your Job
* @param {...*} args The arguments that will be passed to your Job executor
* @throws {Error} Throws an Error if there is no Job registered by that name
* @throws {Error} Throws an Error if the job is already at max concurrency
*/
startJob(name, ...args) {
if (!this.jobs[name]) {
throw new Error(`No job named ${name} found`);
}
let job = this.jobs[name];
if (job.current >= job.concurrency) {
throw new Error(`Job ${name} is at max concurrency level of ${job.concurrency}`);
}
job.current++;
let promise = job.executor(...args);
promise.then(() => { job.current-- }, () => { job.current-- });
this.emit(`running::${name}`, promise);
return promise;
}
/**
* Run your Job periodically
*
* @param {string} name The name of your Job
* @param {number} interval The interval between run attempts
* @param {...*} args The arguments that will be passed to your Job executor
*/
scheduleJob(name, interval, ...args) {
if (!this.jobs[name]) {
throw new Error(`No job named ${name} found`);
}
if (this.schedules[name]) {
throw new Error(`Job ${name} has already been scheduled at a ${this.schedules[name].interval} ms interval`);
}
if (!interval) {
throw new Error(`Param interval must be a number > 0`);
}
this.schedules[name] = {};
this.schedules[name].interval = interval;
this.schedules[name].intervalId = setInterval(() => {
try {
this.startJob(name, ...args);
} catch (error) {
this.emit('error', error);
}
}, interval);
}
/**
* Stop Job's periodic runs
*
* @param {string} name The name of your Job
*/
clearSchedule(name) {
if (!this.schedules[name]) {
throw new Error(`There is no current schedule for job ${name}`);
}
clearInterval(this.schedules[name].intervalId);
delete this.schedules[name];
}
}
module.exports = Scheduler;