Files
portal-api/app/Services/Hosting/HostingServerSelector.php
baodev0andClaude Sonnet 5.5 55a834a2f8 feat: nhắc gia hạn QR theo dịch vụ, failover server hosting, CCCD admin, log cấp phát dịch vụ
- Mail campaign: mẫu nhắc gia hạn thanh toán QR, chọn dịch vụ + chu kỳ từng dòng (renewal_services)
- Hosting: nhiều server Plesk cho một gói (service_plan_servers), chuyển server khi server chết
- Admin xem/sửa giấy tờ định danh khách, tuỳ chọn gia hạn cho khách, nhật ký cấp phát dịch vụ
- Đánh dấu mẫu email dịch vụ là non-marketing

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-30 18:36:41 +07:00

70 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Hosting;
use App\Models\DnsServer;
use App\Models\ServicePlan;
use App\Services\Plesk\PleskService;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Throwable;
class HostingServerSelector
{
private const HEALTH_TTL_SECONDS = 60;
private const PROBE_TIMEOUT_SECONDS = 5;
public function __construct(private readonly PleskService $plesk)
{
}
public function candidates(ServicePlan $plan): Collection
{
return $plan->servers()->get()
->filter(fn (DnsServer $server) => $server->status === 'activated' && $server->servesHosting())
->values();
}
public function pick(ServicePlan $plan, array $excludeIds = []): ?DnsServer
{
return $this->candidates($plan)->first(
fn (DnsServer $server) => ! in_array($server->id, $excludeIds, true) && $this->isHealthy($server)
);
}
public function hasHealthyServer(ServicePlan $plan): bool
{
return $this->pick($plan) !== null;
}
public function isHealthy(DnsServer $server): bool
{
return Cache::remember($this->cacheKey($server), self::HEALTH_TTL_SECONDS, fn () => $this->probe($server));
}
public function forget(DnsServer $server): void
{
Cache::forget($this->cacheKey($server));
}
private function probe(DnsServer $server): bool
{
try {
return $this->plesk
->client($server, self::PROBE_TIMEOUT_SECONDS)
->get($this->plesk->url($server, 'server'))
->successful();
} catch (Throwable) {
return false;
}
}
private function cacheKey(DnsServer $server): string
{
return 'hosting-server-health:' . $server->id;
}
}