mirror of
https://github.com/elyby/accounts.git
synced 2024-11-09 15:02:19 +05:30
Используем собственную реализацию each для выборки строк на удаление в cleanup контроллере
This commit is contained in:
parent
85e432c897
commit
e9208d8f21
@ -4,24 +4,24 @@ namespace console\controllers;
|
||||
use common\models\AccountSession;
|
||||
use common\models\EmailActivation;
|
||||
use common\models\MinecraftAccessKey;
|
||||
use Generator;
|
||||
use yii\console\Controller;
|
||||
use yii\db\ActiveQueryInterface;
|
||||
|
||||
class CleanupController extends Controller {
|
||||
|
||||
public function actionEmailKeys() {
|
||||
$query = EmailActivation::find();
|
||||
$conditions = ['OR'];
|
||||
foreach ($this->getEmailActivationsDurationsMap() as $typeId => $expiration) {
|
||||
$conditions[] = [
|
||||
$query->orWhere([
|
||||
'AND',
|
||||
['type' => $typeId],
|
||||
['<', 'created_at', time() - $expiration],
|
||||
];
|
||||
]);
|
||||
}
|
||||
|
||||
/** @var \yii\db\BatchQueryResult|EmailActivation[] $expiredEmails */
|
||||
$expiredEmails = $query->andWhere($conditions)->each();
|
||||
foreach ($expiredEmails as $email) {
|
||||
foreach ($this->each($query) as $email) {
|
||||
/** @var EmailActivation $email */
|
||||
$email->delete();
|
||||
}
|
||||
|
||||
@ -29,12 +29,11 @@ class CleanupController extends Controller {
|
||||
}
|
||||
|
||||
public function actionMinecraftSessions() {
|
||||
/** @var \yii\db\BatchQueryResult|MinecraftAccessKey[] $expiredMinecraftSessions */
|
||||
$expiredMinecraftSessions = MinecraftAccessKey::find()
|
||||
->andWhere(['<', 'updated_at', time() - 1209600]) // 2 weeks
|
||||
->each();
|
||||
$expiredMinecraftSessionsQuery = MinecraftAccessKey::find()
|
||||
->andWhere(['<', 'updated_at', time() - 1209600]); // 2 weeks
|
||||
|
||||
foreach ($expiredMinecraftSessions as $minecraftSession) {
|
||||
foreach ($this->each($expiredMinecraftSessionsQuery) as $minecraftSession) {
|
||||
/** @var MinecraftAccessKey $minecraftSession */
|
||||
$minecraftSession->delete();
|
||||
}
|
||||
|
||||
@ -63,6 +62,31 @@ class CleanupController extends Controller {
|
||||
return self::EXIT_CODE_NORMAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Each function implementation, that allows you to iterate over values,
|
||||
* when in each iteration row removing from database. If you do not remove
|
||||
* value in iteration, then this will cause infinite loop.
|
||||
*
|
||||
* @param ActiveQueryInterface $query
|
||||
* @param int $size
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
private function each(ActiveQueryInterface $query, int $size = 100): Generator {
|
||||
$query = clone $query;
|
||||
$query->limit($size);
|
||||
while (true) {
|
||||
$rows = $query->all();
|
||||
if (empty($rows)) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($rows as $row) {
|
||||
yield $row;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getEmailActivationsDurationsMap(): array {
|
||||
$durationsMap = [];
|
||||
foreach (EmailActivation::getClassMap() as $typeId => $className) {
|
||||
|
Loading…
Reference in New Issue
Block a user