Skip to content

Commit d574654

Browse files
committed
Skeleton for MONITOR command, forward "monitor" events
1 parent 3ec3c01 commit d574654

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

examples/monitor.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Clue\React\Redis\Client;
4+
use Clue\React\Redis\Factory;
5+
use Clue\Redis\Protocol\Model\StatusReply;
6+
7+
require __DIR__ . '/../vendor/autoload.php';
8+
9+
$loop = React\EventLoop\Factory::create();
10+
$factory = new Factory($loop);
11+
12+
$factory->createClient()->then(function (Client $client) {
13+
$client->monitor()->then(function ($result) {
14+
echo 'Now monitoring all commands' . PHP_EOL;
15+
});
16+
17+
$client->on('monitor', function (StatusReply $message) {
18+
echo 'Monitored: ' . $message->getValueNative() . PHP_EOL;
19+
});
20+
21+
$client->echo('initial echo');
22+
});
23+
24+
$loop->run();

src/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*
1212
* @event data(ModelInterface $messageModel, Client $thisClient)
1313
* @event close()
14+
*
15+
* @event monitor(ModelInterface $statusModel)
1416
*/
1517
interface Client extends EventEmitterInterface
1618
{

src/StreamingClient.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use React\Promise\Deferred;
1515
use Clue\Redis\Protocol\Model\ErrorReply;
1616
use Clue\Redis\Protocol\Model\ModelInterface;
17+
use Clue\Redis\Protocol\Model\StatusReply;
1718

1819
class StreamingClient extends EventEmitter implements Client
1920
{
@@ -24,6 +25,8 @@ class StreamingClient extends EventEmitter implements Client
2425
private $ending = false;
2526
private $closed = false;
2627

28+
private $monitoring = false;
29+
2730
public function __construct(Stream $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
2831
{
2932
if ($parser === null || $serializer === null) {
@@ -78,13 +81,25 @@ public function __call($name, $args)
7881
$this->requests []= $request;
7982
}
8083

84+
if (strtolower($name) === 'monitor') {
85+
$monitoring =& $this->monitoring;
86+
$request->then(function () use (&$monitoring) {
87+
$monitoring = true;
88+
});
89+
}
90+
8191
return $request->promise();
8292
}
8393

8494
public function handleMessage(ModelInterface $message)
8595
{
8696
$this->emit('data', array($message, $this));
8797

98+
if ($this->monitoring && $message instanceof StatusReply) {
99+
$this->emit('monitor', array($message, $this));
100+
return;
101+
}
102+
88103
if (!$this->requests) {
89104
throw new UnderflowException('Unexpected reply received, no matching request found');
90105
}

0 commit comments

Comments
 (0)