Skip to content

Commit

Permalink
Merge pull request #174 from delyriand/feature/fallback-elasticsearch…
Browse files Browse the repository at this point in the history
…-failed

Fallback on sylius taxon renderer if ES is down
  • Loading branch information
maximehuran authored Jul 27, 2023
2 parents e39f4ef + 7968ab9 commit 95f4f99
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 1 deletion.
1 change: 1 addition & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- New method `deleteByDocumentIds` in the `MonsieurBiz\SyliusSearchPlugin\Index\IndexerInterface` interface
- Deprecated the method `deleteByDocuments` in the `MonsieurBiz\SyliusSearchPlugin\Index\IndexerInterface` interface. Use `deleteByDocumentIds` instead.
- `ChannelFilter` and `EnabledFilter` in `MonsieurBiz\SyliusSearchPlugin\Search\Request\QueryFilter\Product` were moved to `MonsieurBiz\SyliusSearchPlugin\Search\Request\QueryFilter`
- A fallback on the Sylius' taxon display is now used to keep your pages even if you Elasticsearch instance is down. If you want to disable it, check the [FakeElasticsearchChecker](docs/disable_elasticsearch_checker.md)

# UPGRADE FROM v1.X.X TO v2.0.x

Expand Down
11 changes: 11 additions & 0 deletions docs/disable_elasticsearch_checker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Disable the ElasticsearchChecker

The plugin now checks if the Elasticsearch server is running before each search.

If you want to disable this feature, you can do it by adding the following configuration:

```yaml
services:
monsieurbiz.search.checker.elasticsearch_checker:
class: MonsieurBiz\SyliusSearchPlugin\Checker\FakeElasticsearchChecker
```
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Menu

- [Disable the ElasticsearchChecker](./disable_elasticsearch_checker.md)
- [Add custom sorts](./add_custom_sorts.md)
- [Add custom values for an entity](./add_custom_values.md)
- [Add custom filters](./add_custom_filters.md)
Expand Down
41 changes: 41 additions & 0 deletions src/Checker/ElasticsearchChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSearchPlugin\Checker;

use JoliCode\Elastically\Factory;

class ElasticsearchChecker implements ElasticsearchCheckerInterface
{
private ?bool $isAvailable = null;

public function check(): bool
{
if (null === $this->isAvailable) {
$client = (new Factory())->buildClient();

// Check client response
try {
$client->getStatus()->getResponse();
} catch (\Exception $e) {
$this->isAvailable = false;

return $this->isAvailable;
}

$this->isAvailable = true;
}

return $this->isAvailable;
}
}
19 changes: 19 additions & 0 deletions src/Checker/ElasticsearchCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSearchPlugin\Checker;

interface ElasticsearchCheckerInterface
{
public function check(): bool;
}
22 changes: 22 additions & 0 deletions src/Checker/FakeElasticsearchChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSearchPlugin\Checker;

class FakeElasticsearchChecker implements ElasticsearchCheckerInterface
{
public function check(): bool
{
return true;
}
}
2 changes: 2 additions & 0 deletions src/Resources/config/routing/shop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ monsieurbiz_search_search:
_controller: MonsieurBiz\SyliusSearchPlugin\Controller\SearchController::searchAction
requirements:
query: .+
condition: "not(context.getPathInfo() matches '`^%sylius.security.new_api_route%`') and context.checkElasticsearch()"

monsieurbiz_search_post:
path: /search
Expand All @@ -27,3 +28,4 @@ monsieurbiz_sylius_search_taxon:
taxon: "expr:notFoundOnNull(service('sylius.repository.taxon').findOneBySlug($slug, service('sylius.context.locale').getLocaleCode()))"
requirements:
slug: .+
condition: "not(context.getPathInfo() matches '`^%sylius.security.new_api_route%`') and context.checkElasticsearch()"
14 changes: 14 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,17 @@ services:
- { name: kernel.event_listener, event: sylius.product_variant.post_update, method: dispatchProductVariantReindexMessage }
- { name: kernel.event_listener, event: sylius.product_variant.pre_delete, method: saveProductIdToDispatchReindexMessage }
- { name: kernel.event_listener, event: sylius.product_variant.post_delete, method: dispatchProductReindexMessage }

