Ported UpdateSubscriptionsCommand.
This commit is contained in:
parent
f700b0c3c3
commit
7b465f46e3
|
@ -1,77 +1,40 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace src\PointToolsBundle\Command;
|
namespace App\Command;
|
||||||
|
|
||||||
|
use App\Repository\UserRepository;
|
||||||
|
use App\Entity\{Subscription, User};
|
||||||
|
use App\Exception\Api\UserNotFoundException;
|
||||||
|
use App\Service\Api\UserApi;
|
||||||
|
use App\Service\SubscriptionsManager;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use src\PointToolsBundle\Entity\User;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use src\PointToolsBundle\Service\SubscriptionsManager;
|
|
||||||
use src\PointToolsBundle\Entity\{Subscription};
|
|
||||||
use src\PointToolsBundle\Exception\Api\UserNotFoundException;
|
|
||||||
use src\PointToolsBundle\Repository\UserRepository;
|
|
||||||
use src\PointToolsBundle\Service\{Api\UserApi};
|
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Helper\ProgressBar;
|
|
||||||
use Symfony\Component\Console\Input\{InputInterface, InputOption};
|
use Symfony\Component\Console\Input\{InputInterface, InputOption};
|
||||||
|
use Symfony\Component\Console\Helper\ProgressBar;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
/**
|
#[AsCommand(name: 'app:subscriptions:update', description: 'Update subscriptions of users subscribed to service')]
|
||||||
* @todo https://symfony.com/doc/current/console/lockable_trait.html
|
|
||||||
*/
|
|
||||||
class UpdateSubscriptionsCommand extends Command
|
class UpdateSubscriptionsCommand extends Command
|
||||||
{
|
{
|
||||||
/** @var EntityManagerInterface */
|
|
||||||
private $em;
|
|
||||||
|
|
||||||
/** @var LoggerInterface */
|
|
||||||
private $logger;
|
|
||||||
|
|
||||||
/** @var UserRepository */
|
|
||||||
private $userRepo;
|
|
||||||
|
|
||||||
/** @var InputInterface */
|
|
||||||
private $input;
|
|
||||||
|
|
||||||
/** @var UserApi */
|
|
||||||
private $api;
|
|
||||||
|
|
||||||
/** @var int */
|
|
||||||
private $apiDelay = 500000;
|
|
||||||
|
|
||||||
/** @var int */
|
|
||||||
private $appUserId;
|
|
||||||
|
|
||||||
/** @var SubscriptionsManager */
|
|
||||||
private $subscriptionManager;
|
|
||||||
|
|
||||||
/** @var ProgressBar */
|
|
||||||
private $progress;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
LoggerInterface $logger,
|
private readonly LoggerInterface $logger,
|
||||||
UserRepository $userRepo,
|
private readonly UserRepository $userRepo,
|
||||||
UserApi $api,
|
private readonly UserApi $api,
|
||||||
SubscriptionsManager $subscriptionManager,
|
private readonly SubscriptionsManager $subscriptionManager,
|
||||||
int $apiDelay,
|
private readonly int $apiDelay,
|
||||||
int $appUserId
|
private readonly int $appUserId
|
||||||
) {
|
) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
$this->em = $em;
|
|
||||||
$this->logger = $logger;
|
|
||||||
$this->userRepo = $userRepo;
|
|
||||||
$this->api = $api;
|
|
||||||
$this->subscriptionManager = $subscriptionManager;
|
|
||||||
$this->apiDelay = $apiDelay;
|
|
||||||
$this->appUserId = $appUserId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure()
|
protected function configure()
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->setName('point:update:subscriptions')
|
|
||||||
->setDescription('Update subscriptions of users subscribed to service')
|
|
||||||
->addOption(
|
->addOption(
|
||||||
'all-users',
|
'all-users',
|
||||||
null,
|
null,
|
||||||
|
@ -87,46 +50,46 @@ class UpdateSubscriptionsCommand extends Command
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$this->input = $input;
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
|
||||||
$this->logger->debug('UpdateSubscriptionsCommand started.');
|
$this->logger->debug('UpdateSubscriptionsCommand started.');
|
||||||
|
|
||||||
$this->progress = new ProgressBar($output);
|
$progress = $io->createProgressBar();
|
||||||
$this->progress->setFormat('debug');
|
$progress->setFormat(ProgressBar::FORMAT_DEBUG);
|
||||||
|
|
||||||
if (!$input->getOption('check-only')) { // Beginning transaction for all changes
|
if (!$input->getOption('check-only')) { // Beginning transaction for all changes
|
||||||
$this->em->beginTransaction();
|
$this->em->beginTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$usersForUpdate = $this->getUsersForUpdate();
|
$usersForUpdate = $this->getUsersForUpdate($input);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->logger->error('Error while getting service subscribers', ['exception' => get_class($e), 'message' => $e->getMessage()]);
|
$this->logger->error('Error while getting service subscribers', ['exception' => get_class($e), 'message' => $e->getMessage()]);
|
||||||
|
|
||||||
return 1;
|
return Command::FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 === count($usersForUpdate)) {
|
if (0 === count($usersForUpdate)) {
|
||||||
$this->logger->info('No local subscribers. Finishing.');
|
$this->logger->info('No local subscribers. Finishing.');
|
||||||
|
|
||||||
return 0;
|
return Command::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->logger->info('Processing users subscribers');
|
$this->logger->info('Processing users subscribers');
|
||||||
$this->progress->start(count($usersForUpdate));
|
$progress->start(count($usersForUpdate));
|
||||||
|
|
||||||
foreach ($usersForUpdate as $user) {
|
foreach ($usersForUpdate as $user) {
|
||||||
usleep($this->apiDelay);
|
usleep($this->apiDelay);
|
||||||
|
|
||||||
$this->progress->advance();
|
$progress->advance();
|
||||||
$this->logger->info('Processing @'.$user->getLogin());
|
$this->logger->info('Processing @'.$user->getLogin());
|
||||||
|
|
||||||
$this->updateUser($user);
|
$this->updateUser($user);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->progress->finish();
|
$progress->finish();
|
||||||
|
|
||||||
// Flushing all changes at once to the database
|
// Flushing all changes at once to the database
|
||||||
if (!$input->getOption('check-only')) {
|
if (!$input->getOption('check-only')) {
|
||||||
|
@ -136,7 +99,7 @@ class UpdateSubscriptionsCommand extends Command
|
||||||
|
|
||||||
$this->logger->debug('Finished');
|
$this->logger->debug('Finished');
|
||||||
|
|
||||||
return 0;
|
return Command::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateUser(User $user): void
|
private function updateUser(User $user): void
|
||||||
|
@ -183,11 +146,11 @@ class UpdateSubscriptionsCommand extends Command
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getUsersForUpdate(): array
|
private function getUsersForUpdate(InputInterface $input): array
|
||||||
{
|
{
|
||||||
$usersForUpdate = [];
|
$usersForUpdate = [];
|
||||||
|
|
||||||
if ($this->input->getOption('all-users')) {
|
if ($input->getOption('all-users')) {
|
||||||
$usersForUpdate = $this->userRepo->findBy(['removed' => false]);
|
$usersForUpdate = $this->userRepo->findBy(['removed' => false]);
|
||||||
} else {
|
} else {
|
||||||
/** @var User $serviceUser */
|
/** @var User $serviceUser */
|
||||||
|
@ -258,4 +221,4 @@ class UpdateSubscriptionsCommand extends Command
|
||||||
|
|
||||||
return $usersForUpdate;
|
return $usersForUpdate;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue