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 protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$io = new SymfonyStyle($input, $output);
$em = $this->entityManager; $em = $this->entityManager;
$repo = $em->getRepository(User::class); $repo = $em->getRepository(User::class);
$chunkSize = 100; $repo->createQueryBuilder('u')
$count = $repo->count([]); ->update()
->set('u.log', 0)
->where('u.log = 1')
->getQuery()
->execute();
$io->progressStart($count); $repo->createQueryBuilder('u')
for ($i = 0; $i < $count; $i += $chunkSize) { ->update()
$data = $repo->findBy([], null, $chunkSize, $i); ->set('u.log', 1)
if ($data == []) { ->where('u.score > 900 OR u.score < 100')
break; ->getQuery()
} ->execute();
/** @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();
return Command::SUCCESS; return Command::SUCCESS;
} }