added docker management
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 4s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m2s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 2m18s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m25s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m0s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s

This commit is contained in:
2025-10-03 20:42:35 +02:00
parent 2f5e2391f7
commit bfeb43f38a
8 changed files with 668 additions and 255 deletions

View File

@@ -1,5 +1,3 @@
/// # API Module
///
/// This module provides all HTTP communication between WatcherAgent and the backend server.
@@ -14,9 +12,11 @@
/// These functions are called from the main agent loop and background tasks. All network operations are asynchronous and robust to transient failures.
use std::time::Duration;
use crate::hardware::HardwareInfo;
use crate::models::{HeartbeatDto, IdResponse, MetricDto, RegistrationDto, ServerMessage, Acknowledgment};
use crate::docker::serverclientcomm::handle_server_message;
use crate::hardware::HardwareInfo;
use crate::models::{
Acknowledgment, HeartbeatDto, IdResponse, MetricDto, RegistrationDto, ServerMessage,
};
use anyhow::Result;
use reqwest::{Client, StatusCode};
@@ -57,7 +57,7 @@ pub async fn register_with_server(
// Prepare registration data
let registration = RegistrationDto {
id: server_id,
server_id: server_id,
ip_address: registered_ip.clone(),
cpu_type: hardware.cpu.name.clone().unwrap_or_default(),
cpu_cores: (hardware.cpu.cores).unwrap_or_default(),
@@ -224,10 +224,13 @@ pub async fn send_metrics(
///
/// # Returns
/// * `Result<(), Box<dyn Error + Send + Sync>>` - Ok if commands are handled successfully.
pub async fn listening_to_server(docker: &Docker, base_url: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
pub async fn listening_to_server(
docker: &Docker,
base_url: &str,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let url = format!("{}/api/message", base_url);
let client = reqwest::Client::new();
loop {
// Get message from server
let resp = client.get(&url).send().await;
@@ -238,20 +241,36 @@ pub async fn listening_to_server(docker: &Docker, base_url: &str) -> Result<(),
match response.json::<ServerMessage>().await {
Ok(msg) => {
// Acknowledge receipt immediately
if let Err(e) = send_acknowledgment(&client, base_url, &msg.message_id, "received", "Message received successfully").await {
if let Err(e) = send_acknowledgment(
&client,
base_url,
&msg.message_id,
"received",
"Message received successfully",
)
.await
{
eprintln!("Failed to send receipt acknowledgment: {}", e);
}
// Handle the message
let result = handle_server_message(docker, msg.clone()).await;
// Send execution result acknowledgment
let (status, details) = match result {
Ok(_) => ("success", "Message executed successfully".to_string()),
Err(e) => ("error", format!("Execution failed: {}", e)),
};
if let Err(e) = send_acknowledgment(&client, base_url, &msg.message_id, status, &details).await {
if let Err(e) = send_acknowledgment(
&client,
base_url,
&msg.message_id,
status,
&details,
)
.await
{
eprintln!("Failed to send execution acknowledgment: {}", e);
}
}
@@ -297,24 +316,26 @@ async fn send_acknowledgment(
details: &str,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let ack_url = format!("{}/api/acknowledge", base_url);
let acknowledgment = Acknowledgment {
message_id: message_id.to_string(),
status: status.to_string(),
details: details.to_string(),
};
let response = client
.post(&ack_url)
.json(&acknowledgment)
.send()
.await?;
let response = client.post(&ack_url).json(&acknowledgment).send().await?;
if response.status().is_success() {
println!("Acknowledgment sent successfully for message {}", message_id);
println!(
"Acknowledgment sent successfully for message {}",
message_id
);
} else {
eprintln!("Server returned error for acknowledgment: {}", response.status());
eprintln!(
"Server returned error for acknowledgment: {}",
response.status()
);
}
Ok(())
}
}