-
Notifications
You must be signed in to change notification settings - Fork 0
"Clear" and "Swap" commands #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace IntegerNet\Solr\Console\Command; | ||
|
||
use IntegerNet\Solr\Indexer\Slice; | ||
use IntegerNet\Solr\Model\Indexer; | ||
use Magento\Framework\App; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* solr:clear command | ||
*/ | ||
class ClearCommand extends Command | ||
{ | ||
const INPUT_STORES = 'stores'; | ||
const INPUT_USESWAPCORE = 'useswapcore'; | ||
/** | ||
* @var Indexer\Console | ||
*/ | ||
private $indexer; | ||
/** | ||
* @var App\State | ||
*/ | ||
private $appState; | ||
|
||
public function __construct(Indexer\Console $indexer, App\State $appState, $name = null) | ||
{ | ||
parent::__construct($name); | ||
$this->indexer = $indexer; | ||
$this->appState = $appState; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$options = [ | ||
new InputOption( | ||
self::INPUT_STORES, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Clear solr product index for given stores (can be store id, store code, comma seperated. Or "all".) ' | ||
. 'If not set, clear all stores.' | ||
), | ||
new InputOption( | ||
self::INPUT_USESWAPCORE, | ||
null, | ||
InputOption::VALUE_NONE, | ||
'Use swap core for clearing instead of live solr core (only if configured correctly).' | ||
), | ||
]; | ||
$this->setName('solr:clear'); | ||
$this->setHelp('Clear Solr index for given stores (see "stores" param).'); | ||
$this->setDescription('Clear Solr index'); | ||
$this->setDefinition($options); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$styledOutput = new StyledOutput( | ||
$output, | ||
class_exists(SymfonyStyle::class) ? new SymfonyStyle($input, $output) : null | ||
); | ||
$startTime = microtime(true); | ||
$this->appState->setAreaCode(App\Area::AREA_GLOBAL); | ||
if (!$input->getOption(self::INPUT_STORES) || $input->getOption(self::INPUT_STORES) === 'all') { | ||
$stores = null; | ||
$styledOutput->title('Clearing full Solr product index...'); | ||
} else { | ||
$stores = \explode(',', $input->getOption(self::INPUT_STORES)); | ||
$styledOutput->title('Clearing Solr product index for stores ' . \implode(', ', $stores) . '...'); | ||
} | ||
try { | ||
$this->indexer->addProgressHandler( | ||
new ProgressInConsole($output) | ||
); | ||
if ($input->getOption(self::INPUT_USESWAPCORE)) { | ||
$this->indexer->clearStoresOnSwappedCore($stores); | ||
} else { | ||
$this->indexer->clearStores($stores); | ||
} | ||
$totalTime = number_format(microtime(true) - $startTime, 2); | ||
$styledOutput->success("Clearing finished in $totalTime seconds."); | ||
} catch (\Exception $e) { | ||
$styledOutput->error($e->getMessage()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace IntegerNet\Solr\Console\Command; | ||
|
||
use IntegerNet\Solr\Indexer\Slice; | ||
use IntegerNet\Solr\Model\Indexer; | ||
use Magento\Framework\App; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* solr:swap command | ||
*/ | ||
class SwapCommand extends Command | ||
{ | ||
const INPUT_STORES = 'stores'; | ||
|
||
/** | ||
* @var Indexer\Console | ||
*/ | ||
private $indexer; | ||
/** | ||
* @var App\State | ||
*/ | ||
private $appState; | ||
|
||
public function __construct(Indexer\Console $indexer, App\State $appState, $name = null) | ||
{ | ||
parent::__construct($name); | ||
$this->indexer = $indexer; | ||
$this->appState = $appState; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$options = [ | ||
new InputOption( | ||
self::INPUT_STORES, | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Swap cores for given stores (can be store id, store code, comma seperated. Or "all".) ' | ||
. 'If not set, swap cores for all stores.' | ||
), | ||
]; | ||
$this->setName('solr:swap'); | ||
$this->setHelp( | ||
'Swap cores. This is useful if using slices (see solr:reindex:slice) ' | ||
. 'after indexing with the "--use_swap_core" param; it\'s not needed otherwise.' | ||
); | ||
$this->setDescription('Swap cores'); | ||
$this->setDefinition($options); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$styledOutput = new StyledOutput( | ||
$output, | ||
class_exists(SymfonyStyle::class) ? new SymfonyStyle($input, $output) : null | ||
); | ||
$startTime = microtime(true); | ||
$this->appState->setAreaCode(App\Area::AREA_GLOBAL); | ||
if (!$input->getOption(self::INPUT_STORES) || $input->getOption(self::INPUT_STORES) === 'all') { | ||
$stores = null; | ||
$styledOutput->title('Swap all cores...'); | ||
} else { | ||
$stores = \explode(',', $input->getOption(self::INPUT_STORES)); | ||
$styledOutput->title('Swap cores for stores ' . \implode(', ', $stores) . '...'); | ||
} | ||
try { | ||
$this->indexer->addProgressHandler( | ||
new ProgressInConsole($output) | ||
); | ||
$this->indexer->swapCores($stores); | ||
$totalTime = number_format(microtime(true) - $startTime, 2); | ||
$styledOutput->success("Core swap finished in $totalTime seconds."); | ||
} catch (\Exception $e) { | ||
$styledOutput->error($e->getMessage()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you call
$this->indexer->checkSwapCoresConfiguration($stores)
before? If you don't have the configuration right, it could severely mess up the index (i.e. two stores sharing a core, but only one of the stores is being swapped).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done it in
Console:swapCores()
now, which is called here