diff --git a/internal/service/task.go b/internal/service/task.go index 38553a0..b4a6bcc 100644 --- a/internal/service/task.go +++ b/internal/service/task.go @@ -9,6 +9,7 @@ import ( "path/filepath" "strings" "sync" + "time" "3588AdminBackend/internal/config" "3588AdminBackend/internal/models" @@ -307,13 +308,26 @@ func (s *TaskService) executeOnDevice(task *models.Task, did string) { s.updateDeviceStatus(task.ID, did, models.TaskFailed, 0, "invalid payload: "+err.Error()) return } - _, code, err := s.agent.Do("PUT", dev.IP, dev.AgentPort, "/v1/config", body) - if err != nil { - s.updateDeviceStatus(task.ID, did, models.TaskFailed, 0, err.Error()) + // Retry config apply up to 3 times (media-server may be slow to reload). + var lastErr error + var lastCode int + for attempt := 0; attempt < 3; attempt++ { + if attempt > 0 { + time.Sleep(time.Duration(attempt) * 3 * time.Second) + s.updateDeviceStatus(task.ID, did, models.TaskRunning, float64(attempt)/3.0, fmt.Sprintf("重试 %d/2", attempt)) + } + _, code, err := s.agent.Do("PUT", dev.IP, dev.AgentPort, "/v1/config", body) + if err == nil && code < 400 { + lastErr = nil; lastCode = code; break + } + lastErr = err; lastCode = code + } + if lastErr != nil { + s.updateDeviceStatus(task.ID, did, models.TaskFailed, 0, lastErr.Error()) return } - if code >= 400 { - s.updateDeviceStatus(task.ID, did, models.TaskFailed, 0, fmt.Sprintf("agent error: %d", code)) + if lastCode >= 400 { + s.updateDeviceStatus(task.ID, did, models.TaskFailed, 0, fmt.Sprintf("agent error: %d", lastCode)) return } s.updateDeviceStatus(task.ID, did, models.TaskSuccess, 1.0, "") diff --git a/internal/web/ui.go b/internal/web/ui.go index ba78cd5..9c2d0d2 100644 --- a/internal/web/ui.go +++ b/internal/web/ui.go @@ -1192,11 +1192,11 @@ func (u *UI) actionConsoleSave(w http.ResponseWriter, r *http.Request) { } if len(taskIDs) == 1 { - http.Redirect(w, r, "/ui/console?task="+taskIDs[0], http.StatusFound) + http.Redirect(w, r, "/ui/console?msg="+url.QueryEscape("正在下发配置...")+"&task="+taskIDs[0], http.StatusFound) return } if len(taskIDs) > 1 { - http.Redirect(w, r, "/ui/console?task="+taskIDs[0]+"&msg="+url.QueryEscape(fmt.Sprintf("已为 %d 台设备创建下发任务", len(taskIDs))), http.StatusFound) + http.Redirect(w, r, "/ui/console?msg="+url.QueryEscape(fmt.Sprintf("正在为 %d 台设备下发配置...", len(taskIDs))), http.StatusFound) return } if len(errs) > 0 { diff --git a/internal/web/ui/templates/console.html b/internal/web/ui/templates/console.html index 73e7c65..5cfcfc7 100644 --- a/internal/web/ui/templates/console.html +++ b/internal/web/ui/templates/console.html @@ -375,11 +375,17 @@ }); }); - // Show saved task info from URL param + // Show saved task info and auto-refresh from URL param (function() { var m = location.search.match(/[?&]task=([^&]+)/); if (m) { - document.getElementById('console-status').innerHTML = '✅ 任务 ' + m[1].substring(0,8) + ' 已创建'; + document.getElementById('console-status').innerHTML = '⏳ 任务 ' + m[1].substring(0,8) + ' 执行中,页面将自动刷新...'; + setTimeout(function(){ location.href = '/ui/console'; }, 6000); + } + var msg = location.search.match(/[?&]msg=([^&]+)/); + if (msg) { + document.getElementById('console-status').innerHTML = decodeURIComponent(msg[1]); + if (!m) setTimeout(function(){ document.getElementById('console-status').innerHTML = ''; }, 5000); } })(); })(); diff --git a/internal/web/ui/templates/dashboard.html b/internal/web/ui/templates/dashboard.html index 661d09a..b90e940 100644 --- a/internal/web/ui/templates/dashboard.html +++ b/internal/web/ui/templates/dashboard.html @@ -73,5 +73,10 @@ + {{end}} diff --git a/internal/web/ui/templates/task.html b/internal/web/ui/templates/task.html index 303dcfd..e7e1d66 100644 --- a/internal/web/ui/templates/task.html +++ b/internal/web/ui/templates/task.html @@ -144,5 +144,13 @@ function cssEscape(s){ return String(s).replace(/[^a-zA-Z0-9_-]/g, (c) => "\\"+c); } syncTaskStatus(); +// Auto-connect SSE and auto-refresh if task is in progress. +(function(){ + var s='{{.Task.Status}}'; + if(s==='running'||s==='pending'){ + startSSE(); + setTimeout(function(){location.reload()},8000); + } +})(); {{end}} diff --git a/internal/web/ui/templates/tasks.html b/internal/web/ui/templates/tasks.html index 4fd0fde..7727a81 100644 --- a/internal/web/ui/templates/tasks.html +++ b/internal/web/ui/templates/tasks.html @@ -1,5 +1,5 @@ {{define "tasks"}} -
+

执行历史