fix: rewrite app:update-all-users-cron

This commit is contained in:
b1ek 2024-10-08 15:06:22 +10:00
parent 1a3a306db5
commit 5e3851a6ca
Signed by: blek
GPG Key ID: A622C22C9BC616B2
1 changed files with 12 additions and 35 deletions

View File

@ -31,45 +31,22 @@ class UpdateAllUsersCronCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$em = $this->entityManager;
$repo = $em->getRepository(User::class);
$chunkSize = 100;
$count = $repo->count([]);
$repo->createQueryBuilder('u')
->update()
->set('u.log', 0)
->where('u.log = 1')
->getQuery()
->execute();
$io->progressStart($count);
for ($i = 0; $i < $count; $i += $chunkSize) {
$data = $repo->findBy([], null, $chunkSize, $i);
if ($data == []) {
break;
}
/** @var User */
foreach ($data as $user) {
$score = (int) $user->getScore();
if ($score < 100 || $score > 900) {
$user->setLog(true);
$log = new UserLog();
$log->setScore($user->getScore());
$log->setUserId($user->getId());
$em->persist($user);
$em->persist($log);
} else if ($user->getLog()) {
$user->setLog(false);
$em->persist($user);
}
}
$io->progressAdvance($chunkSize);
$em->flush();
$em->clear();
}
$io->progressFinish();
$repo->createQueryBuilder('u')
->update()
->set('u.log', 1)
->where('u.score > 900 OR u.score < 100')
->getQuery()
->execute();
return Command::SUCCESS;
}