-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnections.js
More file actions
73 lines (58 loc) · 1.9 KB
/
connections.js
File metadata and controls
73 lines (58 loc) · 1.9 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
var fs = require('fs'),
AWS = require('aws-sdk'),
util = require('util'),
mongoose = require('mongoose'),
redis = require('redis'),
async = require('async'),
EventEmitter = require('events').EventEmitter;
var Connections = function() { //Emmiter for async operations
var self = this;
EventEmitter.call(self);
self.aws = AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});
self.s3 = new AWS.S3({endpoint: new AWS.Endpoint(process.env.AWS_S3_ENDPOINT)});
self.db = mongoose.createConnection(process.env.MONGODB_URL);
self.redisDB = redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST);
self.clientSub = redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST);
console.log('Establishing connections...');
async.parallel([
function(cb) {
self.db.once('open', function() {
console.log('Mongo db connected');
cb();
});
self.db.once('error', function(err) {
console.log(err);
});
},
function (cb) {
self.redisDB.once('connect', function () {
console.log('Redis store db connected');
cb();
});
self.redisDB.once('error', function (err) {
console.log(err);
});
},
function (cb) {
self.clientSub.once('connect', function () {
console.log('Redis pub/sub connected');
cb();
});
self.clientSub.once('error', function (err) {
console.log(err);
});
}
], function (err) {
if(err){
console.log('Unable to connect:', err);
return;
}
console.log('All connetions established');
self.emit('connected');
});
};
util.inherits(Connections, EventEmitter);
module.exports = new Connections();