Добавлен функционал очистки устаревших MinecraftAccessKey

This commit is contained in:
ErickSkrauch 2017-09-27 19:52:28 +03:00
parent d114424906
commit ec0b25e88d
4 changed files with 34 additions and 6 deletions

View File

@ -31,11 +31,11 @@ class MinecraftAccessKey extends ActiveRecord {
const LIFETIME = 172800; // Ключ актуален в течение 2 дней
public static function tableName() {
public static function tableName(): string {
return '{{%minecraft_access_keys}}';
}
public function behaviors() {
public function behaviors(): array {
return [
[
'class' => TimestampBehavior::class,
@ -49,11 +49,11 @@ class MinecraftAccessKey extends ActiveRecord {
];
}
public function getAccount() : ActiveQuery {
public function getAccount(): ActiveQuery {
return $this->hasOne(Account::class, ['id' => 'account_id']);
}
public function isExpired() : bool {
public function isExpired(): bool {
return time() > $this->updated_at + self::LIFETIME;
}

View File

@ -2,6 +2,7 @@
namespace console\controllers;
use common\models\EmailActivation;
use common\models\MinecraftAccessKey;
use yii\console\Controller;
class CleanupController extends Controller {
@ -26,6 +27,19 @@ class CleanupController extends Controller {
return self::EXIT_CODE_NORMAL;
}
public function actionMinecraftSessions() {
/** @var \yii\db\BatchQueryResult|MinecraftAccessKey[] $expiredMinecraftSessions */
$expiredMinecraftSessions = MinecraftAccessKey::find()
->andWhere(['<', 'updated_at', time() - 1209600]) // 2 weeks
->each();
foreach ($expiredMinecraftSessions as $minecraftSession) {
$minecraftSession->delete();
}
return self::EXIT_CODE_NORMAL;
}
private function getEmailActivationsDurationsMap(): array {
$durationsMap = [];
foreach (EmailActivation::getClassMap() as $typeId => $className) {

View File

@ -1,2 +1,3 @@
# https://crontab.guru/every-day
0 0 * * * root /usr/local/bin/php /var/www/html/yii cleanup/email-keys >/dev/null 2>&1
0 1 * * * root /usr/local/bin/php /var/www/html/yii cleanup/minecraft-sessions >/dev/null 2>&1

View File

@ -2,8 +2,10 @@
namespace codeception\console\unit\controllers;
use common\models\EmailActivation;
use common\models\MinecraftAccessKey;
use console\controllers\CleanupController;
use tests\codeception\common\fixtures\EmailActivationFixture;
use tests\codeception\common\fixtures\MinecraftAccessKeyFixture;
use tests\codeception\console\unit\TestCase;
use Yii;
@ -12,17 +14,28 @@ class CleanupControllerTest extends TestCase {
public function _fixtures() {
return [
'emailActivations' => EmailActivationFixture::class,
'minecraftSessions' => MinecraftAccessKeyFixture::class
];
}
public function testActionAccessTokens() {
public function testActionEmailKeys() {
/** @var EmailActivation $expiredConfirmation */
$expiredConfirmation = $this->tester->grabFixture('emailActivations', 'deeplyExpiredConfirmation');
$controller = new CleanupController('cleanup', Yii::$app);
$this->assertEquals(0, $controller->actionEmailKeys());
$this->tester->cantSeeRecord(EmailActivation::className(), ['key' => $expiredConfirmation->key]);
$this->tester->cantSeeRecord(EmailActivation::class, ['key' => $expiredConfirmation->key]);
}
public function testActionMinecraftSessions() {
/** @var MinecraftAccessKey $expiredSession */
$expiredSession = $this->tester->grabFixture('minecraftSessions', 'expired-token');
$controller = new CleanupController('cleanup', Yii::$app);
$this->assertEquals(0, $controller->actionMinecraftSessions());
$this->tester->cantSeeRecord(MinecraftAccessKey::class, ['access_token' => $expiredSession->access_token]);
}
}