Skip to content

Commit

Permalink
Fixing async index runner + adding unit tests
Browse files Browse the repository at this point in the history
Correcting async indexers running + adding unit tests.

Correcting async indexers running + adding unit tests.
  • Loading branch information
Bartosz Kubicki committed Oct 12, 2018
1 parent 8b92f81 commit 0c690ac
Show file tree
Hide file tree
Showing 27 changed files with 1,048 additions and 163 deletions.
27 changes: 27 additions & 0 deletions Api/Adapter/ReactPHP/ChildProcess/ProcessFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* File:ProcessFactoryInterface.php
*
* @author Maciej Sławik <maciej.slawik@lizardmedia.pl>
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
*/

namespace LizardMedia\AdminIndexer\Api\Adapter\ReactPHP\ChildProcess;

use React\ChildProcess\Process;

/**
* Interface ProcessFactoryInterface
* @package LizardMedia\AdminIndexer\Api\Adapter\ReactPHP
*/
interface ProcessFactoryInterface
{
/**
* @param string $command
* @return Process
*/
public function create(string $command): Process;
}
27 changes: 27 additions & 0 deletions Api/Adapter/ReactPHP/EventLoop/LoopFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

/**
* File: LoopFactoryInterface.php
*
* @author Bartosz Kubicki bartosz.kubicki@lizardmedia.pl>
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
*/

namespace LizardMedia\AdminIndexer\Api\Adapter\ReactPHP\EventLoop;

use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;

/**
* Interface LoopFactoryInterface
* @package LizardMedia\AdminIndexer\Api\Adapter\ReactPHP\EventLoop
*/
interface LoopFactoryInterface
{
/**
* @return LoopInterface
*/
public function create(): LoopInterface;
}
4 changes: 2 additions & 2 deletions Api/IndexerProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace LizardMedia\AdminIndexer\Api;

use LizardMedia\AdminIndexer\Exception\ReindexFailureException;
use LizardMedia\AdminIndexer\Exception\ReindexFailureAggregateException;

/**
* Interface IndexerProcessorInterface
Expand All @@ -23,7 +23,7 @@ interface IndexerProcessorInterface
/**
* @param string[] ...$indexerIds
* @return void
* @throws ReindexFailureException
* @throws ReindexFailureAggregateException
*/
public function process(string ...$indexerIds): void;
}
23 changes: 0 additions & 23 deletions Api/ReindexRunner/AsyncReindexRunnerInterface.php

This file was deleted.

23 changes: 0 additions & 23 deletions Api/ReindexRunner/SyncReindexRunnerInterface.php

This file was deleted.

8 changes: 5 additions & 3 deletions Api/ReindexRunnerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@

namespace LizardMedia\AdminIndexer\Api;

use LizardMedia\AdminIndexer\Exception\ReindexFailureAggregateException;

