connect() public method

Opens the underlying connection and connects to the server.
public connect ( )
Example #1
0
 /**
  * @param $sourceConfig connection URI string (tcp://10.0.0.1:6379)
  * @param $destinationConfig connection URI string (tcp://10.0.0.1:6379)
  * @throws Predis\Connection\ConnectionException
  */
 public function __construct($sourceConfig, $destinationConfig)
 {
     $this->source = new Client($sourceConfig);
     $this->destination = new Client($destinationConfig);
     $this->source->connect();
     $this->destination->connect();
 }
 public function fork()
 {
     $id = pcntl_fork();
     if (!$id) {
         $this->redis->disconnect();
         $this->redis->connect();
     }
     return $id;
 }
Example #3
0
 /**
  * Creates a connect to Redis or Sentinel using the Predis\Client object.  It proxies the connecting and converts
  * specific client exceptions to more generic adapted ones in PSRedis
  *
  * @throws \PSRedis\Exception\ConnectionError
  */
 public function connect()
 {
     try {
         $this->predisClient = $this->predisClientFactory->createClient($this->clientType, $this->getPredisClientParameters());
         $this->predisClient->connect();
         $this->isConnected = $this->predisClient->isConnected();
     } catch (ConnectionException $e) {
         throw new ConnectionError($e->getMessage());
     }
 }
Example #4
0
 public function init()
 {
     $settings = Config::get('ajumamoro:broker');
     unset($settings['driver']);
     $this->redis = new \Predis\Client($settings);
     try {
         $this->redis->connect();
     } catch (\Predis\CommunicationException $ex) {
         throw new BrokerConnectionException("Failed to connect to redis broker: {$ex->getMessage()}");
     }
 }
 /**
  * @return bool
  */
 public function checkConnection()
 {
     if (!$this->predis->isConnected()) {
         try {
             $this->predis->connect();
             return true;
         } catch (ConnectionException $e) {
             return false;
         }
     }
     return true;
 }
 function __construct($url)
 {
     try {
         list($user, $passwd, $host, $port, $db) = self::parseRedisUrl($url);
         $this->_redis = new Client(array('host' => $host, 'port' => $port));
     } catch (\Exception $e) {
         throw new RedisBundleException($e);
     }
     try {
         $this->_redis->connect();
         $this->_redis->select($db);
     } catch (\Predis\Connection\ConnectionException $e) {
         throw new RedisBundleException($e);
     }
 }
 /**
  * @group connected
  */
 public function testDispatcherLoopAgainstRedisServer()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => 2);
     $producer = new Client($parameters, REDIS_SERVER_VERSION);
     $producer->connect();
     $consumer = new Client($parameters, REDIS_SERVER_VERSION);
     $consumer->connect();
     $dispatcher = new DispatcherLoop($consumer);
     $function01 = $this->getMock('stdClass', array('__invoke'));
     $function01->expects($this->exactly(2))->method('__invoke')->with($this->logicalOr($this->equalTo('01:argument'), $this->equalTo('01:quit')))->will($this->returnCallback(function ($arg) use($dispatcher) {
         if ($arg === '01:quit') {
             $dispatcher->stop();
         }
     }));
     $function02 = $this->getMock('stdClass', array('__invoke'));
     $function02->expects($this->once())->method('__invoke')->with('02:argument');
     $function03 = $this->getMock('stdClass', array('__invoke'));
     $function03->expects($this->never())->method('__invoke');
     $dispatcher->attachCallback('function:01', $function01);
     $dispatcher->attachCallback('function:02', $function02);
     $dispatcher->attachCallback('function:03', $function03);
     $producer->publish('function:01', '01:argument');
     $producer->publish('function:02', '02:argument');
     $producer->publish('function:01', '01:quit');
     $dispatcher->run();
     $this->assertTrue($consumer->ping());
 }
 public function testHandleLogWithGoodMessageNotImplementingJobInterface()
 {
     $worker = new WorkerPresence();
     $worker->setMemory(12345);
     $frame = new Frame('MESSAGE', array('delivery_tag' => 'delivery-' . mt_rand()), $worker->toJson());
     $loop = LoopFactory::create();
     $options = array('eventloop' => $loop, 'on_error' => array($this, 'throwRedisError'));
     $redisSync = new PredisSync('tcp://127.0.0.1:6379');
     $redisSync->connect();
     $resolver = $this->getResolver();
     $resolver->expects($this->once())->method('ack');
     $done = false;
     $phpunit = $this;
     $redis = new PredisAsync('tcp://127.0.0.1:6379', $options);
     $redis->connect(function () use($resolver, $phpunit, $redis, $frame, $redisSync, &$done, $worker) {
         $component = new LogBuilderComponent();
         $component->handleLog($redis, $phpunit->getLogger(), $frame, $resolver)->then(function ($hashId) use($phpunit, $redis, $redisSync, &$done, $worker) {
             $redis->disconnect();
             $phpunit->assertEquals($worker->toJson(), $redisSync->get($hashId));
             $phpunit->assertTrue($redisSync->sismember('garbages', $hashId));
             $done = true;
         });
     });
     $loop->run();
     $this->assertTrue($done);
 }
 /**
  * Returns a new client instance.
  *
  * @return Client
  */
 protected function getClient()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'iterable_multibulk' => true, 'read_write_timeout' => 2);
     $client = new Client($parameters, REDIS_SERVER_VERSION);
     $client->connect();
     $client->select(REDIS_SERVER_DBNUM);
     $client->flushdb();
     return $client;
 }
