-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (131 loc) · 3.75 KB
/
Copy pathindex.js
File metadata and controls
143 lines (131 loc) · 3.75 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import EventEmitter from 'events';
import { CronJob } from 'cron';
import FunctionsMap from './functions';
/**
* @module Scheduler
*/
/**
* The Scheduler class
* @class
*/
class Scheduler extends EventEmitter {
/**
* Create an instance of Scheduler class
* @param {Object} configOptions - the global config
* @param {String} configOptions.scheduler.collection - the name of the collection to store jobs
* @param {Object[]} configOptions.scheduler.jobs - the jobs array. see [documentation]{@link https://www.npmjs.com/package/cron}
*/
constructor(configOptions) {
super();
this.store = configOptions.$store;
this.logger = configOptions.$logger;
this.collection = AppImport('.util')
.lastValue(configOptions, 'scheduler', 'collection') || 'cron';
let jobsMap = AppImport('.util').lastValue(configOptions, 'scheduler', 'jobs');
if (typeof jobsMap !== 'object' || jobsMap === null || Array.isArray(jobsMap)) {
jobsMap = {};
}
this.jobsMap = jobsMap;
this.jobsInstance = {};
}
/**
* Call one cron
* @param {Object} cron - the cron object
*/
async callOneCron(cron) {
await this.store.write(this.collection, cron._id, { status: 2 });
try {
await FunctionsMap[cron.name](this.store, this.logger, ...cron.params);
} catch (er) {
return this.store.write(this.collection, cron._id, { status: 0, error: er });
}
return this.store.write(this.collection, cron._id, { status: 1 });
}
/**
* To be called when the cron completes
* @param {String} name - the job name to complete with
*/
async completeCron(name) {
this.logger.info(`Job ${name} completed at ${new Date()}.`);
}
/**
* Call the crons that are in queue or yet to be executed
* @param {String} name - the job name to run with
*/
async callCron(name) {
const docs = await this.store.listAll(this.collection, {
name,
enabled: true,
status: { $lt: 1 }, // to execute failed or the ones in queue
}, { params: 1 });
await Promise.all(docs.map(this.callOneCron.bind(this)));
}
/**
* Init the scheduler to assign a collection
* @chainable
*/
init() {
this.store.mkcoll(this.collection);
Object.keys(this.jobsMap).forEach((jb) => {
if (typeof FunctionsMap[jb] === 'function') {
this.jobsInstance[jb] = new CronJob(Object.assign({}, this.jobsMap[jb], {
onTick: this.callCron.bind(this, jb),
onComplete: this.onComplete.bind(this, jb),
start: true,
}));
} else {
delete this.jobsMap[jb];
}
});
return this;
}
/**
* Enable the scheduler
* @param {String} jobName - the job to start
* @chainable
*/
enable(jobName) {
this.jobsInstance[jobName].start();
return this;
}
/**
* Disable the scheduler
* @param {String} jobName - the job to start
* @chainable
*/
disable(jobName) {
this.jobsInstance[jobName].stop();
return this;
}
/**
* Enable a job
* @param {String} jobId - the job id to enable
* @chainable
*/
enableJob(jobId) {
return this.store.write(this.collection, jobId, { enabled: true });
}
/**
* Disable a job
* @param {String} jobId - the job id to disable
* @chainable
*/
disableJob(jobId) {
return this.store.write(this.collection, jobId, { enabled: false });
}
/**
* create a job to schedule
* @param {String} name - the key name of the subscribers
* @param {*} params - the array of parameters to pass on tick.
*/
add(name, params) {
if (!this.jobsMap[name]) throw new Error('Incorrect job name.');
this.store.write(this.collection, null, {
name,
params,
enabled: true,
status: -1, // 0 means, to be executed
});
}
}
export default Scheduler;