|
| 1 | +<?php |
| 2 | +namespace Enqueue\ElasticaBundle\Listener; |
| 3 | + |
| 4 | +use Enqueue\Psr\Context; |
| 5 | +use Enqueue\Util\JSON; |
| 6 | +use FOS\ElasticaBundle\Doctrine\ORM\Provider; |
| 7 | + |
| 8 | +class AsyncDoctrineOrmProvider extends Provider |
| 9 | +{ |
| 10 | + private $batchSize; |
| 11 | + |
| 12 | + /** |
| 13 | + * @var Context |
| 14 | + */ |
| 15 | + private $context; |
| 16 | + |
| 17 | + /** |
| 18 | + * @param Context $context |
| 19 | + */ |
| 20 | + public function setContext(Context $context) |
| 21 | + { |
| 22 | + $this->context = $context; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * {@inheritDoc} |
| 27 | + */ |
| 28 | + protected function doPopulate($options, \Closure $loggerClosure = null) |
| 29 | + { |
| 30 | + $this->batchSize = null; |
| 31 | + if ($options['real_populate']) { |
| 32 | + $this->batchSize = $options['offset'] + $options['batch_size']; |
| 33 | + |
| 34 | + return parent::doPopulate($options, $loggerClosure); |
| 35 | + } |
| 36 | + |
| 37 | + $queryBuilder = $this->createQueryBuilder($options['query_builder_method']); |
| 38 | + $nbObjects = $this->countObjects($queryBuilder); |
| 39 | + $offset = $options['offset']; |
| 40 | + |
| 41 | + $queue = $this->context->createQueue('fos_elastica.populate'); |
| 42 | + $resultQueue = $this->context->createTemporaryQueue(); |
| 43 | + $consumer = $this->context->createConsumer($resultQueue); |
| 44 | + |
| 45 | + $producer = $this->context->createProducer(); |
| 46 | + |
| 47 | + $nbMessages = 0; |
| 48 | + for (; $offset < $nbObjects; $offset += $options['batch_size']) { |
| 49 | + $options['offset'] = $offset; |
| 50 | + $options['real_populate'] = true; |
| 51 | + $message = $this->context->createMessage(JSON::encode($options)); |
| 52 | + $message->setReplyTo($resultQueue->getQueueName()); |
| 53 | + $producer->send($queue, $message); |
| 54 | + |
| 55 | + $nbMessages++; |
| 56 | + } |
| 57 | + |
| 58 | + $limitTime = time() + 180; |
| 59 | + while ($nbMessages) { |
| 60 | + if ($message = $consumer->receive(20000)) { |
| 61 | + if (null !== $loggerClosure) { |
| 62 | + $loggerClosure($options['batch_size'], $nbObjects); |
| 63 | + } |
| 64 | + |
| 65 | + $consumer->acknowledge($message); |
| 66 | + |
| 67 | + $nbMessages--; |
| 68 | + |
| 69 | + $limitTime = time() + 180; |
| 70 | + } |
| 71 | + |
| 72 | + if (time() > $limitTime) { |
| 73 | + throw new \LogicException(sprintf('No response in %d seconds', 180)); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritDoc} |
| 80 | + */ |
| 81 | + protected function countObjects($queryBuilder) |
| 82 | + { |
| 83 | + return $this->batchSize ? $this->batchSize : parent::countObjects($queryBuilder); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritDoc} |
| 88 | + */ |
| 89 | + protected function configureOptions() |
| 90 | + { |
| 91 | + parent::configureOptions(); |
| 92 | + |
| 93 | + $this->resolver->setDefaults([ |
| 94 | + 'real_populate' => false, |
| 95 | + ]); |
| 96 | + } |
| 97 | +} |
0 commit comments