Example #10
0
 /**
  * ### Tries to connect to the configured redis database
  *
  * RedisCache constructor.
  */
 public function __construct()
 {
     $client = new Client(['scheme' => 'tcp', 'host' => Config::get('cache', 'redis_server.host'), 'port' => Config::get('cache', 'redis_server.port'), 'database' => Config::get('cache', 'redis_server.database')]);
     try {
         $client->connect();
     } catch (\Exception $e) {
         throw new RedisException('Connection to redis server failed. Please check your settings.');
     }
     $this->conn = $client;
 }
 public static function setUpBeforeClass()
 {
     parent::setUpBeforeClass();
     $client = new Client();
     try {
         $client->connect();
         $client->disconnect();
         self::$supported = true;
     } catch (ConnectionException $e) {
         self::$supported = false;
     }
 }
Example #12
0
 public function setUp()
 {
     // connect to a redis daemon
     $predis = new Predis(array('host' => $_ENV['redis_host'], 'port' => $_ENV['redis_port']));
     // set it
     $this->storage = new Redis($predis, 'test_user_token', 'test_user_state');
     try {
         $predis->connect();
     } catch (\Predis\Connection\ConnectionException $e) {
         $this->markTestSkipped('No redis instance available: ' . $e->getMessage());
     }
 }
 public function setUp()
 {
     if (!class_exists('Predis\\Client')) {
         $this->markTestSkipped('The ' . __CLASS__ . ' requires Predis to be available');
     }
     try {
         $client = new Client();
         $client->connect();
         $client->flushdb();
     } catch (ConnectionException $e) {
         $this->markTestSkipped('The ' . __CLASS__ . ' requires the use of a Redis Server');
     }
     $this->storage = new RedisStorage($client);
 }
 /**
  * Returns a new client instance.
  *
  * @param Boolean $connect Flush selected database before returning the client.
  * @return Client
  */
 protected function getClient($flushdb = true)
 {
     $profile = $this->getProfile();
     if (!$profile->supportsCommand($id = $this->getExpectedId())) {
         $this->markTestSkipped("The profile {$profile->getVersion()} does not support command {$id}");
     }
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT);
     $options = array('profile' => $profile);
     $client = new Client($parameters, $options);
     $client->connect();
     $client->select(REDIS_SERVER_DBNUM);
     if ($flushdb) {
         $client->flushdb();
     }
     return $client;
 }
