Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions Client/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,40 @@ public function addWarmer(WarmerInterface $warmer)

/**
* Loads warmers into elasticseach.
*
* @param array $names Warmers names to put.
*/
public function putWarmers()
public function putWarmers(array $names = [])
{
foreach ($this->warmers->getWarmers() as $name => $body) {
$this->getClient()->indices()->putWarmer(
[
'index' => $this->getIndexName(),
'name' => $name,
'body' => $body,
]
);
if (empty($names) || in_array($name, $names)) {
$this->getClient()->indices()->putWarmer(
[
'index' => $this->getIndexName(),
'name' => $name,
'body' => $body,
]
);
}
}
}

/**
* Deletes warmers from elasticsearch index.
*
* @param array $names Warmers names to delete.
*/
public function deleteWarmers(array $names = [])
{
foreach ($this->warmers->getWarmers() as $name => $body) {
if (empty($names) || in_array($name, $names)) {
$this->getClient()->indices()->deleteWarmer(
[
'index' => $this->getIndexName(),
'name' => $name,
]
);
}
}
}
}
37 changes: 34 additions & 3 deletions Command/AbstractElasticsearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,41 @@ protected function configure()
*/
protected function getManager($name)
{
/** @var Manager $manager */
$manager = $this->getContainer()->get($this->getManagerId($name));
return $this->getContainer()->get($this->getManagerId($name));
}

return $manager;
/**
* Returns elasticsearch connection by name.
*
* @param string $name
*
* @return \ONGR\ElasticsearchBundle\Client\Connection
*/
protected function getConnection($name)
{
return $this->getManager($this->getManagerNameByConnection($name))->getConnection();
}

/**
* Returns manager name which is using passed connection.
*
* @param string $name Connection name.
*
* @return string
*
* @throws \RuntimeException
*/
private function getManagerNameByConnection($name)
{
foreach ($this->getContainer()->getParameter('es.managers') as $managerName => $params) {
if ($params['connection'] === $name) {
return $managerName;
}
}

throw new \RuntimeException(
sprintf('Connection named %s is not used by any manager. Check your configuration.', $name)
);
}

/**
Expand Down
54 changes: 54 additions & 0 deletions Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Symfony command for clearing elasticsearch cache.
*/
class CacheClearCommand extends AbstractElasticsearchCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('es:cache:clear')
->addOption(
'connection',
'c',
InputOption::VALUE_REQUIRED,
'Connection name to which clear cache.',
'default'
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getConnection($input->getOption('connection'))->clearCache();
$output->writeln(
sprintf(
'Elasticsearch cache has been cleared for <info>%s</info> index.',
$input->getOption('connection')
)
);

return 0;
}
}
69 changes: 69 additions & 0 deletions Command/WarmerDeleteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* This command removes warmers from elasticsearch index.
*/
class WarmerDeleteCommand extends AbstractElasticsearchCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('es:warmer:delete')
->addArgument(
'names',
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
'Warmers names to delete from index.',
[]
)
->addOption(
'connection',
'c',
InputOption::VALUE_REQUIRED,
'Connection name to delete warmers from.',
'default'
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$names = $input->getArgument('names');
$this->getConnection($input->getOption('connection'))->deleteWarmers($names);

$message = '';
if (empty($names)) {
$message = 'All warmers have been deleted from <info>%s</info> index.';
} else {
$callback = function ($val) {
return '<info>' . $val . '</info>';
};
$message = implode(', ', array_map($callback, $names))
. ' warmer(s) have been deleted from <info>%s</info> index.';
}

$output->writeln(sprintf($message, $input->getOption('connection')));

return 0;
}
}
69 changes: 69 additions & 0 deletions Command/WarmerPutCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* This command puts warmers into elasticsearch index.
*/
class WarmerPutCommand extends AbstractElasticsearchCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('es:warmer:put')
->addArgument(
'names',
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
'Warmers names to put into index.',
[]
)
->addOption(
'connection',
'c',
InputOption::VALUE_REQUIRED,
'Connection name to put warmers to.',
'default'
);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$names = $input->getArgument('names');
$this->getConnection($input->getOption('connection'))->putWarmers($names);

$message = '';
if (empty($names)) {
$message = 'All warmers have been put into <info>%s</info> index.';
} else {
$callback = function ($val) {
return '<info>' . $val . '</info>';
};
$message = implode(', ', array_map($callback, $names))
. ' warmer(s) have been put into <info>%s</info> index.';
}

$output->writeln(sprintf($message, $input->getOption('connection')));

return 0;
}
}
71 changes: 71 additions & 0 deletions Tests/Functional/Command/CacheClearCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Tests\Functional\Command;

use ONGR\ElasticsearchBundle\Command\CacheClearCommand;
use ONGR\ElasticsearchBundle\Test\ElasticsearchTestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class CacheClearCommandTest extends ElasticsearchTestCase
{
/**
* Tests if command is being executed.
*/
public function testExecute()
{
$app = new Application();
$app->add($this->getCommand());
$command = $app->find('es:cache:clear');
$tester = new CommandTester($command);
$tester->execute(
[
'command' => $command->getName(),
]
);

$this->assertContains('Elasticsearch cache has been cleared for default index.', $tester->getDisplay());
$this->assertEquals(0, $tester->getStatusCode(), 'Status code should be zero.');
}

/**
* Tests if exception is thown when no connection is found.
*
* @expectedException \RuntimeException
*/
public function testExecuteException()
{
$app = new Application();
$app->add($this->getCommand());
$command = $app->find('es:cache:clear');
$tester = new CommandTester($command);
$tester->execute(
[
'command' => $command->getName(),
'--connection' => 'foo',
]
);
}

/**
* Returns cache clear command instance.
*
* @return CacheClearCommand
*/
private function getCommand()
{
$command = new CacheClearCommand();
$command->setContainer($this->getContainer());

return $command;
}
}
Loading