monsieurbiz.search.checker.elasticsearch_checker:
class: MonsieurBiz\SyliusSearchPlugin\Checker\ElasticsearchChecker

MonsieurBiz\SyliusSearchPlugin\Twig\Extension\RenderSearchForm:
arguments:
$elasticsearchChecker: '@monsieurbiz.search.checker.elasticsearch_checker'

# Routing Context
MonsieurBiz\SyliusSearchPlugin\Routing\RequestContext:
decorates: router.request_context
arguments:
- '@MonsieurBiz\SyliusSearchPlugin\Routing\RequestContext.inner'
- '@monsieurbiz.search.checker.elasticsearch_checker'
63 changes: 63 additions & 0 deletions src/Routing/RequestContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSearchPlugin\Routing;

use Exception;
use MonsieurBiz\SyliusSearchPlugin\Checker\ElasticsearchCheckerInterface;
use Symfony\Component\Routing\RequestContext as BaseRequestContext;

class RequestContext extends BaseRequestContext
{
private BaseRequestContext $decorated;

private ElasticsearchCheckerInterface $elasticsearchChecker;

public function __construct(
BaseRequestContext $decorated,
ElasticsearchCheckerInterface $elasticsearchChecker
) {
parent::__construct(
$decorated->getBaseUrl(),
$decorated->getMethod(),
$decorated->getHost(),
$decorated->getScheme(),
$decorated->getHttpPort(),
$decorated->getHttpsPort(),
$decorated->getPathInfo(),
$decorated->getQueryString()
);
$this->decorated = $decorated;
$this->elasticsearchChecker = $elasticsearchChecker;
}

public function checkElasticsearch(): bool
{
return $this->elasticsearchChecker->check();
}

/**
* @throws Exception
*
* @return mixed
*/
public function __call(string $name, array $arguments)
{
$callback = [$this->decorated, $name];
if (\is_callable($callback)) {
return \call_user_func($callback, $arguments);
}

throw new Exception(sprintf('Method %s not found for class "%s"', $name, \get_class($this->decorated)));
}
}
11 changes: 10 additions & 1 deletion src/Twig/Extension/RenderSearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace MonsieurBiz\SyliusSearchPlugin\Twig\Extension;

use MonsieurBiz\SyliusSearchPlugin\Checker\ElasticsearchCheckerInterface;
use MonsieurBiz\SyliusSearchPlugin\Form\Type\SearchType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -29,14 +30,18 @@ class RenderSearchForm extends AbstractExtension

private RequestStack $requestStack;

private ElasticsearchCheckerInterface $elasticsearchChecker;

public function __construct(
FormFactoryInterface $formFactory,
Environment $templatingEngine,
RequestStack $requestStack
RequestStack $requestStack,
ElasticsearchCheckerInterface $elasticsearchChecker
) {
$this->formFactory = $formFactory;
$this->templatingEngine = $templatingEngine;
$this->requestStack = $requestStack;
$this->elasticsearchChecker = $elasticsearchChecker;
}

public function getFunctions()
Expand All @@ -48,6 +53,10 @@ public function getFunctions()

public function createForm(?string $template = null): Markup
{
if (false === $this->elasticsearchChecker->check()) {
return new Markup('', 'UTF-8');
}

$request = $this->requestStack->getCurrentRequest();
$template = $template ?? '@MonsieurBizSyliusSearchPlugin/Search/_form.html.twig';
$query = null !== $request ? $request->get('query', '') : '';
Expand Down

0 comments on commit 95f4f99

Please sign in to comment.