Skip to content

Commit

Permalink
Added: Loops & chunk options to SendEventsCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
igormukhingmailcom committed Nov 11, 2021
1 parent 392a1d7 commit 0eb6cf7
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/Command/SendEventsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Setono\SyliusFacebookPlugin\Workflow\SendPixelEventWorkflow;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\WorkflowInterface;
Expand Down Expand Up @@ -47,6 +48,28 @@ public function __construct(
parent::__construct($defaultDelay);
}

protected function configure(): void
{
parent::configure();

$this
->addOption(
'loops',
'l',
InputOption::VALUE_REQUIRED,
'Loops to handle before exit. Default (0) value will handle all records before exit',
0
)
->addOption(
'chunk',
'c',
InputOption::VALUE_REQUIRED,
'Limit of records to handle in one loop',
1000
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->lock()) {
Expand All @@ -58,13 +81,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$delay = $input->getOption('delay');
Assert::integerish($delay);

while ($this->pixelEventRepository->hasConsentedPending((int) $delay)) {
$loops = $input->getOption('loops');
Assert::integerish($loops);

$chunk = $input->getOption('chunk');
Assert::integerish($chunk);

$loop = 1;
while ((0 === $loops || $loop <= $loops) && $this->pixelEventRepository->hasConsentedPending((int) $delay)) {
$bulkIdentifier = uniqid('bulk-', true);
$this->pixelEventRepository->assignBulkIdentifierToPendingConsented($bulkIdentifier, (int) $delay);
$this->pixelEventRepository->assignBulkIdentifierToPendingConsented($bulkIdentifier, (int) $delay, (int) $chunk);

$pixelEvents = $this->pixelEventRepository->findByBulkIdentifier($bulkIdentifier);
$output->writeln(sprintf(
'Found %s events to send.',
'%s. Found %s events to send.',
$loop,
count($pixelEvents)
), OutputInterface::VERBOSITY_VERBOSE);

Expand Down Expand Up @@ -102,6 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->entityManager->clear();
++$loop;
}

return 0;
Expand Down

0 comments on commit 0eb6cf7

Please sign in to comment.