Skip to content

Commit 599ec8a

Browse files
authored
Merge pull request #1348 from formapro-forks/add-doctrine-queue-listner-doc
Add doctrine queue listener doc
2 parents 35a41f6 + 3c2117d commit 599ec8a

File tree

6 files changed

+141
-4
lines changed

6 files changed

+141
-4
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ protected function getPersistenceNode()
537537
->arrayNode('listener')
538538
->addDefaultsIfNotSet()
539539
->children()
540+
->booleanNode('enabled')->defaultTrue()->end()
540541
->scalarNode('insert')->defaultTrue()->end()
541542
->scalarNode('update')->defaultTrue()->end()
542543
->scalarNode('delete')->defaultTrue()->end()

DependencyInjection/FOSElasticaExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuil
334334
if (isset($typeConfig['finder'])) {
335335
$this->loadTypeFinder($typeConfig, $container, $elasticaToModelTransformerId, $typeRef, $indexName, $typeName);
336336
}
337-
if (isset($typeConfig['listener'])) {
337+
if (isset($typeConfig['listener']) && $typeConfig['listener']['enabled']) {
338338
$this->loadTypeListener($typeConfig, $container, $objectPersisterId, $indexName, $typeName);
339339
}
340340
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Doctrine queue listener
2+
3+
FOSElasticaBundle subscribes on Doctrine events, such as insert, update, remove to adjust the index accordingly.
4+
The listener might start consuming more and more resources, most importantly time of http response.
5+
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.
6+
Keep reading if you want to improve http response time or strive for better fault tolerance.
7+
8+
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)).
9+
The work does the actual synchronization job in background.
10+
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.
11+
12+
## Installation
13+
14+
I assume you already have `FOSElasticaBundle` installed, if not here's the [setup doc](../setup.md).
15+
So, we only have to install `EnqueueElasticaBundle` and one of the MQ transports.
16+
I am going to install the bundle and filesystem transport by way of example.
17+
18+
```bash
19+
$ composer require enqueue/elastica-bundle:^0.8.1 enqueue/fs:^0.8
20+
```
21+
22+
_**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._
23+
24+
## Usage
25+
26+
The usage is simple, you have to disable the default listener:
27+
28+
```yaml
29+
fos_elastica:
30+
indexes:
31+
acme_index:
32+
types:
33+
acme_type:
34+
persistence:
35+
driver: 'orm'
36+
model: 'AppBundle\Entity\Blog'
37+
listener: { enabled: false }
38+
```
39+
40+
and enable the queue one:
41+
42+
```
43+
enqueue_elastica:
44+
doctrine:
45+
queue_listeners:
46+
-
47+
index_name: 'acme_index'
48+
type_name: 'acme_blog'
49+
model_class: 'AppBundle\Entity\Blog'
50+
```
51+
52+
Don't forget to run some queue consumers (the more you run the better performance you might get):
53+
54+
```bash
55+
$ ./bin/console enqueue:consume --setup-broker -vvv
56+
```
57+
58+
or (use it only if you cannot use the solution above):
59+
60+
```bash
61+
$ ./bin/console enqueue:transport:consume enqueue_elastica.doctrine.sync_index_with_object_change_processor -vvv
62+
```
63+
64+
[back to index](../index.md)

Resources/doc/cookbook/speed-up-populate-command.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ For queuing it uses [EnqueueBundle](https://github.com/php-enqueue/enqueue-dev/b
1919
## Installation
2020

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

2525
```bash

Resources/doc/index.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ Cookbook Entries
1818
* [Custom Repositories](cookbook/custom-repositories.md)
1919
* [Pre Transform Event](cookbook/pre-transform-event.md)
2020
* [HTTP Headers for Elastica](cookbook/elastica-client-http-headers.md)
21-
* Performance - [Logging](cookbook/logging.md) - [Compression](cookbook/compression.md)
2221
* [Manual Providers](cookbook/manual-provider.md)
2322
* [Clustering - Multiple Connections](cookbook/multiple-connections.md)
2423
* [Suppressing server errors](cookbook/suppress-server-errors.md)
2524
* [Hints on result hydration](cookbook/hints-on-result-hydration.md)
2625
* [Multi type search](cookbook/multi-type-search.md)
2726
* [Attachments Handling](cookbook/attachments.md)
2827
* [Populate Events](cookbook/populate-events.md)
29-
* [Speed up populate command](cookbook/speed-up-populate-command.md)
28+
* Performance
29+
- [Logging](cookbook/logging.md)
30+
- [Compression](cookbook/compression.md)
31+
- [Speed up populate command](cookbook/speed-up-populate-command.md)
32+
- [Doctrine queue listener](cookbook/doctrine-queue-listener.md)

Tests/DependencyInjection/FOSElasticaExtensionTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Doctrine\ODM\MongoDB\DocumentManager;
66
use FOS\ElasticaBundle\DependencyInjection\FOSElasticaExtension;
7+
use FOS\ElasticaBundle\Doctrine\Listener;
78
use FOS\ElasticaBundle\Doctrine\RegisterListenersService;
89
use FOS\ElasticaBundle\Doctrine\MongoDBPagerProvider;
910
use FOS\ElasticaBundle\Doctrine\ORMPagerProvider;
@@ -521,4 +522,72 @@ public function testShouldRegisterPagerPersisterRegisterService()
521522
$this->assertSame(PagerPersisterRegistry::class, $listener->getClass());
522523
$this->assertSame([], $listener->getArgument(0));
523524
}
525+
526+
public function testShouldRegisterDoctrineORMListener()
527+
{
528+
$container = new ContainerBuilder();
529+
$container->setParameter('kernel.debug', true);
530+
531+
$extension = new FOSElasticaExtension();
532+
$extension->load([
533+
'fos_elastica' => [
534+
'clients' => [
535+
'default' => ['host' => 'a_host', 'port' => 'a_port'],
536+
],
537+
'indexes' => [
538+
'acme_index' => [
539+
'types' => [
540+
'acme_type' => [
541+
'properties' => ['text' => null],
542+
'persistence' => [
543+
'driver' => 'orm',
544+
'model' => 'theModelClass',
545+
'provider' => ['pager_provider' => true],
546+
'listener' => null,
547+
'finder' => null,
548+
]
549+
]
550+
]
551+
]
552+
]
553+
]
554+
], $container);
555+
556+
$this->assertTrue($container->hasDefinition('fos_elastica.listener.acme_index.acme_type'));
557+
}
558+
559+
public function testShouldNotRegisterDoctrineORMListenerIfDisabled()
560+
{
561+
$container = new ContainerBuilder();
562+
$container->setParameter('kernel.debug', true);
563+
564+
$extension = new FOSElasticaExtension();
565+
$extension->load([
566+
'fos_elastica' => [
567+
'clients' => [
568+
'default' => ['host' => 'a_host', 'port' => 'a_port'],
569+
],
570+
'indexes' => [
571+
'acme_index' => [
572+
'types' => [
573+
'acme_type' => [
574+
'properties' => ['text' => null],
575+
'persistence' => [
576+
'driver' => 'orm',
577+
'model' => 'theModelClass',
578+
'provider' => ['pager_provider' => true],
579+
'listener' => [
580+
'enabled' => false,
581+
],
582+
'finder' => null,
583+
]
584+
]
585+
]
586+
]
587+
]
588+
]
589+
], $container);
590+
591+
$this->assertFalse($container->hasDefinition('fos_elastica.listener.acme_index.acme_type'));
592+
}
524593
}

0 commit comments

Comments
 (0)