1
0
mirror of synced 2025-01-25 01:31: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;
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(
"<error>Failed to update statuses for account {$account->getCrmUrl()}[{$account->getId()}]</error>"
);
$output->writeln("<error>Error: {$e->getMessage()}</error>");
$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(
"<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>");
$this->release();
return 0;
return $commandResult;
}
}

View File

@ -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(
"<error>Failed to update configuration for account {$account->getCrmUrl()}[{$account->getId()}]</error>"
);
$output->writeln("<error>Error: {$e->getMessage()}</error>");
$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(
"<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>");
$this->release();
return 0;
return $commandResult;
}
}

View File

@ -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')
;
}
}

View File

@ -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;