addSeconds(self::TIME_BUDGET_SECONDS); $lastId = Cache::get(self::CURSOR_CACHE_KEY); $completed = S3Storage::query() ->where('is_usable', false) ->when($lastId !== null, fn ($query) => $query->where('id', '>', $lastId)) ->chunkById(100, function ($storages) use ($deadline): bool { foreach ($storages as $storage) { if (now()->greaterThanOrEqualTo($deadline)) { return false; } try { $storage->testConnection(shouldSave: true); } catch (\Throwable) { // testConnection() keeps the storage unusable and notifies the team once. } Cache::forever(self::CURSOR_CACHE_KEY, $storage->id); } return true; }); if ($completed) { Cache::forget(self::CURSOR_CACHE_KEY); } } }