/** * Pick the index identifier from the provided command line option. * @param string $option command line option * 'now' => current time * 'current' => if there is just one index for this type then use its identifier * other string => that string back * @param string $typeName * @return string index identifier to use */ public function pickIndexIdentifierFromOption($option, $typeName) { if ($option === 'now') { $identifier = strval(time()); $this->outputIndented("Setting index identifier...{$typeName}_{$identifier}\n"); return $identifier; } if ($option === 'current') { $this->outputIndented('Infering index identifier...'); $found = array(); foreach ($this->client->getStatus()->getIndexNames() as $name) { if (substr($name, 0, strlen($typeName)) === $typeName) { $found[] = $name; } } if (count($found) > 1) { $this->output("error\n"); $this->error("Looks like the index has more than one identifier. You should delete all\n" . "but the one of them currently active. Here is the list: " . implode($found, ','), 1); } if ($found) { $identifier = substr($found[0], strlen($typeName) + 1); if (!$identifier) { // This happens if there is an index named what the alias should be named. // If the script is run with --startOver it should nuke it. $identifier = 'first'; } } else { $identifier = 'first'; } $this->output("{$typeName}_{$identifier}\n"); return $identifier; } return $option; }
/** * До любых других действий */ public function initialize() { $currentActionName = $this->dispatcher->getActiveMethod(); $annotations = $this->annotations->getMethod(self::class, $currentActionName); if ($annotations->has('actionInfo')) { $annotation = $annotations->get('actionInfo'); $actionTitle = $annotation->getNamedArgument('name'); $this->log->info('Запустили: {actionTitle}', ['actionTitle' => $actionTitle]); } else { $currentTaskName = $this->dispatcher->getTaskName(); $this->log->info('Запустили: {currentTaskName}::{currentActionName}', ['currentTaskName' => $currentTaskName, 'currentActionName' => $currentActionName]); } $this->indexName = $this->dispatcher->getParam('index', 'string', false); $this->typeName = $this->dispatcher->getParam('type', 'string', false); if (!$this->indexName) { $this->log->error('Указание индекса является обязательным параметром'); die; } $this->sizePerShard = $this->dispatcher->getParam('sizePerShard', 'int', false) ?: $this->sizePerShard; $this->elasticsearchHost = $this->dispatcher->getParam('host', 'string', false) ?: $this->elasticsearchHost; $this->elasticsearchPort = $this->dispatcher->getParam('port', 'int', false) ?: $this->elasticsearchPort; $connectParams = ['host' => $this->elasticsearchHost, 'port' => $this->elasticsearchPort]; $this->client = new Client($connectParams); try { $this->client->getStatus(); } catch (\Elastica\Exception\Connection\HttpException $e) { $context = ['host' => $this->elasticsearchHost, 'port' => $this->elasticsearchPort]; $this->log->error('Подключение к серверу elasticsearch отсутствует: http://{host}:{port}', $context); die; } $this->elasticaIndex = $this->client->getIndex($this->indexName); $this->elasticaType = $this->elasticaIndex->getType($this->typeName); }
/** * @return Status */ public function validate() { // arrays of aliases to be added/removed $add = $remove = array(); $this->outputIndented("\tValidating {$this->aliasName} alias..."); $status = $this->client->getStatus(); if ($status->indexExists($this->aliasName)) { $this->output("is an index..."); if ($this->startOver) { $this->client->getIndex($this->aliasName)->delete(); $this->output("index removed..."); $add[] = $this->specificIndexName; } else { $this->output("cannot correct!\n"); return Status::newFatal(new RawMessage("There is currently an index with the name of the alias. Rerun this\n" . "script with --startOver and it'll remove the index and continue.\n")); } } else { foreach ($status->getIndicesWithAlias($this->aliasName) as $index) { if ($index->getName() === $this->specificIndexName) { $this->output("ok\n"); return Status::newGood(); } elseif ($this->shouldRemoveFromAlias($index->getName())) { $remove[] = $index->getName(); } } $add[] = $this->specificIndexName; } return $this->updateIndices($add, $remove); }
/** * @return void */ private function checkConnectToSearch() { try { $this->client->getStatus(); } catch (\Exception $e) { $this->addDysfunction(self::HEALTH_MESSAGE_UNABLE_TO_CONNECT_TO_SEARCH); $this->addDysfunction($e->getMessage()); } }
/** * @dataProvider getConfig */ public function testDynamicHttpMethodOnlyAffectsRequestsWithBody(array $config, $httpMethod) { $client = new Client($config); $status = $client->getStatus(); $info = $status->getResponse()->getTransferInfo(); $this->assertStringStartsWith('GET', $info['request_header']); }
/** * @test */ public function doGetStats() { $data = ['foo' => 'bar']; $status = $this->prophesize('\\Elastica\\Status'); $status->getServerStatus()->shouldBeCalled()->willReturn($data); $this->client->getStatus()->shouldBeCalled()->willReturn($status->reveal()); self::assertEquals($data, $this->cache->getStats()); }
/** * @expectedException \Elastica\Exception\ConnectionException */ public function testInvalidHostRequest() { $this->_checkPlugin(); $client = new Client(array('host' => 'unknown', 'port' => 9555, 'transport' => 'Thrift')); $client->getStatus(); }
/** * Retrieves cached information from the data store. * * @since 2.2 * * @return array|null An associative array with server's statistics if available, NULL otherwise. */ protected function doGetStats() { return $this->client->getStatus()->getServerStatus(); }