From d175dcdaed5df93ecf50228566f5e24aa8d2e01c Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Wed, 15 Nov 2017 00:03:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BD=D0=B5=D0=B1=D1=83=D1=84=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D0=B7=D0=B8=D1=80=D1=83=D0=B5=D0=BC=D0=BE=D0=B5=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B4=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=20=D0=B1=D0=B0=D0=B7=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BF=D1=80=D0=BE=D0=B1=D0=BB=D0=B5=D0=BC=D1=8B?= =?UTF-8?q?=20=D1=81=20each=20=D0=B8=20batch=20=D0=B7=D0=B0=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=81=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autocompletion.php | 1 + common/config/config.php | 13 +++++++++ console/controllers/CleanupController.php | 33 +++-------------------- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/autocompletion.php b/autocompletion.php index e0dce4d..3890cc7 100644 --- a/autocompletion.php +++ b/autocompletion.php @@ -16,6 +16,7 @@ class Yii extends \yii\BaseYii { * Class BaseApplication * Used for properties that are identical for both WebApplication and ConsoleApplication * + * @property \yii\db\Connection $unbufferedDb * @property \yii\swiftmailer\Mailer $mailer * @property \common\components\Redis\Connection $redis * @property \common\components\RabbitMQ\Component $amqp diff --git a/common/config/config.php b/common/config/config.php index b5d7a68..d334a5c 100644 --- a/common/config/config.php +++ b/common/config/config.php @@ -17,6 +17,19 @@ return [ 'mysql' => common\db\mysql\Schema::class, ], ], + 'unbufferedDb' => [ + 'class' => yii\db\Connection::class, + 'dsn' => 'mysql:host=' . (getenv('DB_HOST') ?: 'db') . ';dbname=' . getenv('DB_DATABASE'), + 'username' => getenv('DB_USER'), + 'password' => getenv('DB_PASSWORD'), + 'charset' => 'utf8', + 'attributes' => [ + PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false, + ], + 'schemaMap' => [ + 'mysql' => common\db\mysql\Schema::class, + ], + ], 'mailer' => [ 'class' => yii\swiftmailer\Mailer::class, 'viewPath' => '@common/mail', diff --git a/console/controllers/CleanupController.php b/console/controllers/CleanupController.php index 73e3f27..e2276e8 100644 --- a/console/controllers/CleanupController.php +++ b/console/controllers/CleanupController.php @@ -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 */