Skip to content

Commit

Permalink
Merge pull request #1349 from formapro-forks/4x-to-master
Browse files Browse the repository at this point in the history
4x to master
  • Loading branch information
makasim committed Dec 15, 2017
2 parents 99d0b0d + c81d820 commit d08da85
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 4 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -327,6 +327,7 @@ protected function getPersistenceNode()
->arrayNode('listener')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')->defaultTrue()->end()
->scalarNode('insert')->defaultTrue()->end()
->scalarNode('update')->defaultTrue()->end()
->scalarNode('delete')->defaultTrue()->end()
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/FOSElasticaExtension.php
Expand Up @@ -368,7 +368,7 @@ private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuil
if (isset($typeConfig['finder'])) {
$this->loadTypeFinder($typeConfig, $container, $elasticaToModelTransformerId, $typeRef, $indexName, $typeName);
}
if (isset($typeConfig['listener'])) {
if (isset($typeConfig['listener']) && $typeConfig['listener']['enabled']) {
$this->loadTypeListener($typeConfig, $container, $objectPersisterId, $indexName, $typeName);
}
}
Expand Down
64 changes: 64 additions & 0 deletions Resources/doc/cookbook/doctrine-queue-listener.md
@@ -0,0 +1,64 @@
# Doctrine queue listener

FOSElasticaBundle subscribes on Doctrine events, such as insert, update, remove to adjust the index accordingly.
The listener might start consuming more and more resources, most importantly time of http response.
Or, Sometimes it fails, bringing the whole your app down too, because of ElasticSearch server is out of order or some bug in the code.
Keep reading if you want to improve http response time or strive for better fault tolerance.

Instead of doing everything in one single process the listener just sends a message to a worker (via [message queue](https://en.wikipedia.org/wiki/Message_queue)).
The work does the actual synchronization job in background.
For queuing it uses [EnqueueBundle](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/bundle/quick_tour.md) which supports a lot of MQ transports out of the box.

## Installation

I assume you already have `FOSElasticaBundle` installed, if not here's the [setup doc](../setup.md).
So, we only have to install `EnqueueElasticaBundle` and one of the MQ transports.
I am going to install the bundle and filesystem transport by way of example.

```bash
$ composer require enqueue/elastica-bundle:^0.8.1 enqueue/fs:^0.8
```

_**Note:** As long as you are on Symfony Flex you are done. If not, you have to do some extra things, like registering the bundle in your `AppKernel` class._

## Usage

The usage is simple, you have to disable the default listener:

```yaml
fos_elastica:
indexes:
acme_index:
types:
acme_type:
persistence:
driver: 'orm'
model: 'AppBundle\Entity\Blog'
listener: { enabled: false }
```

and enable the queue one:

```
enqueue_elastica:
doctrine:
queue_listeners:
-
index_name: 'acme_index'
type_name: 'acme_blog'
model_class: 'AppBundle\Entity\Blog'
```

Don't forget to run some queue consumers (the more you run the better performance you might get):

```bash
$ ./bin/console enqueue:consume --setup-broker -vvv
```

or (use it only if you cannot use the solution above):

```bash
$ ./bin/console enqueue:transport:consume enqueue_elastica.doctrine.sync_index_with_object_change_processor -vvv
```

[back to index](../index.md)
2 changes: 1 addition & 1 deletion Resources/doc/cookbook/speed-up-populate-command.md
Expand Up @@ -19,7 +19,7 @@ For queuing it uses [EnqueueBundle](https://github.com/php-enqueue/enqueue-dev/b
## Installation

I assume you already have `FOSElasticaBundle` installed, if not here's the [setup doc](../setup.md).
So, we only have to install `EnqueueBundle` and one of the MQ transports.
So, we only have to install `EnqueueElasticaBundle` and one of the MQ transports.
I am going to install the bundle and filesystem transport by way of example.

```bash
Expand Down
7 changes: 5 additions & 2 deletions Resources/doc/index.md
Expand Up @@ -19,11 +19,14 @@ Cookbook Entries
* [Pre Transform Event](cookbook/pre-transform-event.md)
* [HTTP Headers for Elastica](cookbook/elastica-client-http-headers.md)
* [HTTP Auth for Elastica](cookbook/http-auth-for-elastica.md)
* Performance - [Logging](cookbook/logging.md) - [Compression](cookbook/compression.md)
* [Manual Providers](cookbook/manual-provider.md)
* [Clustering - Multiple Connections](cookbook/multiple-connections.md)
* [Hints on result hydration](cookbook/hints-on-result-hydration.md)
* [Multi type search](cookbook/multi-type-search.md)
* [Attachments Handling](cookbook/attachments.md)
* [Populate Events](cookbook/populate-events.md)
* [Speed up populate command](cookbook/speed-up-populate-command.md)
* Performance
- [Logging](cookbook/logging.md)
- [Compression](cookbook/compression.md)
- [Speed up populate command](cookbook/speed-up-populate-command.md)
- [Doctrine queue listener](cookbook/doctrine-queue-listener.md)
69 changes: 69 additions & 0 deletions Tests/DependencyInjection/FOSElasticaExtensionTest.php
Expand Up @@ -12,6 +12,7 @@
namespace FOS\ElasticaBundle\Tests\DependencyInjection;

use FOS\ElasticaBundle\DependencyInjection\FOSElasticaExtension;
use FOS\ElasticaBundle\Doctrine\Listener;
use FOS\ElasticaBundle\Doctrine\RegisterListenersService;
use FOS\ElasticaBundle\Doctrine\MongoDBPagerProvider;
use FOS\ElasticaBundle\Doctrine\ORMPagerProvider;
Expand Down Expand Up @@ -493,4 +494,72 @@ public function testShouldRegisterPagerPersisterRegisterService()
$this->assertSame(PagerPersisterRegistry::class, $listener->getClass());
$this->assertSame([], $listener->getArgument(0));
}

public function testShouldRegisterDoctrineORMListener()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', true);

$extension = new FOSElasticaExtension();
$extension->load([
'fos_elastica' => [
'clients' => [
'default' => ['host' => 'a_host', 'port' => 'a_port'],
],
'indexes' => [
'acme_index' => [
'types' => [
'acme_type' => [
'properties' => ['text' => null],
'persistence' => [
'driver' => 'orm',
'model' => 'theModelClass',
'provider' => null,
'listener' => null,
'finder' => null,
]
]
]
]
]
]
], $container);

$this->assertTrue($container->hasDefinition('fos_elastica.listener.acme_index.acme_type'));
}

public function testShouldNotRegisterDoctrineORMListenerIfDisabled()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', true);

$extension = new FOSElasticaExtension();
$extension->load([
'fos_elastica' => [
'clients' => [
'default' => ['host' => 'a_host', 'port' => 'a_port'],
],
'indexes' => [
'acme_index' => [
'types' => [
'acme_type' => [
'properties' => ['text' => null],
'persistence' => [
'driver' => 'orm',
'model' => 'theModelClass',
'provider' => null,
'listener' => [
'enabled' => false,
],
'finder' => null,
]
]
]
]
]
]
], $container);

$this->assertFalse($container->hasDefinition('fos_elastica.listener.acme_index.acme_type'));
}
}

0 comments on commit d08da85

Please sign in to comment.