Используем собственную реализацию each для выборки строк на удаление в cleanup контроллере

This commit is contained in:
ErickSkrauch 2017-11-14 19:49:51 +03:00
parent 85e432c897
commit e9208d8f21

View File

@ -4,24 +4,24 @@ namespace console\controllers;
use common\models\AccountSession; use common\models\AccountSession;
use common\models\EmailActivation; use common\models\EmailActivation;
use common\models\MinecraftAccessKey; use common\models\MinecraftAccessKey;
use Generator;
use yii\console\Controller; use yii\console\Controller;
use yii\db\ActiveQueryInterface;
class CleanupController extends Controller { class CleanupController extends Controller {
public function actionEmailKeys() { public function actionEmailKeys() {
$query = EmailActivation::find(); $query = EmailActivation::find();
$conditions = ['OR'];
foreach ($this->getEmailActivationsDurationsMap() as $typeId => $expiration) { foreach ($this->getEmailActivationsDurationsMap() as $typeId => $expiration) {
$conditions[] = [ $query->orWhere([
'AND', 'AND',
['type' => $typeId], ['type' => $typeId],
['<', 'created_at', time() - $expiration], ['<', 'created_at', time() - $expiration],
]; ]);
} }
/** @var \yii\db\BatchQueryResult|EmailActivation[] $expiredEmails */ foreach ($this->each($query) as $email) {
$expiredEmails = $query->andWhere($conditions)->each(); /** @var EmailActivation $email */
foreach ($expiredEmails as $email) {
$email->delete(); $email->delete();
} }
@ -29,12 +29,11 @@ class CleanupController extends Controller {
} }
public function actionMinecraftSessions() { public function actionMinecraftSessions() {
/** @var \yii\db\BatchQueryResult|MinecraftAccessKey[] $expiredMinecraftSessions */ $expiredMinecraftSessionsQuery = MinecraftAccessKey::find()
$expiredMinecraftSessions = MinecraftAccessKey::find() ->andWhere(['<', 'updated_at', time() - 1209600]); // 2 weeks
->andWhere(['<', 'updated_at', time() - 1209600]) // 2 weeks
->each();
foreach ($expiredMinecraftSessions as $minecraftSession) { foreach ($this->each($expiredMinecraftSessionsQuery) as $minecraftSession) {
/** @var MinecraftAccessKey $minecraftSession */
$minecraftSession->delete(); $minecraftSession->delete();
} }
@ -63,6 +62,31 @@ class CleanupController extends Controller {
return self::EXIT_CODE_NORMAL; 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 { private function getEmailActivationsDurationsMap(): array {
$durationsMap = []; $durationsMap = [];
foreach (EmailActivation::getClassMap() as $typeId => $className) { foreach (EmailActivation::getClassMap() as $typeId => $className) {