1
0
mirror of synced 2025-01-26 10:11:41 +03:00

Merge pull request #10 from pm14kas/account-select-fix-in-commands

Fixed account selection in StatusesCommand and UpdateModuleConfiguration
This commit is contained in:
Kruglov Kirill 2020-11-17 12:45:49 +03:00 committed by GitHub
commit 5ebe43db31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 125 additions and 56 deletions

View File

@ -2,8 +2,7 @@
namespace RetailCrm\DeliveryModuleBundle\Command; namespace RetailCrm\DeliveryModuleBundle\Command;
use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\Persistence\ObjectManager;
use RetailCrm\DeliveryModuleBundle\Exception\AbstractModuleException;
use RetailCrm\DeliveryModuleBundle\Service\AccountManager; use RetailCrm\DeliveryModuleBundle\Service\AccountManager;
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface; use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -16,6 +15,8 @@ class StatusesCommand extends Command
{ {
use LockableTrait; use LockableTrait;
const QUERY_MAX_RESULTS = 100;
/** /**
* @var ModuleManagerInterface * @var ModuleManagerInterface
*/ */
@ -26,6 +27,11 @@ class StatusesCommand extends Command
*/ */
private $accountManager; private $accountManager;
/**
* @var ObjectManager
*/
private $entityManager;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -34,13 +40,15 @@ class StatusesCommand extends Command
$this $this
->setName('statuses:update') ->setName('statuses:update')
->setDescription('Update statuses') ->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->moduleManager = $moduleManager;
$this->accountManager = $accountManager; $this->accountManager = $accountManager;
$this->entityManager = $entityManager;
parent::__construct(); parent::__construct();
} }
@ -60,40 +68,57 @@ class StatusesCommand extends Command
? (int) $input->getArgument('accountId') ? (int) $input->getArgument('accountId')
: null; : null;
$paginator = []; $accountQueryBuilder = $this->accountManager->getActiveQueryBuilder()
->andWhere('account.id > :lastId')
->setMaxResults(static::QUERY_MAX_RESULTS)
;
if (null !== $accountId) { if (null !== $accountId) {
$paginator = [$this->accountManager - find($accountId)]; $accountQueryBuilder
} else { ->andWhere('account.id = :accountId')
$accountQuery = $this->accountManager->getRepository() ->setParameter('accountId', $accountId)
->createQueryBuilder('account') ;
->where('account.active = true')
->andWhere('account.freeze != true')
->addOrderBy('account.id')
->getQuery()
->setFirstResult(0)
->setMaxResults(100);
$paginator = new Paginator($accountQuery);
} }
$accountQuery = $accountQueryBuilder->getQuery();
$commandResult = 0;
$count = 0; $count = 0;
foreach ($paginator as $account) { $lastId = 0;
try { while (true) {
$count += $this->moduleManager $accountQuery->setParameter('lastId', $lastId);
->setAccount($account)
->updateStatuses() $result = $accountQuery->getResult();
; if (empty($result)) {
} catch (AbstractModuleException $e) { break;
$output->writeln(
"<error>Failed to update statuses for account {$account->getCrmUrl()}[{$account->getId()}]</error>"
);
$output->writeln("<error>Error: {$e->getMessage()}</error>");
} }
foreach ($result as $account) {
$lastId = $account->getId();
try {
$count += $this->moduleManager
->setAccount($account)
->updateStatuses()
;
} catch (\Exception $e) {
$output->writeln(
"<error>Failed to update statuses for account {$account->getCrmUrl()}[{$account->getId()}]</error>"
);
$output->writeln("<error>Error: {$e->getMessage()}</error>");
$commandResult = 1;
}
}
$this->entityManager->clear();
gc_collect_cycles();
} }
$output->writeln("<info>{$count} statuses updated.</info>"); $output->writeln("<info>{$count} statuses updated.</info>");
$this->release(); $this->release();
return 0; return $commandResult;
} }
} }

View File

