-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (40 loc) · 1.63 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
const { inject } = require('deep-spread');
const schedule = require('./schedule');
const clientNames = ['mongodb', 'redis'];
/**
* Initializate Persistent-Scheduler:
* @param {object} Config - Configuration for Persistent-Scheduler.
* @param {object} Config.redis - Redis configuration.
* @param {String} Config.redis.url - URL for Redis connection.
* @param {Boolean} [Config.redis.helloMessage=true] - true for showing a message when connected to Redis.
* @param {object} Config.mongodb - MongoDB configuration.
* @param {String} Config.mongodb.url - URL for MongoDB connection.
* @param {String} [Config.mongodb.collection="SchedulerJobs"] - Name of the collection for storing functions.
* @param {Boolean} [Config.mongodb.helloMessage=true] - true for showing a message when connected to MongoDB.
*/
module.exports = (Config = {}) => {
let config = {
redis: { helloMessage: true },
mongodb: { helloMessage: true, collection: 'SchedulerJobs' }
};
config = inject(Config).to(config);
const clientPromises = [];
let foundUrl = false;
for (let k = 0; k < clientNames.length; k += 1) {
if (config[clientNames[k]].url) {
foundUrl = true;
clientPromises[k] = require(`./clients/${clientNames[k]}`)(config[clientNames[k]]);
}
}
if (!foundUrl) {
console.error('Error: please specify a connection url for mongodb or redis!');
return {};
}
const setClients = resolvedPromises => {
const clients = {};
for (let k = 0; k < clientNames.length; k += 1)
resolvedPromises[k] && (clients[clientNames[k]] = resolvedPromises[k]);
return clients;
};
return schedule(clientPromises, setClients);
};