fix: agent exec log error
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"baihu/internal/database"
|
||||
"baihu/internal/logger"
|
||||
"baihu/internal/models"
|
||||
"baihu/internal/services/tasks"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
@@ -350,7 +351,10 @@ func (s *AgentService) buildEnvVarsString(envIDs string) string {
|
||||
|
||||
// ReportResult Agent 上报执行结果
|
||||
func (s *AgentService) ReportResult(result *models.AgentTaskResult) error {
|
||||
taskExecutionService := NewTaskExecutionService()
|
||||
// 获取依赖的服务
|
||||
agentWSManager := GetAgentWSManager()
|
||||
sendStatsService := NewSendStatsService()
|
||||
taskExecutionService := tasks.NewTaskExecutionService(agentWSManager, sendStatsService)
|
||||
|
||||
// 使用统一的结果处理流程
|
||||
return taskExecutionService.ProcessAgentResult(result)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package services
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"sync"
|
||||
+22
-11
@@ -1,4 +1,4 @@
|
||||
package services
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"baihu/internal/constant"
|
||||
@@ -17,6 +17,16 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// SettingsService 接口定义(避免循环依赖)
|
||||
type SettingsService interface {
|
||||
Get(section, key string) string
|
||||
}
|
||||
|
||||
// EnvService 接口定义(避免循环依赖)
|
||||
type EnvService interface {
|
||||
GetEnvVarsByIDs(ids string) []string
|
||||
}
|
||||
|
||||
// ExecutionResult represents the result of a task execution
|
||||
type ExecutionResult struct {
|
||||
TaskID int
|
||||
@@ -36,6 +46,8 @@ type taskJob struct {
|
||||
type ExecutorService struct {
|
||||
taskService *TaskService
|
||||
taskExecutionService *TaskExecutionService
|
||||
settingsService SettingsService
|
||||
envService EnvService
|
||||
results []ExecutionResult
|
||||
runningTasks map[int]bool
|
||||
mu sync.RWMutex
|
||||
@@ -50,9 +62,8 @@ type ExecutorService struct {
|
||||
}
|
||||
|
||||
// NewExecutorService creates a new executor service
|
||||
func NewExecutorService(taskService *TaskService) *ExecutorService {
|
||||
func NewExecutorService(taskService *TaskService, taskExecutionService *TaskExecutionService, settingsService SettingsService, envService EnvService) *ExecutorService {
|
||||
// 从设置中读取调度配置
|
||||
settingsService := NewSettingsService()
|
||||
workerCount := getIntSetting(settingsService, constant.SectionScheduler, constant.KeyWorkerCount, 4)
|
||||
queueSize := getIntSetting(settingsService, constant.SectionScheduler, constant.KeyQueueSize, 100)
|
||||
rateInterval := getIntSetting(settingsService, constant.SectionScheduler, constant.KeyRateInterval, 200)
|
||||
@@ -61,7 +72,9 @@ func NewExecutorService(taskService *TaskService) *ExecutorService {
|
||||
|
||||
es := &ExecutorService{
|
||||
taskService: taskService,
|
||||
taskExecutionService: NewTaskExecutionService(),
|
||||
taskExecutionService: taskExecutionService,
|
||||
settingsService: settingsService,
|
||||
envService: envService,
|
||||
results: make([]ExecutionResult, 0, 100),
|
||||
runningTasks: make(map[int]bool),
|
||||
taskQueue: make(chan taskJob, queueSize),
|
||||
@@ -77,7 +90,7 @@ func NewExecutorService(taskService *TaskService) *ExecutorService {
|
||||
}
|
||||
|
||||
// getIntSetting 从设置中获取整数值
|
||||
func getIntSetting(s *SettingsService, section, key string, defaultVal int) int {
|
||||
func getIntSetting(s SettingsService, section, key string, defaultVal int) int {
|
||||
val := s.Get(section, key)
|
||||
if val == "" {
|
||||
return defaultVal
|
||||
@@ -128,10 +141,9 @@ func (es *ExecutorService) Reload() {
|
||||
logger.Info("[Executor] 已停止工作线程")
|
||||
|
||||
// 从设置中读取新配置
|
||||
settingsService := NewSettingsService()
|
||||
workerCount := getIntSetting(settingsService, constant.SectionScheduler, constant.KeyWorkerCount, 4)
|
||||
queueSize := getIntSetting(settingsService, constant.SectionScheduler, constant.KeyQueueSize, 100)
|
||||
rateInterval := getIntSetting(settingsService, constant.SectionScheduler, constant.KeyRateInterval, 200)
|
||||
workerCount := getIntSetting(es.settingsService, constant.SectionScheduler, constant.KeyWorkerCount, 4)
|
||||
queueSize := getIntSetting(es.settingsService, constant.SectionScheduler, constant.KeyQueueSize, 100)
|
||||
rateInterval := getIntSetting(es.settingsService, constant.SectionScheduler, constant.KeyRateInterval, 200)
|
||||
|
||||
// 重建 channel 和配置
|
||||
es.mu.Lock()
|
||||
@@ -228,8 +240,7 @@ func (es *ExecutorService) executeNormalTask(task *models.Task) *ExecutionResult
|
||||
}
|
||||
|
||||
// 加载环境变量
|
||||
envService := NewEnvService()
|
||||
envVars := envService.GetEnvVarsByIDs(task.Envs)
|
||||
envVars := es.envService.GetEnvVarsByIDs(task.Envs)
|
||||
|
||||
// 确定工作目录
|
||||
workDir := task.WorkDir
|
||||
+14
-5
@@ -1,4 +1,4 @@
|
||||
package services
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"baihu/internal/database"
|
||||
@@ -15,15 +15,22 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// AgentWSManager 接口定义(避免循环依赖)
|
||||
type AgentWSManager interface {
|
||||
SendToAgent(agentID uint, msgType string, data interface{}) error
|
||||
}
|
||||
|
||||
// TaskExecutionService 统一的任务执行服务
|
||||
type TaskExecutionService struct {
|
||||
taskLogService *TaskLogService
|
||||
agentWSManager AgentWSManager
|
||||
}
|
||||
|
||||
// NewTaskExecutionService 创建任务执行服务
|
||||
func NewTaskExecutionService() *TaskExecutionService {
|
||||
func NewTaskExecutionService(agentWSManager AgentWSManager, sendStatsService SendStatsService) *TaskExecutionService {
|
||||
return &TaskExecutionService{
|
||||
taskLogService: NewTaskLogService(),
|
||||
taskLogService: NewTaskLogService(sendStatsService),
|
||||
agentWSManager: agentWSManager,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,8 +134,10 @@ func (s *TaskExecutionService) executeRemote(req *TaskExecutionRequest) error {
|
||||
}
|
||||
|
||||
// 通过 WebSocket 发送立即执行命令给 Agent
|
||||
manager := GetAgentWSManager()
|
||||
err := manager.SendToAgent(agentID, "execute", map[string]interface{}{
|
||||
if s.agentWSManager == nil {
|
||||
return fmt.Errorf("AgentWSManager 未初始化")
|
||||
}
|
||||
err := s.agentWSManager.SendToAgent(agentID, "execute", map[string]interface{}{
|
||||
"task_id": task.ID,
|
||||
})
|
||||
if err != nil {
|
||||
+18
-6
@@ -1,4 +1,4 @@
|
||||
package services
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"baihu/internal/database"
|
||||
@@ -9,12 +9,21 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// SendStatsService 接口定义(避免循环依赖)
|
||||
type SendStatsService interface {
|
||||
IncrementStats(taskID uint, status string) error
|
||||
}
|
||||
|
||||
// TaskLogService 任务日志服务
|
||||
type TaskLogService struct{}
|
||||
type TaskLogService struct {
|
||||
sendStatsService SendStatsService
|
||||
}
|
||||
|
||||
// NewTaskLogService 创建任务日志服务
|
||||
func NewTaskLogService() *TaskLogService {
|
||||
return &TaskLogService{}
|
||||
func NewTaskLogService(sendStatsService SendStatsService) *TaskLogService {
|
||||
return &TaskLogService{
|
||||
sendStatsService: sendStatsService,
|
||||
}
|
||||
}
|
||||
|
||||
// CleanConfig 清理配置
|
||||
@@ -37,8 +46,11 @@ func (s *TaskLogService) SaveTaskLog(taskLog *models.TaskLog) error {
|
||||
|
||||
// UpdateTaskStats 更新任务统计
|
||||
func (s *TaskLogService) UpdateTaskStats(taskID uint, status string) {
|
||||
sendStatsService := NewSendStatsService()
|
||||
err := sendStatsService.IncrementStats(taskID, status)
|
||||
if s.sendStatsService == nil {
|
||||
logger.Error("[TaskLog] SendStatsService 未初始化")
|
||||
return
|
||||
}
|
||||
err := s.sendStatsService.IncrementStats(taskID, status)
|
||||
if err != nil {
|
||||
logger.Errorf("UpdateTaskStats err: %v", err)
|
||||
return
|
||||
@@ -1,4 +1,4 @@
|
||||
package services
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"baihu/internal/database"
|
||||
Reference in New Issue
Block a user