Example #15
0
 public function test_published_messages_fire_event_listeners_when_rechech_called()
 {
     $redis = new Client();
     $pubsub = new RedisPubSub($redis);
     try {
         $redis->connect();
     } catch (ConnectionException $e) {
         $this->markTestSkipped("Test skipped because no Redis server is running on default ports");
     }
     $value = null;
     $pubsub->on('foo', function ($data) use(&$value) {
         $value = $data;
     });
     $pubsub->publish('foo', 'bar');
     $pubsub->recheck('foo');
     $this->assertEquals('bar', $value);
 }
 /**
  * @group connected
  */
 public function testDispatcherLoopAgainstRedisServerWithPrefix()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => 2);
     $options = array('profile' => REDIS_SERVER_VERSION);
     $producerNonPfx = new Client($parameters, $options);
     $producerNonPfx->connect();
     $producerPfx = new Client($parameters, $options + array('prefix' => 'foobar'));
     $producerPfx->connect();
     $consumer = new Client($parameters, $options + array('prefix' => 'foobar'));
     $dispatcher = new DispatcherLoop($consumer);
     $callback = $this->getMock('stdClass', array('__invoke'));
     $callback->expects($this->exactly(1))->method('__invoke')->with($this->equalTo('arg:prefixed'))->will($this->returnCallback(function ($arg) use($dispatcher) {
         $dispatcher->stop();
     }));
     $dispatcher->attachCallback('callback', $callback);
     $producerNonPfx->publish('callback', 'arg:non-prefixed');
     $producerPfx->publish('callback', 'arg:prefixed');
     $dispatcher->run();
     $this->assertTrue($consumer->ping());
 }
 /**
  * Returns a new client instance.
  *
  * @return Client
  */
 protected function getClient()
 {
     $parameters = $this->getParameters(array('read_write_timeout' => 2));
     $protocol = new TextProtocolProcessor();
     $protocol->useIterableMultibulk(true);
     $connection = new CompositeStreamConnection($parameters, $protocol);
     $client = new Client($connection);
     $client->connect();
     $client->flushdb();
     return $client;
 }
Example #18
0
 public function connect()
 {
     $this->_client->connect();
 }
Example #19
0
 /**
  * @inheritdoc
  */
 public function connect($connectionTimeout = null, $responseTimeout = null)
 {
     parent::connect($connectionTimeout, $responseTimeout);
     $this->client = $this->buildClient($this->host, $this->port);
     $this->client->connect();
 }
 /**
  * @group connected
  * @requires extension pcntl
  */
 public function testPubSubAgainstRedisServerBlocking()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => -1);
     $options = array('profile' => REDIS_SERVER_VERSION);
     // create consumer before forking so the child can disconnect it
     $consumer = new Client($parameters, $options);
     $consumer->connect();
     /*
      * fork
      *  parent: consumer
      *  child: producer
      */
     if ($childPID = pcntl_fork()) {
         $messages = array();
         $pubsub = new PubSubConsumer($consumer);
         $pubsub->subscribe('channel:foo');
         foreach ($pubsub as $message) {
             if ($message->kind !== 'message') {
                 continue;
             }
             $messages[] = $payload = $message->payload;
             if ($payload === 'QUIT') {
                 $pubsub->stop();
             }
         }
         $this->assertSame(array('message1', 'message2', 'QUIT'), $messages);
         $this->assertFalse($pubsub->valid());
         $this->assertEquals('ECHO', $consumer->echo('ECHO'));
         // kill the child
         posix_kill($childPID, SIGKILL);
     } else {
         // create producer, read_write_timeout = 2 because it doesn't do blocking reads anyway
         $producer = new Client(array_replace($parameters, array('read_write_timeout' => 2)), $options);
         $producer->connect();
         $producer->publish('channel:foo', 'message1');
         $producer->publish('channel:foo', 'message2');
         $producer->publish('channel:foo', 'QUIT');
         // sleep, giving the consumer a chance to respond to the QUIT message
         sleep(1);
         // disconnect the consumer because otherwise it could remain stuck in blocking read
         //  if it failed to respond to the QUIT message
         $consumer->disconnect();
         // exit child
         exit(0);
     }
 }
