2026-04-01 13:51:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use App\Enums\SocialAccount\Status;
|
|
|
|
|
use App\Jobs\RefreshSocialToken;
|
|
|
|
|
use App\Models\SocialAccount;
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
|
|
class RefreshExpiringTokens extends Command
|
|
|
|
|
{
|
|
|
|
|
protected $signature = 'social:refresh-expiring-tokens';
|
|
|
|
|
|
2026-07-02 23:32:55 +00:00
|
|
|
protected $description = 'Proactively refresh tokens expiring in the next 30 minutes (or already expired)';
|
2026-04-01 13:51:53 +00:00
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
|
{
|
|
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
|
|
SocialAccount::query()
|
|
|
|
|
->where('status', Status::Connected)
|
|
|
|
|
->whereNotNull('token_expires_at')
|
2026-07-02 23:32:55 +00:00
|
|
|
->where('token_expires_at', '<=', now()->addMinutes(30))
|
2026-04-01 13:51:53 +00:00
|
|
|
->chunk(50, function ($accounts) use (&$count) {
|
|
|
|
|
foreach ($accounts as $account) {
|
|
|
|
|
RefreshSocialToken::dispatch($account);
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->info("Dispatched {$count} token refresh jobs.");
|
|
|
|
|
}
|
|
|
|
|
}
|