2018-07-08 20:50:19 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace common\tasks;
|
|
|
|
|
|
|
|
use common\models\Account;
|
|
|
|
use Yii;
|
|
|
|
use yii\queue\RetryableJobInterface;
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
final readonly class ClearAccountSessions implements RetryableJobInterface {
|
2018-07-08 20:50:19 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function __construct(private int $accountId) {
|
2018-07-08 20:50:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int time to reserve in seconds
|
|
|
|
*/
|
|
|
|
public function getTtr(): int {
|
|
|
|
return 5 * 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $attempt number
|
|
|
|
* @param \Exception|\Throwable $error from last execute of the job
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canRetry($attempt, $error): bool {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \yii\queue\Queue $queue which pushed and is handling the job
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function execute($queue): void {
|
2020-06-12 02:57:02 +05:30
|
|
|
$account = Account::findOne(['id' => $this->accountId]);
|
2018-07-08 20:50:19 +05:30
|
|
|
if ($account === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
/** @var \common\models\AccountSession $authSession */
|
2018-07-08 20:50:19 +05:30
|
|
|
foreach ($account->getSessions()->each(100, Yii::$app->unbufferedDb) as $authSession) {
|
|
|
|
$authSession->delete();
|
|
|
|
}
|
|
|
|
|
2020-06-12 02:57:02 +05:30
|
|
|
/** @var \common\models\OauthSession $oauthSession */
|
2018-07-08 20:50:19 +05:30
|
|
|
foreach ($account->getOauthSessions()->each(100, Yii::$app->unbufferedDb) as $oauthSession) {
|
|
|
|
$oauthSession->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|