Example #21
0
 /**
  * Connect
  *
  * @param array $options Connection options
  */
 public function connect(array $options = [])
 {
     parent::connect($options);
     $this->client = $this->buildClient($this->host, $this->port);
     $this->client->connect();
 }
 /**
  * @group connected
  */
 public function testPubSubAgainstRedisServer()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => 2);
     $options = array('profile' => REDIS_SERVER_VERSION);
     $messages = array();
     $producer = new Client($parameters, $options);
     $producer->connect();
     $consumer = new Client($parameters, $options);
     $consumer->connect();
     $pubsub = new PubSubContext($consumer);
     $pubsub->subscribe('channel:foo');
     $producer->publish('channel:foo', 'message1');
     $producer->publish('channel:foo', 'message2');
     $producer->publish('channel:foo', 'QUIT');
     foreach ($pubsub as $message) {
         if ($message->kind !== 'message') {
             continue;
         }
         $messages[] = $payload = $message->payload;
         if ($payload === 'QUIT') {
             $pubsub->closeContext();
         }
     }
     $this->assertSame(array('message1', 'message2', 'QUIT'), $messages);
     $this->assertFalse($pubsub->valid());
     $this->assertTrue($consumer->ping());
 }
Example #23
0
 /**
  * Redis constructor.
  */
 public function __construct()
 {
     $connParams = Core\Config()->CACHE['redis'];
     $this->redisClient = new Predis\Client($connParams);
     $this->redisClient->connect();
 }
 /**
  * Connects to client
  */
 public function connect()
 {
     $this->client = $this->clientFactory->createClient($this->getClientParameters());
     $this->client->connect();
 }
Example #25
0
 private function testRedisConnection(\Predis\Client $redis)
 {
     try {
         $redis->connect();
     } catch (\Predis\CommunicationException $exception) {
         // we were unable to connect to the redis server
         return false;
     }
     return true;
 }
 /**
  * Connects to client
  */
 public function connect()
 {
     $this->client = new \Redis();
     $this->isConnected = $this->client->connect($this->getHost(), $this->getPort());
 }
Example #27
0
 /**
  * @group disconnected
  */
 public function testConnectAndDisconnect()
 {
     $connection = $this->getMock('Predis\\Connection\\ConnectionInterface');
     $connection->expects($this->once())->method('connect');
     $connection->expects($this->once())->method('disconnect');
     $client = new Client($connection);
     $client->connect();
     $client->disconnect();
 }
 /**
  * Returns a new client instance.
  *
  * @param  array  $parameters Additional connection parameters.
  * @param  array  $options    Additional client options.
  * @param  bool   $flushdb    Flush selected database before returning the client.
  * @return Client
  */
 protected function createClient(array $parameters = null, array $options = null, $flushdb = true)
 {
     $parameters = array_merge($this->getDefaultParametersArray(), $parameters ?: array());
     $options = array_merge(array('profile' => $this->getProfile()), $options ?: array());
     $client = new Client($parameters, $options);
     $client->connect();
     if ($flushdb) {
         $client->flushdb();
     }
     return $client;
 }
 /**
  * @group connected
  */
 public function testMonitorAgainstRedisServer()
 {
     $parameters = array('host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM, 'read_write_timeout' => 2);
     $options = array('profile' => REDIS_SERVER_VERSION);
     $echoed = array();
     $producer = new Client($parameters, $options);
     $producer->connect();
     $consumer = new Client($parameters, $options);
     $consumer->connect();
     $monitor = new MonitorContext($consumer);
     $producer->echo('message1');
     $producer->echo('message2');
     $producer->echo('QUIT');
     foreach ($monitor as $message) {
         if ($message->command == 'ECHO') {
             $echoed[] = $arguments = trim($message->arguments, '"');
             if ($arguments == 'QUIT') {
                 $monitor->closeContext();
             }
         }
     }
     $this->assertSame(array('message1', 'message2', 'QUIT'), $echoed);
     $this->assertFalse($monitor->valid());
     $this->assertTrue($consumer->ping());
 }
Example #30
0
 /**
  * Returns a client instance connected to the specified Redis
  * server instance to perform integration tests.
  *
  * @param array Additional connection parameters.
  * @param array Additional client options.
  * @return Client client instance.
  */
 protected function getClient(array $parameters = array(), array $options = array())
 {
     $parameters = array_merge(array('scheme' => 'tcp', 'host' => REDIS_SERVER_HOST, 'port' => REDIS_SERVER_PORT, 'database' => REDIS_SERVER_DBNUM), $parameters);
     $options = array_merge(array('profile' => REDIS_SERVER_VERSION), $options);
     $client = new Client($parameters, $options);
     $client->connect();
     $client->flushdb();
     return $client;
 }