pre/postFlush configuration. Update documentation.

This commit is contained in:
nurikabe 2013-12-03 20:41:26 +00:00
parent af2827df01
commit 22a5d67d05
5 changed files with 56 additions and 8 deletions

View file

@ -165,6 +165,7 @@ class Configuration implements ConfigurationInterface
->scalarNode('insert')->defaultTrue()->end()
->scalarNode('update')->defaultTrue()->end()
->scalarNode('delete')->defaultTrue()->end()
->scalarNode('persist')->defaultValue('postFlush')->end()
->scalarNode('service')->end()
->variableNode('is_indexable_callback')->defaultNull()->end()
->end()
@ -257,6 +258,7 @@ class Configuration implements ConfigurationInterface
->scalarNode('insert')->defaultTrue()->end()
->scalarNode('update')->defaultTrue()->end()
->scalarNode('delete')->defaultTrue()->end()
->booleanNode('immediate')->defaultFalse()->end()
->scalarNode('service')->end()
->variableNode('is_indexable_callback')->defaultNull()->end()
->end()

View file

@ -405,7 +405,7 @@ class FOSElasticaExtension extends Extension
private function getDoctrineEvents(array $typeConfig)
{
// Flush event always fires; not configurable
// Flush always calls depending on actions scheduled in lifecycle listeners
$typeConfig['listener']['flush'] = true;
$events = array();
@ -413,9 +413,11 @@ class FOSElasticaExtension extends Extension
'insert' => array('postPersist'),
'update' => array('postUpdate'),
'delete' => array('preRemove'),
'flush' => array('postFlush')
'flush' => array($typeConfig['listener']['immediate'] ? 'preFlush' : 'postFlush')
);
var_dump($eventMapping);
foreach ($eventMapping as $event => $doctrineEvents) {
if (isset($typeConfig['listener'][$event]) && $typeConfig['listener'][$event]) {
$events = array_merge($events, $doctrineEvents);

View file

@ -210,10 +210,9 @@ class Listener implements EventSubscriber
}
/**
* Iterate through scheduled actions *after* flushing to ensure that the ElasticSearch index will only be affected
* only if the query is successful
* Persist scheduled action to ElasticSearch
*/
public function postFlush(EventArgs $eventArgs)
private function persistScheduled()
{
foreach ($this->scheduledForInsertion as $entity) {
$this->objectPersister->insertOne($entity);
@ -225,4 +224,22 @@ class Listener implements EventSubscriber
$this->objectPersister->deleteOne($entity);
}
}
/**
* Iterate through scheduled actions before flushing to emulate 2.x behavior. Note that the ElasticSearch index
* will fall out of sync with the data source in event of a crash on flush.
*/
public function preFlush(EventArgs $eventArgs)
{
$this->persistScheduled();
}
/**
* Iterating through scheduled actions *after* flushing ensures that the ElasticSearch index will be affected
* only if the query is successful
*/
public function postFlush(EventArgs $eventArgs)
{
$this->persistScheduled();
}
}

View file

@ -576,7 +576,11 @@ class User
### Realtime, selective index update
If you use the Doctrine integration, you can let ElasticaBundle update the indexes automatically
when an object is added, updated or removed. It uses Doctrine lifecycle events.
when an object is added, updated or removed. It uses Doctrine lifecycle events to schedule updates
and then synchronizes changes either before or after flush.
> **Propel** doesn't support this feature yet.
Declare that you want to update the index in real time:
fos_elastica:
@ -592,7 +596,7 @@ Declare that you want to update the index in real time:
persistence:
driver: orm
model: Application\UserBundle\Entity\User
listener: ~ # by default, listens to "insert", "update" and "delete"
listener: ~ # by default, listens to "insert", "update" and "delete" and updates `postFlush`
Now the index is automatically updated each time the state of the bound Doctrine repository changes.
No need to repopulate the whole "user" index when a new `User` is created.
@ -605,7 +609,19 @@ You can also choose to only listen for some of the events:
update: false
delete: true
> **Propel** doesn't support this feature yet.
By default, the ElasticSearch index will be updated after flush. To update before flushing, set `immediate`
to `true`:
persistence:
listener:
insert: true
update: false
delete: true
immediate: true
> Updating ElasticSearch before flushing may cause the ElasticSearch index to fall out of sync with the
> original data in the event of a crash.
### Checking an entity method for listener

11
UPGRADE-3.0.md Normal file
View file

@ -0,0 +1,11 @@
UPGRADE FROM 2.x to 3.0
=======================
### ElasticSearch Synchronization Event
* Prior to 3.0, the ElasticSearch index was synchronized in the `postInsert`,
`postUpdate`, and `pre/postRemove` events which fire before flush. Because
of this, exceptions thrown when flushing would cause the data source and
ElasticSearch index to fall out of sync.
As of 3.0, ElasticSearch is updated `postFlush` by default.