Пример #1
0
 /**
  * Get hosts
  *
  * This method is only useful with a connection to a replica set. It returns the status of all of the hosts in the
  * set. Without a replica set, it will just return an array with one element containing the host that you are
  * connected to.
  *
  * @return array
  */
 public function getHosts()
 {
     $this->forceConnect();
     $results = [];
     try {
         $servers = $this->manager->getServers();
     } catch (\MongoDB\Driver\Exception\Exception $e) {
         throw ExceptionConverter::toLegacy($e);
     }
     foreach ($servers as $server) {
         $key = sprintf('%s:%d;-;.;%d', $server->getHost(), $server->getPort(), getmypid());
         $info = $server->getInfo();
         switch ($server->getType()) {
             case \MongoDB\Driver\Server::TYPE_RS_PRIMARY:
                 $state = 1;
                 break;
             case \MongoDB\Driver\Server::TYPE_RS_SECONDARY:
                 $state = 2;
                 break;
             default:
                 $state = 0;
         }
         $results[$key] = ['host' => $server->getHost(), 'port' => $server->getPort(), 'health' => (int) $info['ok'], 'state' => $state, 'ping' => $server->getLatency(), 'lastPing' => null];
     }
     return $results;
 }
Пример #2
0
 /**
  * Get hosts
  *
  * This method is only useful with a connection to a replica set. It returns the status of all of the hosts in the
  * set. Without a replica set, it will just return an array with one element containing the host that you are
  * connected to.
  *
  * @return array
  */
 public function getHosts()
 {
     $this->forceConnect();
     $servers = [];
     foreach ($this->manager->getServers() as $server) {
         $key = sprintf('%s:%d', $server->getHost(), $server->getPort());
         $info = $server->getInfo();
         switch ($server->getType()) {
             case \MongoDB\Driver\Server::TYPE_RS_PRIMARY:
                 $state = 1;
                 break;
             case \MongoDB\Driver\Server::TYPE_RS_SECONDARY:
                 $state = 2;
                 break;
             default:
                 $state = 0;
         }
         $servers[$key] = ['host' => $server->getHost(), 'port' => $server->getPort(), 'health' => (int) $info['ok'], 'state' => $state, 'ping' => $server->getLatency(), 'lastPing' => null];
     }
     return $servers;
 }