feat: refact id column define

This commit is contained in:
engigu
2026-03-03 17:25:38 +08:00
parent 117ae126f7
commit 4589c64923
62 changed files with 881 additions and 454 deletions
+8 -8
View File
@@ -59,13 +59,13 @@ type Result struct {
// Hooks 执行钩子接口
type Hooks interface {
// PreExecute 执行前钩子,返回日志ID和错误
PreExecute(ctx context.Context, req Request) (logID uint, err error)
PreExecute(ctx context.Context, req Request) (logID string, err error)
// PostExecute 执行后钩子,处理日志压缩和记录更新
PostExecute(ctx context.Context, logID uint, result *Result) error
PostExecute(ctx context.Context, logID string, result *Result) error
// OnHeartbeat 执行中心跳钩子,用于更新实时状态
OnHeartbeat(ctx context.Context, logID uint, duration int64) error
OnHeartbeat(ctx context.Context, logID string, duration int64) error
}
// Execute 执行命令(基础版本,不带钩子)
@@ -85,7 +85,7 @@ func ExecuteWithHooks(ctx context.Context, req Request, stdout, stderr io.Writer
}
// 仍然触发 PreExecute 以便流程完整
var logID uint
var logID string
if hooks != nil {
logID, _ = hooks.PreExecute(ctx, req)
}
@@ -118,7 +118,7 @@ func ExecuteWithHooks(ctx context.Context, req Request, stdout, stderr io.Writer
}
// 1. 执行前钩子
var logID uint
var logID string
if hooks != nil {
id, err := hooks.PreExecute(ctx, req)
if err != nil {
@@ -171,7 +171,7 @@ func ExecuteWithHooks(ctx context.Context, req Request, stdout, stderr io.Writer
)
f, ptyErr := pty.Start(cmd)
if ptyErr == nil {
logger.Infof("[Executor] 任务 #%d 启动于 PTY 模式", logID)
logger.Infof("[Executor] 任务 #%s 启动于 PTY 模式", logID)
ptyFile = f
started = true
copyDone = make(chan struct{})
@@ -182,7 +182,7 @@ func ExecuteWithHooks(ctx context.Context, req Request, stdout, stderr io.Writer
f.Close()
}()
} else {
logger.Errorf("[Executor] 任务 #%d PTY 启动失败: %v", logID, ptyErr)
logger.Errorf("[Executor] 任务 #%s PTY 启动失败: %v", logID, ptyErr)
}
}
@@ -192,7 +192,7 @@ func ExecuteWithHooks(ctx context.Context, req Request, stdout, stderr io.Writer
if stdout != stderr && stdout != io.Discard {
logger.Debugf("[Executor] 任务 #%d stdout (%p) 和 stderr (%p) 不同,回退到 Pipe 模式。", logID, stdout, stderr)
}
logger.Infof("[Executor] 任务 #%d 启动于 Pipe 模式", logID)
logger.Infof("[Executor] 任务 #%s 启动于 Pipe 模式", logID)
if stdout != nil && stdout == stderr {
pr, pw, err := os.Pipe()
if err == nil {
+11 -11
View File
@@ -63,7 +63,7 @@ const (
// ExecutionRequest 执行请求(标准接口)
type ExecutionRequest struct {
TaskID string // 任务 ID
LogID uint // 日志 ID
LogID string // 日志 ID
Name string // 任务名称
Type TaskType // 任务类型
Command string // 命令
@@ -84,7 +84,7 @@ type ExecutionMetadata struct {
// ExecutionResult 执行结果(标准接口)
type ExecutionResult struct {
TaskID string // 任务 ID
LogID uint // 日志 ID
LogID string // 日志 ID
Success bool // 是否成功
Output string // 输出内容
Error string // 错误信息
@@ -151,15 +151,15 @@ type schedulerHooksAdapter struct {
req *ExecutionRequest
}
func (h *schedulerHooksAdapter) PreExecute(ctx context.Context, req Request) (uint, error) {
func (h *schedulerHooksAdapter) PreExecute(ctx context.Context, req Request) (string, error) {
return h.req.LogID, nil
}
func (h *schedulerHooksAdapter) PostExecute(ctx context.Context, logID uint, result *Result) error {
func (h *schedulerHooksAdapter) PostExecute(ctx context.Context, logID string, result *Result) error {
return nil
}
func (h *schedulerHooksAdapter) OnHeartbeat(ctx context.Context, logID uint, duration int64) error {
func (h *schedulerHooksAdapter) OnHeartbeat(ctx context.Context, logID string, duration int64) error {
if h.handler != nil {
h.handler.OnTaskHeartbeat(h.req, duration)
}
@@ -182,7 +182,7 @@ type Scheduler struct {
mu sync.RWMutex
logger SchedulerLogger
runningTasks map[string]context.CancelFunc // 记录运行中的任务,用于停止 (TaskID -> CancelFunc)
runningExecs map[uint]context.CancelFunc // 记录运行中的执行,用于停止 (LogID -> CancelFunc)
runningExecs map[string]context.CancelFunc // 记录运行中的执行,用于停止 (LogID -> CancelFunc)
}
// NewScheduler 创建调度器
@@ -216,7 +216,7 @@ func NewScheduler(config SchedulerConfig, handler SchedulerEventHandler) *Schedu
stopCh: make(chan struct{}),
logger: &DefaultLogger{},
runningTasks: make(map[string]context.CancelFunc),
runningExecs: make(map[uint]context.CancelFunc),
runningExecs: make(map[string]context.CancelFunc),
}
return s
@@ -443,7 +443,7 @@ func (s *Scheduler) executeTask(req *ExecutionRequest) (*ExecutionResult, error)
// 注册到运行中任务
s.mu.Lock()
s.runningTasks[req.TaskID] = cancel
if req.LogID > 0 {
if req.LogID != "" {
s.runningExecs[req.LogID] = cancel
}
s.mu.Unlock()
@@ -451,7 +451,7 @@ func (s *Scheduler) executeTask(req *ExecutionRequest) (*ExecutionResult, error)
defer func() {
s.mu.Lock()
delete(s.runningTasks, req.TaskID)
if req.LogID > 0 {
if req.LogID != "" {
delete(s.runningExecs, req.LogID)
}
s.mu.Unlock()
@@ -527,14 +527,14 @@ func (s *Scheduler) StopTask(taskID string) bool {
}
// StopLog 停止正在运行的任务(通过 LogID,精确停止单个执行副本)
func (s *Scheduler) StopLog(logID uint) bool {
func (s *Scheduler) StopLog(logID string) bool {
s.mu.RLock()
cancel, exists := s.runningExecs[logID]
s.mu.RUnlock()
if exists && cancel != nil {
cancel()
s.logger.Infof("[Scheduler] 已尝试停止任务执行 #%d", logID)
s.logger.Infof("[Scheduler] 已尝试停止任务执行 #%s", logID)
return true
}
return false