mirror of
https://github.com/elyby/accounts.git
synced 2024-11-30 02:32:26 +05:30
Добавлено небуферизируемое подключение к базе данных для решения проблемы с each и batch запросами
This commit is contained in:
parent
dab0ab4cba
commit
d175dcdaed
@ -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
|
||||
|
@ -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',
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user