Improve USB policy responsiveness and reliability
- Reduce USB policy polling from 60s to 15s for faster response - Add retry with exponential backoff (2s, 4s, 8s) on report failures - Add APPLYING state for real-time progress bar feedback - Check if policy is already applied locally before re-applying - Fix API schema to accept APPLYING status - Update agent to v0.1.9 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
23e7cf58ae
commit
b60255fe03
6 changed files with 115 additions and 14 deletions
|
|
@ -1215,11 +1215,9 @@ async fn check_and_apply_usb_policy(base_url: &str, token: &str) {
|
|||
}
|
||||
};
|
||||
|
||||
eprintln!("[agent] Aplicando politica USB: {}", policy_str);
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
use crate::usb_control::{apply_usb_policy, UsbPolicy};
|
||||
use crate::usb_control::{apply_usb_policy, get_current_policy, UsbPolicy};
|
||||
|
||||
let policy = match UsbPolicy::from_str(&policy_str) {
|
||||
Some(p) => p,
|
||||
|
|
@ -1230,6 +1228,26 @@ async fn check_and_apply_usb_policy(base_url: &str, token: &str) {
|
|||
}
|
||||
};
|
||||
|
||||
// Verifica se a politica ja esta aplicada localmente
|
||||
match get_current_policy() {
|
||||
Ok(current) if current == policy => {
|
||||
eprintln!("[agent] Politica USB ja esta aplicada localmente: {}", policy_str);
|
||||
report_usb_policy_status(base_url, token, "APPLIED", None, Some(policy_str)).await;
|
||||
return;
|
||||
}
|
||||
Ok(current) => {
|
||||
eprintln!("[agent] Politica atual: {:?}, esperada: {:?}", current, policy);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("[agent] Nao foi possivel ler politica atual: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("[agent] Aplicando politica USB: {}", policy_str);
|
||||
|
||||
// Reporta APPLYING para progress bar real no frontend
|
||||
report_usb_policy_status(base_url, token, "APPLYING", None, None).await;
|
||||
|
||||
match apply_usb_policy(policy) {
|
||||
Ok(result) => {
|
||||
eprintln!("[agent] Politica USB aplicada com sucesso: {:?}", result);
|
||||
|
|
@ -1265,8 +1283,38 @@ async fn report_usb_policy_status(
|
|||
current_policy,
|
||||
};
|
||||
|
||||
if let Err(e) = HTTP_CLIENT.post(&url).json(&report).send().await {
|
||||
eprintln!("[agent] Falha ao reportar status de politica USB: {e}");
|
||||
// Retry com backoff exponencial: 2s, 4s, 8s
|
||||
let delays = [2, 4, 8];
|
||||
let mut last_error = None;
|
||||
|
||||
for (attempt, delay_secs) in delays.iter().enumerate() {
|
||||
match HTTP_CLIENT.post(&url).json(&report).send().await {
|
||||
Ok(response) if response.status().is_success() => {
|
||||
if attempt > 0 {
|
||||
eprintln!("[agent] Report de politica USB enviado na tentativa {}", attempt + 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
Ok(response) => {
|
||||
last_error = Some(format!("HTTP {}", response.status()));
|
||||
}
|
||||
Err(e) => {
|
||||
last_error = Some(e.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if attempt < delays.len() - 1 {
|
||||
eprintln!(
|
||||
"[agent] Falha ao reportar politica USB (tentativa {}), retentando em {}s...",
|
||||
attempt + 1,
|
||||
delay_secs
|
||||
);
|
||||
tokio::time::sleep(Duration::from_secs(*delay_secs)).await;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(err) = last_error {
|
||||
eprintln!("[agent] Falha ao reportar status de politica USB apos 3 tentativas: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1344,7 +1392,7 @@ impl AgentRuntime {
|
|||
|
||||
let mut heartbeat_ticker = tokio::time::interval(Duration::from_secs(interval));
|
||||
heartbeat_ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
|
||||
let mut usb_ticker = tokio::time::interval(Duration::from_secs(60));
|
||||
let mut usb_ticker = tokio::time::interval(Duration::from_secs(15));
|
||||
usb_ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
|
||||
|
||||
loop {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue