-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace QueueItem with ItemImport model (#123)
- Loading branch information
Showing
16 changed files
with
62 additions
and
540 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +0,0 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusAkeneoPlugin\Command; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Command\LockableTrait; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Webgriffe\SyliusAkeneoPlugin\ImporterInterface; | ||
use Webgriffe\SyliusAkeneoPlugin\ImporterRegistryInterface; | ||
use Webgriffe\SyliusAkeneoPlugin\Repository\QueueItemRepositoryInterface; | ||
|
||
final class ConsumeCommand extends Command | ||
{ | ||
use LockableTrait; | ||
|
||
protected static $defaultName = 'webgriffe:akeneo:consume'; | ||
|
||
public function __construct( | ||
private QueueItemRepositoryInterface $queueItemRepository, | ||
private ImporterRegistryInterface $importerRegistry, | ||
private ManagerRegistry $managerRegistry, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setDescription('Process the Queue by calling the proper importer for each item'); | ||
} | ||
|
||
/** | ||
* @throws \Throwable | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
if (!$this->lock()) { | ||
$output->writeln('The command is already running in another process.'); | ||
|
||
return 0; | ||
} | ||
|
||
$queueItems = $this->queueItemRepository->findAllToImport(); | ||
foreach ($queueItems as $queueItem) { | ||
$akeneoIdentifier = $queueItem->getAkeneoIdentifier(); | ||
|
||
try { | ||
$importer = $this->resolveImporter($queueItem->getAkeneoEntity()); | ||
$importer->import($akeneoIdentifier); | ||
$queueItem->setImportedAt(new \DateTime()); | ||
$queueItem->setErrorMessage(null); | ||
} catch (\Throwable $t) { | ||
/** @var EntityManagerInterface $objectManager */ | ||
$objectManager = $this->managerRegistry->getManager(); | ||
if (!$objectManager->isOpen()) { | ||
$this->release(); | ||
|
||
throw $t; | ||
} | ||
$queueItem->setErrorMessage($t->getMessage() . \PHP_EOL . $t->getTraceAsString()); | ||
$output->writeln( | ||
sprintf( | ||
'There has been an error importing <info>%s</info> entity with identifier <info>%s</info>. ' . | ||
'The error was: <error>%s</error>.', | ||
$queueItem->getAkeneoEntity(), | ||
$akeneoIdentifier, | ||
$t->getMessage(), | ||
), | ||
); | ||
if ($output->isVeryVerbose()) { | ||
$output->writeln((string) $t); | ||
} | ||
} | ||
|
||
$this->queueItemRepository->add($queueItem); | ||
$output->writeln( | ||
sprintf( | ||
'<info>%s</info> entity with identifier <info>%s</info> has been imported.', | ||
$queueItem->getAkeneoEntity(), | ||
$akeneoIdentifier, | ||
), | ||
); | ||
} | ||
|
||
$this->release(); | ||
|
||
return 0; | ||
} | ||
|
||
private function resolveImporter(string $akeneoEntity): ImporterInterface | ||
{ | ||
foreach ($this->importerRegistry->all() as $importer) { | ||
if ($importer->getAkeneoEntity() === $akeneoEntity) { | ||
return $importer; | ||
} | ||
} | ||
|
||
throw new \RuntimeException(sprintf('Cannot find suitable importer for entity "%s".', $akeneoEntity)); | ||
} | ||
} | ||
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +0,0 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusAkeneoPlugin\Command; | ||
|
||
use DateInterval; | ||
use DateTime; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Webgriffe\SyliusAkeneoPlugin\DateTimeBuilder; | ||
use Webgriffe\SyliusAkeneoPlugin\Entity\QueueItem; | ||
use Webgriffe\SyliusAkeneoPlugin\Repository\CleanableQueueItemRepositoryInterface; | ||
|
||
final class QueueCleanupCommand extends Command | ||
{ | ||
public const SUCCESS = 0; | ||
|
||
public const FAILURE = 1; | ||
|
||
private const DEFAULT_DAYS = 10; | ||
|
||
private const DAYS_ARGUMENT_NAME = 'days'; | ||
|
||
// the name of the command (the part after "bin/console") | ||
protected static $defaultName = 'webgriffe:akeneo:cleanup-queue'; | ||
|
||
/** | ||
* QueueCleanupCommand constructor. | ||
*/ | ||
public function __construct(private CleanableQueueItemRepositoryInterface $queueItemRepository) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Clean the Akeneo\'s queue of items older than N days.') | ||
->setHelp('This command allows you to clean the Akeneo\'s queue of item older than a specificed numbers of days.') | ||
->addArgument( | ||
self::DAYS_ARGUMENT_NAME, | ||
InputArgument::OPTIONAL, | ||
'Number of days from which to purge the queue of previous items', | ||
(string) (self::DEFAULT_DAYS), | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$numberOfDays = self::DEFAULT_DAYS; | ||
// get the number of days from user | ||
$numberOfDaysEntered = $input->getArgument(self::DAYS_ARGUMENT_NAME); | ||
if ($numberOfDaysEntered !== null) { | ||
if (!is_string($numberOfDaysEntered) || (int) $numberOfDaysEntered < 0) { | ||
$output->writeln('Sorry, the number of days entered is not valid!'); | ||
|
||
return self::FAILURE; | ||
} | ||
$numberOfDays = (int) $numberOfDaysEntered; | ||
} | ||
|
||
// get the beginning date | ||
$dateToDelete = $this->getPreviousDateNDays($numberOfDays); | ||
|
||
$queueItems = $this->queueItemRepository->findToCleanup($dateToDelete); | ||
|
||
if (count($queueItems) === 0) { | ||
$output->writeln('There are no items to clean'); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
/** @var QueueItem $queueItem */ | ||
foreach ($queueItems as $queueItem) { | ||
$this->queueItemRepository->remove($queueItem); | ||
} | ||
|
||
$output->writeln(sprintf('<info>%s</info> items imported before <info>%s</info> has been deleted.', count($queueItems), $dateToDelete->format('Y-m-d H:i:s'))); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
private function getPreviousDateNDays(int $numberOfDays): DateTime | ||
{ | ||
$dtBuilder = new DateTimeBuilder(); | ||
$today = $dtBuilder->build(); | ||
|
||
return $today->sub(new DateInterval(sprintf('P%dD', $numberOfDays))); | ||
} | ||
} | ||
Oops, something went wrong.