|
| 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) |
0 commit comments