-
Notifications
You must be signed in to change notification settings - Fork 7
/
redisClient.js
209 lines (191 loc) · 6.28 KB
/
redisClient.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict';
var
winston = require('winston'),
redis = require('redis'),
url = require('url');
module.exports = exports = function (core) {
var config,
redisConfigUrlParsed,
rrc;
if (core.config.redisUrl && core.config.redisUrl.port && core.config.redisUrl.host) {
config = {
'port': core.config.redisUrl.port || 6379,
'host': core.config.redisUrl.host || 'localhost',
'auth': core.config.redisUrl.auth
};
} else {
redisConfigUrlParsed = url.parse(core.config.redisUrl);
if (redisConfigUrlParsed && redisConfigUrlParsed.protocol === 'redis:') {
config = {
port: redisConfigUrlParsed.port,
host: redisConfigUrlParsed.hostname
};
if (redisConfigUrlParsed.auth && redisConfigUrlParsed.auth.split(':')[1]) {
config.auth = redisConfigUrlParsed.auth.split(':')[1];
}
} else {
config = {
'port': 6379,
'host': 'localhost',
'auth': null
};
}
}
/**
* @method Hunt#createRedisClient
* @description
* Creates redis client
* @see Hunt#redisClient
*/
core.createRedisClient = function () {
var
h = this,
r = redis.createClient(config.port, config.host, {
'auth_pass': config.auth,
//'detect_buffers': true,
'return_buffers': true
}), //https://github.com/Automattic/socket.io-redis/issues/17
/*jshint -W106 */
original = r.send_command;
r.send_command = function (command, args, callback) {
/*jshint +W106 */
var
startedAt = new Date(),
constructedArguments = [];
// console.log('redis.send_command',command, args, args[args.length-1],callback);
if (callback === undefined) {
if (typeof args[args.length - 1] === 'function') {
callback = args[args.length - 1];
args.pop();
} else {
callback = function (error) {
if (error) {
throw error;
}
};
}
}
/**
* Emitted on every request send to any of data storage drivers - redis, mongoose, sequilize...
* Namespace is `profiling:redis:*` with namespaces consisted of
* redis command names and arguments' values
*
* @see Hunt#on
* @see Hunt#onAny
* @see Hunt#once
* @event Hunt#profiling:*
* @type {object}
* @property {Date} startedAt
* @property {Date} finishedAt
* @property {Number} duration - duration of request in milliseconds
* @property {String} driver - database driver used, `redis` in this case
* @property {String} command
* @property {String} command
* @property {Error|null} error
* @property {string} result
* @tutorial profiling
* @tutorial events
* @example
*
* function listener(payload){
* console.log('We received event '+this.event+' with payload', payload);
* }
*
* //All this listeners will be fired on
* //redis command `set someKeyName someKeyValue`
*
* Hunt.on('profiling:*', listener);
* Hunt.on('profiling:redis:*', listener);
* Hunt.on('profiling:redis:set:*', listener);
* Hunt.on('profiling:redis:set:someKeyName:*', listener);
* Hunt.on('profiling:redis:set:someKeyName:someKeyValue', listener);
* Hunt.redisClient.set('someKeyName','someKeyValue');
*/
/**
* Emitted on every redis command.
* Namespace is `profiling:redis:*` with namespaces consisted of
* redis command names and arguments' values
*
* @see Hunt#on
* @see Hunt#onAny
* @see Hunt#once
* @event Hunt#profiling:redis:*
* @type {object}
* @property {Date} startedAt
* @property {Date} finishedAt
* @property {Number} duration - duration of request in milliseconds
* @property {String} driver - database driver used, `redis` in this case
* @property {String} command
* @property {String} command
* @property {Error|null} error
* @property {string} result
* @tutorial profiling
* @tutorial events
* @example
*
* function listener(payload){
* console.log('We recieved event '+this.event+' with payload', payload);
* }
*
* //All this listeners will be fired on
* //redis command `set someKeyName someKeyValue`
*
* Hunt.on('profiling:*', listener);
* Hunt.on('profiling:redis:*', listener);
* Hunt.on('profiling:redis:set:*', listener);
* Hunt.on('profiling:redis:set:someKeyName:*', listener);
* Hunt.on('profiling:redis:set:someKeyName:someKeyValue', listener);
* Hunt.redisClient.set('someKeyName','someKeyValue');
*/
function newCallback(error, result) {
var tree = ['profiling', 'redis', command],
finishedAt = new Date(),
payload = {
'startedAt': startedAt,
'finishedAt': finishedAt,
'duration': (finishedAt.getTime() - startedAt.getTime()),
'driver': 'redis',
'command': command,
'query': (command + ' ' + args.join(' ')),
'error': error,
'result': result ? result.toString() : null
};
args.map(function (a) {
tree.push(a);
});
h.huntEmit(tree, payload);
return callback(error, result);
}
constructedArguments.push(command);
constructedArguments.push(args);
constructedArguments.push(newCallback);
return original.apply(this, constructedArguments);
};
r.on('ready', function(){
winston.debug('Process %s established connection to redis://%s:%s',
process.pid, config.host, config.port
);
});
r.on('end', function(){
winston.debug('Process %s closed connection to redis://%s:%s',
process.pid, config.host, config.port
);
});
return r;
};
/**
* @name Hunt#redisClient
* @description
* Ready to use redis client with connection parameters
* taken from config object.
* {@link http://npmjs.org/package/redis | Additional info on redis client}
*/
Object.defineProperty(core, 'redisClient', {
get: function () {
if (!rrc) {
rrc = this.createRedisClient();
}
return rrc;
}
});
};