monitor_consumer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require __DIR__.'/shared.php';
  11. // This is a basic example on how to use the Predis\Monitor\Consumer class. You
  12. // can use redis-cli to send commands to the same Redis instance your client is
  13. // connected to, and then type "ECHO QUIT_MONITOR" in redis-cli when you want to
  14. // exit the monitor loop and terminate this script in a graceful way.
  15. // Create a client and disable r/w timeout on the socket.
  16. $client = new Predis\Client($single_server + array('read_write_timeout' => 0));
  17. // Use only one instance of DateTime, we will update the timestamp later.
  18. $timestamp = new DateTime();
  19. foreach (($monitor = $client->monitor()) as $event) {
  20. $timestamp->setTimestamp((int) $event->timestamp);
  21. // If we notice a ECHO command with the message QUIT_MONITOR, we stop the
  22. // monitor consumer and then break the loop.
  23. if ($event->command === 'ECHO' && $event->arguments === '"QUIT_MONITOR"') {
  24. echo 'Exiting the monitor loop...', PHP_EOL;
  25. $monitor->stop();
  26. break;
  27. }
  28. echo "* Received {$event->command} on DB {$event->database} at {$timestamp->format(DateTime::W3C)}", PHP_EOL;
  29. if (isset($event->arguments)) {
  30. echo " Arguments: {$event->arguments}", PHP_EOL;
  31. }
  32. }
  33. // Say goodbye :-)
  34. $version = redis_version($client->info());
  35. echo "Goodbye from Redis $version!", PHP_EOL;