Integrate Coolify API client and module for managing Coolify services, enhancing automation and deployment processes.
This commit is contained in:
@@ -1018,6 +1018,7 @@ final class TruckwashEdgeAgent
|
||||
private int $lastHeartbeatAt = 0;
|
||||
private int $lastMachineSignalPollAt = 0;
|
||||
private int $lastMachineSignalMonitorRefreshAt = 0;
|
||||
private ?array $lastControlPlaneResponse = null;
|
||||
private string $agentInstanceId;
|
||||
|
||||
public function __construct(string $configPath)
|
||||
@@ -1283,6 +1284,7 @@ final class TruckwashEdgeAgent
|
||||
}
|
||||
|
||||
$heartbeatSucceededAt = date('c');
|
||||
$this->applyBrokerUrlFromControlPlaneResponse($this->lastControlPlaneResponse);
|
||||
$this->lastHeartbeatAt = time();
|
||||
$this->recordSuccessfulSync($heartbeatSucceededAt, [
|
||||
'last_heartbeat_success_at' => $heartbeatSucceededAt,
|
||||
@@ -2430,13 +2432,15 @@ final class TruckwashEdgeAgent
|
||||
|
||||
private function sendControlPlaneEvent(string $endpoint, array $payload, string $type): bool
|
||||
{
|
||||
$this->lastControlPlaneResponse = null;
|
||||
$brokerDispatch = $this->dispatchBrokerControlPlaneEvent($endpoint, $payload);
|
||||
if ($brokerDispatch === true && !$this->shouldPersistControlPlaneEventOverHttp($endpoint)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->http->post($endpoint, $payload, 20);
|
||||
$response = $this->http->post($endpoint, $payload, 20);
|
||||
$this->lastControlPlaneResponse = is_array($response) ? $response : null;
|
||||
$this->recordSuccessfulSync();
|
||||
return true;
|
||||
} catch (Throwable $throwable) {
|
||||
@@ -2457,6 +2461,29 @@ final class TruckwashEdgeAgent
|
||||
}
|
||||
}
|
||||
|
||||
private function applyBrokerUrlFromControlPlaneResponse(?array $response): void
|
||||
{
|
||||
if (!is_array($response)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$payload = is_array($response['data'] ?? null) ? (array)$response['data'] : $response;
|
||||
$brokerUrl = trim((string)($payload['broker_url'] ?? $payload['gateway']['broker_url'] ?? ''));
|
||||
if ($brokerUrl === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$current = trim((string)$this->config->get('brokerUrl'));
|
||||
if (rtrim($current, '/') === rtrim($brokerUrl, '/')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->config->set('brokerUrl', rtrim($brokerUrl, '/'));
|
||||
$this->config->save();
|
||||
$this->configureBrokerClient();
|
||||
$this->logger->info('Updated broker URL from control plane heartbeat response.');
|
||||
}
|
||||
|
||||
private function shouldPersistControlPlaneEventOverHttp(string $endpoint): bool
|
||||
{
|
||||
return preg_match('#/edge-agent/gateways/\d+/heartbeat$#', $endpoint) === 1;
|
||||
|
||||
Reference in New Issue
Block a user