-
-
Save vixe76/4281972603637b5a8b6463490e70649c to your computer and use it in GitHub Desktop.
Neos Flow CLI Command to process entities in batches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| declare(strict_types=1); | |
| namespace Some\Package\Command; | |
| use Neos\Flow\Annotations as Flow; | |
| use Neos\Flow\Cli\CommandController; | |
| class SomeCommand extends CommandController | |
| { | |
| /** | |
| * @Flow\InjectConfiguration(package="Neos.Flow") | |
| * @var array | |
| */ | |
| protected $flowSettings; | |
| /** | |
| * @Flow\Inject | |
| * @var SomeRepository | |
| */ | |
| protected $someRepository; | |
| /** | |
| * Some description | |
| * | |
| * @param int $batchSize number of entities to process at once | |
| */ | |
| public function processAllCommand(int $batchSize = 50) | |
| { | |
| $entityCount = $this->someRepository->countAll(); | |
| $this->outputLine('Processing <b>%s</b> entities.', [$entityCount]); | |
| $this->output->progressStart($documentCount); | |
| $offset = 0; | |
| while ($offset < $documentCount) { | |
| $commandArguments = [ | |
| 'offset' => $offset, | |
| 'limit' => $batchSize | |
| ]; | |
| Scripts::executeCommand('some.package:some:processbatch', $this->flowSettings, true, ['offset' => $offset, 'limit' => $batchSize]); | |
| $offset = min($offset + $batchSize, $entityCount); | |
| $this->output->progressSet($offset); | |
| } | |
| $this->output->progressFinish(); | |
| $this->outputLine('<success>Done.</success>'); | |
| } | |
| /** | |
| * @param int $offset | |
| * @param int $limit | |
| * @internal | |
| */ | |
| public function processBatchCommand(int $offset, int $limit) | |
| { | |
| $allEntities = $this->someRepository->findAll(); | |
| $query = $allEntities->getQuery(); | |
| $query->setOffset($offset); | |
| $query->setLimit($limit); | |
| /** @var SomeEntity $entity */ | |
| foreach ($query->execute() as $entity) { | |
| // do something with $entity | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment