You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Add MONITOR
command and special monitor command reply parsing.
This commit is contained in:
23
README.md
23
README.md
@@ -394,6 +394,29 @@ of commands and arguments to the constructor:
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
## Monitor mode
|
||||||
|
|
||||||
|
Redis supports the `MONITOR` command, which lets you see all commands received by the Redis server
|
||||||
|
across all client connections, including from other client libraries and other computers.
|
||||||
|
|
||||||
|
After you send the `MONITOR` command, no other commands are valid on that connection. `node_redis`
|
||||||
|
will emit a `monitor` event for every new monitor message that comes across. The callback for the
|
||||||
|
`monitor` event takes a timestamp from the Redis server and an array of command arguments.
|
||||||
|
|
||||||
|
Here is a simple example:
|
||||||
|
|
||||||
|
var client = require("redis").createClient(),
|
||||||
|
util = require("util");
|
||||||
|
|
||||||
|
client.monitor(function (err, res) {
|
||||||
|
console.log("Entering monitoring mode.");
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on("monitor", function (time, args) {
|
||||||
|
console.log(time + ": " + util.inspect(args));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
# Extras
|
# Extras
|
||||||
|
|
||||||
Some other things you might like to know about.
|
Some other things you might like to know about.
|
||||||
|
@@ -1,6 +1,10 @@
|
|||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
## v0.5.8 - March 14, 2011
|
||||||
|
|
||||||
|
Add `MONITOR` command and special monitor command reply parsing.
|
||||||
|
|
||||||
## v0.5.7 - February 27, 2011
|
## v0.5.7 - February 27, 2011
|
||||||
|
|
||||||
Add magical auth command.
|
Add magical auth command.
|
||||||
|
23
index.js
23
index.js
@@ -48,6 +48,7 @@ function RedisClient(stream, options) {
|
|||||||
this.retry_delay = 250;
|
this.retry_delay = 250;
|
||||||
this.retry_backoff = 1.7;
|
this.retry_backoff = 1.7;
|
||||||
this.subscriptions = false;
|
this.subscriptions = false;
|
||||||
|
this.monitoring = false;
|
||||||
this.closing = false;
|
this.closing = false;
|
||||||
this.server_info = {};
|
this.server_info = {};
|
||||||
this.auth_pass = null;
|
this.auth_pass = null;
|
||||||
@@ -279,6 +280,7 @@ RedisClient.prototype.connection_gone = function (why) {
|
|||||||
self.connected = false;
|
self.connected = false;
|
||||||
self.ready = false;
|
self.ready = false;
|
||||||
self.subscriptions = false;
|
self.subscriptions = false;
|
||||||
|
self.monitoring = false;
|
||||||
|
|
||||||
// since we are collapsing end and close, users don't expect to be called twice
|
// since we are collapsing end and close, users don't expect to be called twice
|
||||||
if (! self.emitted_end) {
|
if (! self.emitted_end) {
|
||||||
@@ -361,7 +363,7 @@ RedisClient.prototype.return_error = function (err) {
|
|||||||
|
|
||||||
RedisClient.prototype.return_reply = function (reply) {
|
RedisClient.prototype.return_reply = function (reply) {
|
||||||
var command_obj = this.command_queue.shift(),
|
var command_obj = this.command_queue.shift(),
|
||||||
obj, i, len, key, val, type;
|
obj, i, len, key, val, type, timestamp, args;
|
||||||
|
|
||||||
if (this.subscriptions === false && this.command_queue.length === 0) {
|
if (this.subscriptions === false && this.command_queue.length === 0) {
|
||||||
this.emit("idle");
|
this.emit("idle");
|
||||||
@@ -414,6 +416,13 @@ RedisClient.prototype.return_reply = function (reply) {
|
|||||||
} else {
|
} else {
|
||||||
throw new Error("subscriptions are active but got an invalid reply: " + reply);
|
throw new Error("subscriptions are active but got an invalid reply: " + reply);
|
||||||
}
|
}
|
||||||
|
} else if (this.monitoring) {
|
||||||
|
len = reply.indexOf(" ");
|
||||||
|
timestamp = reply.slice(0, len);
|
||||||
|
args = reply.slice(len + 1).match(/"[^"]+"/g).map(function (elem) {
|
||||||
|
return elem.replace(/"/g, "");
|
||||||
|
});
|
||||||
|
this.emit("monitor", timestamp, args);
|
||||||
} else {
|
} else {
|
||||||
throw new Error("node_redis command queue state error. If you can reproduce this, please report it.");
|
throw new Error("node_redis command queue state error. If you can reproduce this, please report it.");
|
||||||
}
|
}
|
||||||
@@ -469,12 +478,12 @@ RedisClient.prototype.send_command = function () {
|
|||||||
}
|
}
|
||||||
command_obj.sub_command = true;
|
command_obj.sub_command = true;
|
||||||
this.subscriptions = true;
|
this.subscriptions = true;
|
||||||
} else {
|
} else if (command === "monitor") {
|
||||||
if (command === "quit") {
|
this.monitoring = true;
|
||||||
this.closing = true;
|
} else if (command === "quit") {
|
||||||
} else if (this.subscriptions === true) {
|
this.closing = true;
|
||||||
throw new Error("Connection in pub/sub mode, only pub/sub commands may be used");
|
} else if (this.subscriptions === true) {
|
||||||
}
|
throw new Error("Connection in pub/sub mode, only pub/sub commands may be used");
|
||||||
}
|
}
|
||||||
this.command_queue.push(command_obj);
|
this.command_queue.push(command_obj);
|
||||||
this.commands_sent += 1;
|
this.commands_sent += 1;
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
{ "name" : "redis",
|
{ "name" : "redis",
|
||||||
"version" : "0.5.7",
|
"version" : "0.5.8",
|
||||||
"description" : "Redis client library",
|
"description" : "Redis client library",
|
||||||
"author": "Matt Ranney <mjr@ranney.com>",
|
"author": "Matt Ranney <mjr@ranney.com>",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
|
5
test.js
5
test.js
@@ -1068,7 +1068,8 @@ client.on('end', function () {
|
|||||||
ended = true;
|
ended = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO - need a better way to test auth, maybe auto-config a local Redis server?
|
// TODO - need a better way to test auth, maybe auto-config a local Redis server? Sounds hard.
|
||||||
|
// Yes, this is the real password. Please be nice, thanks.
|
||||||
client4.auth("664b1b6aaf134e1ec281945a8de702a9", function (err, res) {
|
client4.auth("664b1b6aaf134e1ec281945a8de702a9", function (err, res) {
|
||||||
if (err) {
|
if (err) {
|
||||||
assert.fail(err, name);
|
assert.fail(err, name);
|
||||||
@@ -1091,7 +1092,7 @@ client3.on("error", function (err) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
client.on("reconnecting", function (params) {
|
client.on("reconnecting", function (params) {
|
||||||
console.log("reconnecting: " + util.inspect(params));
|
// console.log("reconnecting: " + util.inspect(params));
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('uncaughtException', function (err) {
|
process.on('uncaughtException', function (err) {
|
||||||
|
Reference in New Issue
Block a user