/**
* Interface ReindexRunnerInterface
* @package LizardMedia\AdminIndexer\Api
*/
interface ReindexRunnerInterface
{
/**
* @param string $indexerId
* @param string[] ...$indexerIds
* @return void
* @throws \Exception
* @throws ReindexFailureAggregateException
*/
public function run(string $indexerId): void;
public function run(string ...$indexerIds): void;
}
30 changes: 22 additions & 8 deletions Controller/Adminhtml/Indexer/MassReindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use LizardMedia\AdminIndexer\Api\IndexerProcessorInterface;
use LizardMedia\AdminIndexer\Api\ReindexRunner\MessageBagInterface;
use LizardMedia\AdminIndexer\Exception\ReindexFailureException;
use LizardMedia\AdminIndexer\Exception\ReindexFailureAggregateException;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\Redirect;
Expand Down Expand Up @@ -60,7 +60,6 @@ public function __construct(
IndexerProcessorInterface $indexerProcessor
) {
parent::__construct($context);

$this->messageBag = $messageBag;
$this->redirectFactory = $redirectFactory;
$this->indexerProcessor = $indexerProcessor;
Expand All @@ -69,21 +68,22 @@ public function __construct(
/**
* @return Redirect
*/
public function execute() : Redirect
public function execute(): Redirect
{
$indexerIds = $this->getRequest()->getParam('indexer_ids');

if (!$this->validateParam($indexerIds)) {
$this->messageManager->addErrorMessage(__('Please select at least one index.'));
return $this->getRedirect();
}

$indexerIds = $this->castValuesToString($indexerIds);

try {
$this->indexerProcessor->process(...$indexerIds);
$this->displayMessages();
} catch (ReindexFailureException $exception) {
$this->messageManager->addErrorMessage(__('Reindex failed on indexer %1.', $exception->getIndexerName()));
$this->displayMessagesAboutRunningIndexers();
} catch (ReindexFailureAggregateException $exception) {
$this->displayErrors($exception);
}

return $this->getRedirect();
Expand Down Expand Up @@ -121,17 +121,31 @@ private function validateParam($indexerIds): bool
private function castValuesToString(array $indexerIds): array
{
return array_filter($indexerIds, function ($indexerId) {
return (string)$indexerId;
return (string) $indexerId;
});
}

/**
* @return void
*/
private function displayMessages(): void
private function displayMessagesAboutRunningIndexers(): void
{
foreach ($this->messageBag->getMessages() as $message) {
$this->messageManager->addNoticeMessage($message);
}
}

/**
* @param ReindexFailureAggregateException $exception
* @return void
*/
private function displayErrors(ReindexFailureAggregateException $exception): void
{
$this->messageManager->addErrorMessage($exception->getMessage());
$errors = $exception->getErrors();

foreach ($errors as $error) {
$this->messageManager->addErrorMessage($error->getMessage());
}
}
}
22 changes: 22 additions & 0 deletions Exception/ReindexFailureAggregateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/**
* File: ReindexFailureAggregateException.php
*
* @author Bartosz Kubicki bartosz.kubicki@lizardmedia.pl>
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
*/

namespace LizardMedia\AdminIndexer\Exception;

use Magento\Framework\Exception\AbstractAggregateException;

/**
* Class ReindexFailureAggregateException
* @package LizardMedia\AdminIndexer\Exception
*/
class ReindexFailureAggregateException extends AbstractAggregateException
{
}
49 changes: 0 additions & 49 deletions Exception/ReindexFailureException.php

This file was deleted.

31 changes: 31 additions & 0 deletions Model/Adapter/ReactPHP/ChildProcess/ProcessFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* File:ProcessFactory.php
*
* @author Maciej Sławik <maciej.slawik@lizardmedia.pl>
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
*/

namespace LizardMedia\AdminIndexer\Model\Adapter\ReactPHP\ChildProcess;

use LizardMedia\AdminIndexer\Api\Adapter\ReactPHP\ChildProcess\ProcessFactoryInterface;
use React\ChildProcess\Process;

/**
* Class ProcessFactory
* @package LizardMedia\AdminIndexer\Model\Adapter\ReactPHP
*/
class ProcessFactory implements ProcessFactoryInterface
{
/**
* @param string $command
* @return Process
*/
public function create(string $command): Process
{
return new Process($command);
}
}
46 changes: 46 additions & 0 deletions Model/Adapter/ReactPHP/EventLoop/LoopFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

/**
* File: LoopFactory.php
*
* @author Bartosz Kubicki bartosz.kubicki@lizardmedia.pl>
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
*/

namespace LizardMedia\AdminIndexer\Model\Adapter\ReactPHP\EventLoop;

use LizardMedia\AdminIndexer\Api\Adapter\ReactPHP\EventLoop\LoopFactoryInterface;
use React\EventLoop\LoopInterface;
use React\EventLoop\Factory;

/**
* Class LoopFactory
* @package LizardMedia\AdminIndexer\Model\Adapter\ReactPHP\EventLoop
*/
class LoopFactory implements LoopFactoryInterface
{
/**
* @var Factory
*/
private $factory;

/**
* LoopFactory constructor.
* @param Factory $factory
*/
public function __construct(
Factory $factory
) {
$this->factory = $factory;
}

/**
* @return LoopInterface
*/
public function create(): LoopInterface
{
return $this->factory->create();
}
}
Loading

0 comments on commit 0c690ac

Please sign in to comment.