Skip to content

Commit d08da85

Browse files
authored
Merge pull request #1349 from formapro-forks/4x-to-master
4x to master
2 parents 99d0b0d + c81d820 commit d08da85

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
@@ -327,6 +327,7 @@ protected function getPersistenceNode()
327327
->arrayNode('listener')
328328
->addDefaultsIfNotSet()
329329
->children()
330+
->booleanNode('enabled')->defaultTrue()->end()
330331
->scalarNode('insert')->defaultTrue()->end()
331332
->scalarNode('update')->defaultTrue()->end()
332333
->scalarNode('delete')->defaultTrue()->end()

DependencyInjection/FOSElasticaExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ private function loadTypePersistenceIntegration(array $typeConfig, ContainerBuil
368368
if (isset($typeConfig['finder'])) {
369369
$this->loadTypeFinder($typeConfig, $container, $elasticaToModelTransformerId, $typeRef, $indexName, $typeName);
370370
}
371-
if (isset($typeConfig['listener'])) {
371+
if (isset($typeConfig['listener']) && $typeConfig['listener']['enabled']) {
372372
$this->loadTypeListener($typeConfig, $container, $objectPersisterId, $indexName, $typeName);
373373
}
374374
}
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
@@ -19,11 +19,14 @@ Cookbook Entries
1919
* [Pre Transform Event](cookbook/pre-transform-event.md)
2020
* [HTTP Headers for Elastica](cookbook/elastica-client-http-headers.md)
2121
* [HTTP Auth for Elastica](cookbook/http-auth-for-elastica.md)
22-
* Performance - [Logging](cookbook/logging.md) - [Compression](cookbook/compression.md)
2322
* [Manual Providers](cookbook/manual-provider.md)
2423
* [Clustering - Multiple Connections](cookbook/multiple-connections.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
@@ -12,6 +12,7 @@
1212
namespace FOS\ElasticaBundle\Tests\DependencyInjection;
1313

1414
use FOS\ElasticaBundle\DependencyInjection\FOSElasticaExtension;
15+
use FOS\ElasticaBundle\Doctrine\Listener;
1516
use FOS\ElasticaBundle\Doctrine\RegisterListenersService;
1617
use FOS\ElasticaBundle\Doctrine\MongoDBPagerProvider;
1718
use FOS\ElasticaBundle\Doctrine\ORMPagerProvider;
@@ -493,4 +494,72 @@ public function testShouldRegisterPagerPersisterRegisterService()
493494
$this->assertSame(PagerPersisterRegistry::class, $listener->getClass());
494495
$this->assertSame([], $listener->getArgument(0));
495496
}
497+
498+
public function testShouldRegisterDoctrineORMListener()
499+
{
500+
$container = new ContainerBuilder();
501+
$container->setParameter('kernel.debug', true);
502+
503+
$extension = new FOSElasticaExtension();
504+
$extension->load([
505+
'fos_elastica' => [
506+
'clients' => [
507+
'default' => ['host' => 'a_host', 'port' => 'a_port'],
508+
],
509+
'indexes' => [
510+
'acme_index' => [
511+
'types' => [
512+
'acme_type' => [
513+
'properties' => ['text' => null],
514+
'persistence' => [
515+
'driver' => 'orm',
516+
'model' => 'theModelClass',
517+
'provider' => null,
518+
'listener' => null,
519+
'finder' => null,
520+
]
521+
]
522+
]
523+
]
524+
]
525+
]
526+
], $container);
527+
528+
$this->assertTrue($container->hasDefinition('fos_elastica.listener.acme_index.acme_type'));
529+
}
530+
531+
public function testShouldNotRegisterDoctrineORMListenerIfDisabled()
532+
{
533+
$container = new ContainerBuilder();
534+
$container->setParameter('kernel.debug', true);
535+
536+
$extension = new FOSElasticaExtension();
537+
$extension->load([
538+
'fos_elastica' => [
539+
'clients' => [
540+
'default' => ['host' => 'a_host', 'port' => 'a_port'],
541+
],
542+
'indexes' => [
543+
'acme_index' => [
544+
'types' => [
545+
'acme_type' => [
546+
'properties' => ['text' => null],
547+
'persistence' => [
548+
'driver' => 'orm',
549+
'model' => 'theModelClass',
550+
'provider' => null,
551+
'listener' => [
552+
'enabled' => false,
553+
],
554+
'finder' => null,
555+
]
556+
]
557+
]
558+
]
559+
]
560+
]
561+
], $container);
562+
563+
$this->assertFalse($container->hasDefinition('fos_elastica.listener.acme_index.acme_type'));
564+
}
496565
}

0 commit comments

Comments
 (0)