Добавлено небуферизируемое подключение к базе данных для решения проблемы с each и batch запросами

This commit is contained in:
ErickSkrauch
2017-11-15 00:03:38 +03:00
parent dab0ab4cba
commit d175dcdaed
3 changed files with 18 additions and 29 deletions

View File

@@ -4,9 +4,8 @@ namespace console\controllers;
use common\models\AccountSession;
use common\models\EmailActivation;
use common\models\MinecraftAccessKey;
use Generator;
use Yii;
use yii\console\Controller;
use yii\db\ActiveQueryInterface;
class CleanupController extends Controller {
@@ -20,7 +19,7 @@ class CleanupController extends Controller {
]);
}
foreach ($this->each($query) as $email) {
foreach ($query->each(100, Yii::$app->unbufferedDb) as $email) {
/** @var EmailActivation $email */
$email->delete();
}
@@ -32,7 +31,7 @@ class CleanupController extends Controller {
$expiredMinecraftSessionsQuery = MinecraftAccessKey::find()
->andWhere(['<', 'updated_at', time() - 1209600]); // 2 weeks
foreach ($this->each($expiredMinecraftSessionsQuery) as $minecraftSession) {
foreach ($expiredMinecraftSessionsQuery->each(100, Yii::$app->unbufferedDb) as $minecraftSession) {
/** @var MinecraftAccessKey $minecraftSession */
$minecraftSession->delete();
}
@@ -62,31 +61,6 @@ 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) {
@@ -94,6 +68,7 @@ class CleanupController extends Controller {
$object = new $className;
/** @var \common\behaviors\EmailActivationExpirationBehavior $behavior */
$behavior = $object->getBehavior('expirationBehavior');
/** @noinspection NullPointerExceptionInspection */
$expiration = $behavior->expirationTimeout ?? 1123200; // 13d по умолчанию
// Приращаем 1 день, чтобы пользователи ещё могли получать сообщения об истечении кода активации
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */