diff --git a/Command/StatusesCommand.php b/Command/StatusesCommand.php
index 3168b18..1a92722 100644
--- a/Command/StatusesCommand.php
+++ b/Command/StatusesCommand.php
@@ -2,8 +2,7 @@
namespace RetailCrm\DeliveryModuleBundle\Command;
-use Doctrine\ORM\Tools\Pagination\Paginator;
-use RetailCrm\DeliveryModuleBundle\Exception\AbstractModuleException;
+use Doctrine\Persistence\ObjectManager;
use RetailCrm\DeliveryModuleBundle\Service\AccountManager;
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
use Symfony\Component\Console\Command\Command;
@@ -16,6 +15,8 @@ class StatusesCommand extends Command
{
use LockableTrait;
+ const QUERY_MAX_RESULTS = 100;
+
/**
* @var ModuleManagerInterface
*/
@@ -26,6 +27,11 @@ class StatusesCommand extends Command
*/
private $accountManager;
+ /**
+ * @var ObjectManager
+ */
+ private $entityManager;
+
/**
* {@inheritdoc}
*/
@@ -34,13 +40,15 @@ class StatusesCommand extends Command
$this
->setName('statuses:update')
->setDescription('Update statuses')
- ->addArgument('accountId', InputArgument::OPTIONAL, 'Choose account, or make it for all');
+ ->addArgument('accountId', InputArgument::OPTIONAL, 'Choose account, or make it for all')
+ ;
}
- public function __construct(ModuleManagerInterface $moduleManager, AccountManager $accountManager)
+ public function __construct(ModuleManagerInterface $moduleManager, AccountManager $accountManager, ObjectManager $entityManager)
{
$this->moduleManager = $moduleManager;
$this->accountManager = $accountManager;
+ $this->entityManager = $entityManager;
parent::__construct();
}
@@ -60,40 +68,57 @@ class StatusesCommand extends Command
? (int) $input->getArgument('accountId')
: null;
- $paginator = [];
+ $accountQueryBuilder = $this->accountManager->getActiveQueryBuilder()
+ ->andWhere('account.id > :lastId')
+ ->setMaxResults(static::QUERY_MAX_RESULTS)
+ ;
+
if (null !== $accountId) {
- $paginator = [$this->accountManager - find($accountId)];
- } else {
- $accountQuery = $this->accountManager->getRepository()
- ->createQueryBuilder('account')
- ->where('account.active = true')
- ->andWhere('account.freeze != true')
- ->addOrderBy('account.id')
- ->getQuery()
- ->setFirstResult(0)
- ->setMaxResults(100);
- $paginator = new Paginator($accountQuery);
+ $accountQueryBuilder
+ ->andWhere('account.id = :accountId')
+ ->setParameter('accountId', $accountId)
+ ;
}
+ $accountQuery = $accountQueryBuilder->getQuery();
+
+ $commandResult = 0;
$count = 0;
- foreach ($paginator as $account) {
- try {
- $count += $this->moduleManager
- ->setAccount($account)
- ->updateStatuses()
- ;
- } catch (AbstractModuleException $e) {
- $output->writeln(
- "Failed to update statuses for account {$account->getCrmUrl()}[{$account->getId()}]"
- );
- $output->writeln("Error: {$e->getMessage()}");
+ $lastId = 0;
+ while (true) {
+ $accountQuery->setParameter('lastId', $lastId);
+
+ $result = $accountQuery->getResult();
+ if (empty($result)) {
+ break;
}
+
+ foreach ($result as $account) {
+ $lastId = $account->getId();
+
+ try {
+ $count += $this->moduleManager
+ ->setAccount($account)
+ ->updateStatuses()
+ ;
+ } catch (\Exception $e) {
+ $output->writeln(
+ "Failed to update statuses for account {$account->getCrmUrl()}[{$account->getId()}]"
+ );
+ $output->writeln("Error: {$e->getMessage()}");
+
+ $commandResult = 1;
+ }
+ }
+
+ $this->entityManager->clear();
+ gc_collect_cycles();
}
$output->writeln("{$count} statuses updated.");
$this->release();
- return 0;
+ return $commandResult;
}
}
diff --git a/Command/UpdateModuleCommand.php b/Command/UpdateModuleCommand.php
index a5e6632..e36be72 100644
--- a/Command/UpdateModuleCommand.php
+++ b/Command/UpdateModuleCommand.php
@@ -2,7 +2,7 @@
namespace RetailCrm\DeliveryModuleBundle\Command;
-use Doctrine\ORM\Tools\Pagination\Paginator;
+use Doctrine\Persistence\ObjectManager;
use RetailCrm\DeliveryModuleBundle\Service\AccountManager;
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
use Symfony\Component\Console\Command\Command;
@@ -15,6 +15,8 @@ class UpdateModuleCommand extends Command
{
use LockableTrait;
+ const QUERY_MAX_RESULTS = 100;
+
/**
* @var ModuleManagerInterface
*/
@@ -25,6 +27,11 @@ class UpdateModuleCommand extends Command
*/
private $accountManager;
+ /**
+ * @var ObjectManager
+ */
+ private $entityManager;
+
/**
* {@inheritdoc}
*/
@@ -33,13 +40,15 @@ class UpdateModuleCommand extends Command
$this
->setName('module:update')
->setDescription('Update module')
- ->addArgument('accountId', InputArgument::OPTIONAL, 'Choose account, or make it for all');
+ ->addArgument('accountId', InputArgument::OPTIONAL, 'Choose account, or make it for all')
+ ;
}
- public function __construct(ModuleManagerInterface $moduleManager, AccountManager $accountManager)
+ public function __construct(ModuleManagerInterface $moduleManager, AccountManager $accountManager, ObjectManager $entityManager)
{
$this->moduleManager = $moduleManager;
$this->accountManager = $accountManager;
+ $this->entityManager = $entityManager;
parent::__construct();
}
@@ -59,41 +68,59 @@ class UpdateModuleCommand extends Command
? $input->getArgument('accountId')
: null;
- $paginator = [];
+ $accountQueryBuilder = $this->accountManager->getActiveQueryBuilder()
+ ->andWhere('account.id > :lastId')
+ ->setMaxResults(static::QUERY_MAX_RESULTS)
+ ;
+
if (null !== $accountId) {
- $paginator = [$this->accountManager->find($accountId)];
- } else {
- $accountQuery = $this->accountManager->getRepository()
- ->createQueryBuilder('account')
- ->where('account.active = true')
- ->andWhere('account.freeze != true')
- ->addOrderBy('account.id')
- ->getQuery()
- ->setFirstResult(0)
- ->setMaxResults(100);
- $paginator = new Paginator($accountQuery);
+ $accountQueryBuilder
+ ->andWhere('account.id = :accountId')
+ ->setParameter('accountId', $accountId)
+ ;
}
+ $accountQuery = $accountQueryBuilder->getQuery();
+
+ $commandResult = 0;
$count = 0;
- foreach ($paginator as $account) {
- try {
- $this->moduleManager
- ->setAccount($account)
- ->updateModuleConfiguration()
- ;
- ++$count;
- } catch (\Exception $e) {
- $output->writeln(
- "Failed to update configuration for account {$account->getCrmUrl()}[{$account->getId()}]"
- );
- $output->writeln("Error: {$e->getMessage()}");
+
+ $lastId = 0;
+ while (true) {
+ $accountQuery->setParameter('lastId', $lastId);
+
+ $result = $accountQuery->getResult();
+ if (empty($result)) {
+ break;
}
+
+ foreach ($result as $account) {
+ $lastId = $account->getId();
+
+ try {
+ $this->moduleManager
+ ->setAccount($account)
+ ->updateModuleConfiguration()
+ ;
+ ++$count;
+ } catch (\Exception $e) {
+ $output->writeln(
+ "Failed to update configuration for account {$account->getCrmUrl()}[{$account->getId()}]"
+ );
+ $output->writeln("Error: {$e->getMessage()}");
+
+ $commandResult = 1;
+ }
+ }
+
+ $this->entityManager->clear();
+ gc_collect_cycles();
}
$output->writeln("{$count} modules updated.");
$this->release();
- return 0;
+ return $commandResult;
}
}
diff --git a/Service/AccountManager.php b/Service/AccountManager.php
index d9da37c..97d7f44 100644
--- a/Service/AccountManager.php
+++ b/Service/AccountManager.php
@@ -52,4 +52,13 @@ class AccountManager
{
return $this->entityManager->getRepository($this->class);
}
+
+ public function getActiveQueryBuilder()
+ {
+ return $this->getRepository()->createQueryBuilder('account')
+ ->where('account.isActive IS TRUE')
+ ->andWhere('account.isFreeze IS NOT TRUE')
+ ->orderBy('account.id')
+ ;
+ }
}
diff --git a/Service/DeliveryOrderManager.php b/Service/DeliveryOrderManager.php
index e68390f..4f8161c 100644
--- a/Service/DeliveryOrderManager.php
+++ b/Service/DeliveryOrderManager.php
@@ -8,8 +8,16 @@ use RetailCrm\DeliveryModuleBundle\Model\Entity\DeliveryOrder;
class DeliveryOrderManager
{
+ /**
+ * @var string
+ */
protected $class;
+ /**
+ * @var ObjectManager
+ */
+ protected $entityManager;
+
public function __construct(string $deliveryOrderClass, ObjectManager $entityManager)
{
$this->class = $deliveryOrderClass;