kick forever
CI/CD / test_and_check_app (push) Successful in 2m19s
CI/CD / Publish immutable image (push) Successful in 1m16s

This commit is contained in:
2026-09-24 14:38:18 +02:00
parent b190ff719e
commit 4dfe79ce5c
22 changed files with 271 additions and 113 deletions
-22
View File
@@ -1,22 +0,0 @@
<?php
namespace App\Jobs;
use App\Models\Player;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
class MarkInactivePlayers implements ShouldQueue
{
use Queueable;
public function handle(): void
{
Player::query()
->whereNull('inactive_at')
->where('last_seen_at', '<=', now()->subHour())
->update([
'inactive_at' => now(),
]);
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace App\Jobs;
use App\Enums\RoomStateChange;
use App\Events\RoomStateChanged;
use App\Models\Player;
use App\Models\Room;
use App\Services\Rooms\RoomRealtimeStateService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\DB;
class RemoveAbsentPlayers implements ShouldQueue
{
use Queueable;
public function handle(RoomRealtimeStateService $realtimeState): void
{
$cutoff = now()->subHour();
Player::query()
->where(function ($query) use ($cutoff): void {
$query->where('last_seen_at', '<=', $cutoff)
->orWhere(function ($query) use ($cutoff): void {
$query->whereNull('last_seen_at')
->where('created_at', '<=', $cutoff);
});
})
->select(['id', 'room_id'])
->chunkById(100, function ($players) use ($cutoff, $realtimeState): void {
foreach ($players as $player) {
$room = DB::transaction(function () use ($player, $cutoff): ?Room {
$room = Room::withTrashed()
->lockForUpdate()
->find($player->room_id);
if ($room === null) {
return null;
}
$currentPlayer = $room->players()
->lockForUpdate()
->find($player->id);
if ($currentPlayer === null) {
return null;
}
$lastActivity = $currentPlayer->last_seen_at ?? $currentPlayer->created_at;
if ($lastActivity === null || $lastActivity->greaterThan($cutoff)) {
return null;
}
$currentPlayer->delete();
$room->update([
'state_version' => $room->state_version + 1,
]);
return $room;
});
if ($room !== null && ! $room->trashed()) {
RoomStateChanged::dispatch(
$room->id,
$room->state_version,
RoomStateChange::PlayerKicked,
$realtimeState->for($room),
$player->id,
);
}
}
});
}
}