@ -2,7 +2,7 @@
namespace RetailCrm\DeliveryModuleBundle\Command; namespace RetailCrm\DeliveryModuleBundle\Command;
use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\Persistence\ObjectManager;
use RetailCrm\DeliveryModuleBundle\Service\AccountManager; use RetailCrm\DeliveryModuleBundle\Service\AccountManager;
use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface; use RetailCrm\DeliveryModuleBundle\Service\ModuleManagerInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
@ -15,6 +15,8 @@ class UpdateModuleCommand extends Command
{ {
use LockableTrait; use LockableTrait;
const QUERY_MAX_RESULTS = 100;
/** /**
* @var ModuleManagerInterface * @var ModuleManagerInterface
*/ */
@ -25,6 +27,11 @@ class UpdateModuleCommand extends Command
*/ */
private $accountManager; private $accountManager;
/**
* @var ObjectManager
*/
private $entityManager;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -33,13 +40,15 @@ class UpdateModuleCommand extends Command
$this $this
->setName('module:update') ->setName('module:update')
->setDescription('Update module') ->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->moduleManager = $moduleManager;
$this->accountManager = $accountManager; $this->accountManager = $accountManager;
$this->entityManager = $entityManager;
parent::__construct(); parent::__construct();
} }
@ -59,41 +68,59 @@ class UpdateModuleCommand extends Command
? $input->getArgument('accountId') ? $input->getArgument('accountId')
: null; : null;
$paginator = []; $accountQueryBuilder = $this->accountManager->getActiveQueryBuilder()
->andWhere('account.id > :lastId')
->setMaxResults(static::QUERY_MAX_RESULTS)
;
if (null !== $accountId) { if (null !== $accountId) {
$paginator = [$this->accountManager->find($accountId)]; $accountQueryBuilder
} else { ->andWhere('account.id = :accountId')
$accountQuery = $this->accountManager->getRepository() ->setParameter('accountId', $accountId)
->createQueryBuilder('account') ;
->where('account.active = true')
->andWhere('account.freeze != true')
->addOrderBy('account.id')
->getQuery()
->setFirstResult(0)
->setMaxResults(100);
$paginator = new Paginator($accountQuery);
} }
$accountQuery = $accountQueryBuilder->getQuery();
$commandResult = 0;
$count = 0; $count = 0;
foreach ($paginator as $account) {
try { $lastId = 0;
$this->moduleManager while (true) {
->setAccount($account) $accountQuery->setParameter('lastId', $lastId);
->updateModuleConfiguration()
; $result = $accountQuery->getResult();
++$count; if (empty($result)) {
} catch (\Exception $e) { break;
$output->writeln(
"<error>Failed to update configuration for account {$account->getCrmUrl()}[{$account->getId()}]</error>"
);
$output->writeln("<error>Error: {$e->getMessage()}</error>");
} }
foreach ($result as $account) {
$lastId = $account->getId();
try {
$this->moduleManager
->setAccount($account)
->updateModuleConfiguration()
;
++$count;
} catch (\Exception $e) {
$output->writeln(
"<error>Failed to update configuration for account {$account->getCrmUrl()}[{$account->getId()}]</error>"
);
$output->writeln("<error>Error: {$e->getMessage()}</error>");
$commandResult = 1;
}
}
$this->entityManager->clear();
gc_collect_cycles();
} }
$output->writeln("<info>{$count} modules updated.</info>"); $output->writeln("<info>{$count} modules updated.</info>");
$this->release(); $this->release();
return 0; return $commandResult;
} }
} }

View File

@ -52,4 +52,13 @@ class AccountManager
{ {
return $this->entityManager->getRepository($this->class); 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')
;
}
} }

View File

@ -8,8 +8,16 @@ use RetailCrm\DeliveryModuleBundle\Model\Entity\DeliveryOrder;
class DeliveryOrderManager class DeliveryOrderManager
{ {
/**
* @var string
*/
protected $class; protected $class;
/**
* @var ObjectManager
*/
protected $entityManager;
public function __construct(string $deliveryOrderClass, ObjectManager $entityManager) public function __construct(string $deliveryOrderClass, ObjectManager $entityManager)
{ {
$this->class = $deliveryOrderClass; $this->class = $deliveryOrderClass;