feat(service): Ajoute la mise en cache pour améliorer les performances des requêtes API OVH et Google.
```
This commit is contained in:
Serreau Jovann
2025-11-06 08:11:34 +01:00
parent e738753a6a
commit a34589721f
2 changed files with 79 additions and 64 deletions

View File

@@ -10,7 +10,9 @@ use Google\Cloud\Compute\V1\GetInstanceRequest;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\ListInstancesRequest;
use Google\Cloud\Compute\V1\NetworkInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Cache\CacheInterface;
class ComputeEngineClient
{
@@ -20,6 +22,7 @@ class ComputeEngineClient
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly CacheInterface $cache,
KernelInterface $kernel
) {
$credentialsPath = $kernel->getProjectDir() . "/account.json";
@@ -39,45 +42,48 @@ class ComputeEngineClient
*/
public function list(): array
{
$request = (new ListInstancesRequest())
->setProject($this->projectId)
->setZone($this->zone);
return $this->cache->get('list_server_google',function (){
$instancesList = $this->client->list($request);
$request = (new ListInstancesRequest())
->setProject($this->projectId)
->setZone($this->zone);
$instances = [];
/** @var Instance $instance */
foreach ($instancesList as $instance) {
if (str_contains($instance->getName(), 'srv-')) {
/** @var NetworkInterface $network */
$network = $instance->getNetworkInterfaces()[0];
/** @var AccessConfig $accessConfig */
$accessConfig = $network->getAccessConfigs()[0];
$instancesList = $this->client->list($request);
$compute = $this->entityManager->getRepository(Compute::class)
->findOneBy(['instanceId' => $instance->getId()]);
$instances = [];
/** @var Instance $instance */
foreach ($instancesList as $instance) {
if (str_contains($instance->getName(), 'srv-')) {
/** @var NetworkInterface $network */
$network = $instance->getNetworkInterfaces()[0];
/** @var AccessConfig $accessConfig */
$accessConfig = $network->getAccessConfigs()[0];
if (!$compute instanceof Compute) {
$compute = new Compute();
$compute->setInstanceId($instance->getId());
$compute->setZone(str_replace(
"https://www.googleapis.com/compute/v1/projects/{$this->projectId}/zones/",
'',
$instance->getZone()
));
$compute->setInternalIp($network->getNetworkIP());
$compute->setExternalIp($accessConfig->getNatIP());
$compute->setStatus('down');
$compute = $this->entityManager->getRepository(Compute::class)
->findOneBy(['instanceId' => $instance->getId()]);
if (!$compute instanceof Compute) {
$compute = new Compute();
$compute->setInstanceId($instance->getId());
$compute->setZone(str_replace(
"https://www.googleapis.com/compute/v1/projects/{$this->projectId}/zones/",
'',
$instance->getZone()
));
$compute->setInternalIp($network->getNetworkIP());
$compute->setExternalIp($accessConfig->getNatIP());
$compute->setStatus('down');
}
$this->entityManager->persist($compute);
$instances[] = $compute;
}
$this->entityManager->persist($compute);
$instances[] = $compute;
}
}
$this->entityManager->flush();
$this->entityManager->flush();
return $instances;
return $instances;
});
}
/**
@@ -85,16 +91,19 @@ class ComputeEngineClient
*/
public function detail(Compute $compute): Compute
{
$request = (new GetInstanceRequest())
->setInstance($compute->getInstanceId())
->setProject($this->projectId)
->setZone($this->zone);
return $this->cache->get('list_server_google_'.$compute->getId(),function () use ($compute) {
$instance = $this->client->get($request);
$request = (new GetInstanceRequest())
->setInstance($compute->getInstanceId())
->setProject($this->projectId)
->setZone($this->zone);
$compute->setStatus($instance->getStatus());
$compute->name = $instance->getName();
$instance = $this->client->get($request);
return $compute;
$compute->setStatus($instance->getStatus());
$compute->name = $instance->getName();
return $compute;
});
}
}

View File

@@ -5,12 +5,14 @@ namespace App\Service\Ovh;
use App\Entity\Compute;
use Doctrine\ORM\EntityManagerInterface;
use Ovh\Api;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Contracts\Cache\CacheInterface;
class Client
{
private Api $ovhClient;
public function __construct(private readonly EntityManagerInterface $em)
public function __construct(private readonly CacheInterface $cache,private readonly EntityManagerInterface $em)
{
$this->ovhClient = new Api(
$_ENV['OVH_KEY'] ?? '',
@@ -51,36 +53,40 @@ class Client
public function servers()
{
$list =[];
$servers = $this->ovhClient->get('/dedicated/server');
foreach ($servers as $server) {
$compute = $this->em->getRepository(Compute::class)
->findOneBy(['instanceId' => "ovh|".$server]);
return $this->cache->get('list_server_ovh',function () {
$list = [];
$servers = $this->ovhClient->get('/dedicated/server');
foreach ($servers as $server) {
$compute = $this->em->getRepository(Compute::class)
->findOneBy(['instanceId' => "ovh|" . $server]);
$detail = $this->ovhClient->get('/dedicated/server/' . $server);
if (!$compute instanceof Compute) {
$compute = new Compute();
$compute->setInstanceId("ovh|".$server);
$compute->setZone($detail['region']."|".$detail['rack']);
$compute->setInternalIp($detail['ip']);
$compute->setExternalIp($detail['ip']);
$compute->setStatus('down');
$detail = $this->ovhClient->get('/dedicated/server/' . $server);
if (!$compute instanceof Compute) {
$compute = new Compute();
$compute->setInstanceId("ovh|" . $server);
$compute->setZone($detail['region'] . "|" . $detail['rack']);
$compute->setInternalIp($detail['ip']);
$compute->setExternalIp($detail['ip']);
$compute->setStatus('down');
}
$this->em->persist($compute);
$list[] = $compute;
}
$this->em->persist($compute);
$list[] = $compute;
}
$this->em->flush();
$this->em->flush();
return $list;
return $list;
});
}
public function detail(Compute $compute)
{
$c = explode("|",$compute->getInstanceId());
$detail = $this->ovhClient->get('/dedicated/server/' . $c[1]);
$compute->setStatus($detail['state'] == "ok"?"RUNNING":"DOWN");
$compute->name = $c[1];
$compute->type = $c[0];
return $compute;
return $this->cache->get('list_server_ovh_'.$compute->getId(),function () use ($compute) {
$c = explode("|", $compute->getInstanceId());
$detail = $this->ovhClient->get('/dedicated/server/' . $c[1]);
$compute->setStatus($detail['state'] == "ok" ? "RUNNING" : "DOWN");
$compute->name = $c[1];
$compute->type = $c[0];
return $compute;
});
}
}