Skip to content

Commit

Permalink
Add user:reconstruct_user_data command to refresh UserData (Redis) …
Browse files Browse the repository at this point in the history
…cache

Command was opensourced based on the InternalModule version and
enhanced with the filter proposed in the related PR.

#13
  • Loading branch information
rootpd committed Oct 8, 2020
1 parent 7049c06 commit d783f92
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/UsersModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public function registerCommands(CommandsContainerInterface $commandsContainer)
$commandsContainer->registerCommand($this->getInstance(\Crm\UsersModule\Commands\UpdateLoginAttemptsCommand::class));
$commandsContainer->registerCommand($this->getInstance(\Crm\UsersModule\Commands\CheckEmailsCommand::class));
$commandsContainer->registerCommand($this->getInstance(\Crm\UsersModule\Commands\DisableUserCommand::class));
$commandsContainer->registerCommand($this->getInstance(\Crm\UsersModule\Commands\ReconstructUserDataCommand::class));
}

public function registerWidgets(WidgetManagerInterface $widgetManager)
Expand Down
83 changes: 83 additions & 0 deletions src/commands/ReconstructUserDataCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Crm\UsersModule\Commands;

use Crm\UsersModule\Repository\UsersRepository;
use Crm\UsersModule\User\UserData;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ReconstructUserDataCommand extends Command
{
private $usersRepository;

private $userData;

public function __construct(
UsersRepository $usersRepository,
UserData $userData
) {
parent::__construct();
$this->usersRepository = $usersRepository;
$this->userData = $userData;
}

protected function configure()
{
$this->setName('user:reconstruct_user_data')
->setDescription('Reconstruct user data (Redis) cache based on the actual data')
->addOption(
'user_ids',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'User IDs to refresh. If not provided, all users are refreshed.'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Reconstructing user data');
$output->writeln('');

$usersQuery = $this->usersRepository->getTable()
->where(['active' => true])
->where(':access_tokens.id IS NOT NULL')
->order('id ASC');

$userIdsFilter = $input->getOption('user_ids');
if (count($userIdsFilter)) {
$usersQuery->where('users.id IN (?)', $userIdsFilter);
}

$totalUsers = (clone $usersQuery)->count('*');
$progress = new ProgressBar($output, $totalUsers);
$progress->setFormat('debug');
$progress->start();

$step = 100;
$lastId = 0;

do {
$processed = 0;
$users = (clone $usersQuery)
->where('users.id > ?', $lastId)
->limit($step);

foreach ($users as $user) {
$this->userData->refreshUserTokens($user->id);
$processed++;
$lastId = $user->id;

$progress->advance();
}
} while ($processed > 0);

$progress->finish();
$output->writeln('');
return 0;
}
}
1 change: 1 addition & 0 deletions src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ services:
- Crm\UsersModule\Commands\GenerateAccessCommand
- Crm\UsersModule\Commands\CheckEmailsCommand
- Crm\UsersModule\Commands\DisableUserCommand
- Crm\UsersModule\Commands\ReconstructUserDataCommand
- Crm\UsersModule\Components\AddressWidget
- Crm\UsersModule\Components\UserLoginAttempts
- Crm\UsersModule\Components\UserPasswordChanges
Expand Down

0 comments on commit d783f92

Please sign in to comment.