From 9c174d8db25111ad8cf2e7d33817c720cb00077b Mon Sep 17 00:00:00 2001 From: duorameng <2997944583@qq.com> Date: Sat, 9 May 2026 08:42:15 +0800 Subject: [PATCH] chore: history page support sharedworker.js --- cmd/reposync/reposync.go | 5 +-- internal/constant/constant.go | 3 ++ internal/controllers/task_controller.go | 8 ++--- internal/services/repo/repo_parser.go | 4 +-- internal/services/tasks/executor_service.go | 8 +++-- web/src/constants/index.ts | 11 +++++++ web/src/types/ws.ts | 16 +++++++++ web/src/utils/repo-parser.ts | 9 +++--- web/src/views/history/History.vue | 36 ++++++++++++++++++++- web/src/views/tasks/TaskDialog.vue | 17 +++++----- 10 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 web/src/types/ws.ts diff --git a/cmd/reposync/reposync.go b/cmd/reposync/reposync.go index 830854d..965c157 100644 --- a/cmd/reposync/reposync.go +++ b/cmd/reposync/reposync.go @@ -14,6 +14,7 @@ import ( "strings" "time" + "github.com/engigu/baihu-panel/internal/constant" "github.com/engigu/baihu-panel/internal/services" "github.com/engigu/baihu-panel/internal/services/repo" "github.com/engigu/baihu-panel/internal/utils" @@ -70,10 +71,10 @@ func Run(args []string) { fs.Parse(args) // 处理 $SCRIPTS_DIR$ 代号替换 - if strings.Contains(cfg.TargetPath, "$SCRIPTS_DIR$") { + if strings.Contains(cfg.TargetPath, constant.ScriptsDirPlaceholder) { scriptsDir := os.Getenv("BH_SCRIPTS_DIR") if scriptsDir != "" { - cfg.TargetPath = filepath.Clean(strings.ReplaceAll(cfg.TargetPath, "$SCRIPTS_DIR$", scriptsDir)) + cfg.TargetPath = filepath.Clean(strings.ReplaceAll(cfg.TargetPath, constant.ScriptsDirPlaceholder, scriptsDir)) } } diff --git a/internal/constant/constant.go b/internal/constant/constant.go index 24bcd93..58771c8 100644 --- a/internal/constant/constant.go +++ b/internal/constant/constant.go @@ -177,6 +177,9 @@ const ( MaxMessageSize = 1024 * 1024 // 1MB // MaxLogSize 允许的最大日志大小 (保留末尾 10MB) MaxLogSize = 10 * 1024 * 1024 // 10MB + + // ScriptsDirPlaceholder 脚本目录占位符 + ScriptsDirPlaceholder = "$SCRIPTS_DIR$" ) // TablePrefix 表前缀,从配置文件读取 diff --git a/internal/controllers/task_controller.go b/internal/controllers/task_controller.go index b9f7e8b..87df3cc 100644 --- a/internal/controllers/task_controller.go +++ b/internal/controllers/task_controller.go @@ -42,7 +42,7 @@ func resolveWorkDir(workDir string) string { return absPath } // 如果已经是绝对路径,直接返回 - if strings.HasPrefix(workDir, "$SCRIPTS_DIR$") { + if strings.HasPrefix(workDir, constant.ScriptsDirPlaceholder) { return workDir } if filepath.IsAbs(workDir) { @@ -381,7 +381,7 @@ func (tc *TaskController) deleteRepoPhysicalFiles(task *models.Task) { } } - if targetPath == "" || targetPath == "$SCRIPTS_DIR$" { + if targetPath == "" || targetPath == constant.ScriptsDirPlaceholder { logger.Warnf("[Controller] 任务 %s 无法确定有效的物理删除路径,跳过", task.Name) return } @@ -389,8 +389,8 @@ func (tc *TaskController) deleteRepoPhysicalFiles(task *models.Task) { // 确定绝对路径 scriptsDir, _ := filepath.Abs(constant.ScriptsWorkDir) fullPath := targetPath - if strings.HasPrefix(targetPath, "$SCRIPTS_DIR$") { - fullPath = filepath.Join(scriptsDir, strings.TrimPrefix(targetPath, "$SCRIPTS_DIR$")) + if strings.HasPrefix(targetPath, constant.ScriptsDirPlaceholder) { + fullPath = filepath.Join(scriptsDir, strings.TrimPrefix(targetPath, constant.ScriptsDirPlaceholder)) } else if !filepath.IsAbs(targetPath) { fullPath = filepath.Join(scriptsDir, targetPath) } diff --git a/internal/services/repo/repo_parser.go b/internal/services/repo/repo_parser.go index 79f5f18..2484cde 100644 --- a/internal/services/repo/repo_parser.go +++ b/internal/services/repo/repo_parser.go @@ -134,9 +134,9 @@ func ParseRepoScriptsAndAddCron(taskID string, logWriter io.Writer, forceComment displayPath = filepath.Base(path) relDir, _ := filepath.Rel(absScriptsDir, filepath.Dir(absPath)) if relDir == "." { - displayWorkDir = "$SCRIPTS_DIR$" + displayWorkDir = constant.ScriptsDirPlaceholder } else { - displayWorkDir = filepath.ToSlash(filepath.Join("$SCRIPTS_DIR$", relDir)) + displayWorkDir = filepath.ToSlash(filepath.Join(constant.ScriptsDirPlaceholder, relDir)) } } diff --git a/internal/services/tasks/executor_service.go b/internal/services/tasks/executor_service.go index 714bbdd..ac11565 100644 --- a/internal/services/tasks/executor_service.go +++ b/internal/services/tasks/executor_service.go @@ -291,6 +291,7 @@ func (h *ServerSchedulerHandler) OnTaskCompleted(req *executor.ExecutionRequest, eventbus.DefaultBus.Publish(eventbus.Event{ Type: eventType, Payload: map[string]interface{}{ + "log_id": req.LogID, "task_id": task.ID, "task_name": task.Name, "status": result.Status, @@ -372,6 +373,7 @@ func (h *ServerSchedulerHandler) OnTaskFailed(req *executor.ExecutionRequest, er eventbus.DefaultBus.Publish(eventbus.Event{ Type: constant.EventTaskFailed, Payload: map[string]interface{}{ + "log_id": req.LogID, "task_id": taskID, "task_name": taskName, "error": err.Error(), @@ -1099,9 +1101,9 @@ func BuildRepoCommand(task *models.Task) (string, string) { displayTargetPath := absTargetPath if rel, err := filepath.Rel(scriptsDir, absTargetPath); err == nil && !strings.HasPrefix(rel, "..") { if rel == "." { - displayTargetPath = "$SCRIPTS_DIR$" + displayTargetPath = constant.ScriptsDirPlaceholder } else { - displayTargetPath = "$SCRIPTS_DIR$/" + filepath.ToSlash(rel) + displayTargetPath = constant.ScriptsDirPlaceholder + "/" + filepath.ToSlash(rel) } } @@ -1233,7 +1235,7 @@ func (es *ExecutorService) refreshExecutionRequestEnvs(req *executor.ExecutionRe func (es *ExecutorService) ResolvePath(path string) string { absScriptsDir := resolveAbsScriptsDir() - return strings.ReplaceAll(path, "$SCRIPTS_DIR$", absScriptsDir) + return strings.ReplaceAll(path, constant.ScriptsDirPlaceholder, absScriptsDir) } func buildRepoCommandEnvPrefix() string { diff --git a/web/src/constants/index.ts b/web/src/constants/index.ts index 759562b..0649416 100644 --- a/web/src/constants/index.ts +++ b/web/src/constants/index.ts @@ -8,6 +8,8 @@ export const PATHS = { CONFIGS_DIR: '/app/configs', // 环境目录 ENVS_DIR: '/app/envs', + // 脚本目录占位符 + SCRIPTS_DIR_PLACEHOLDER: '$SCRIPTS_DIR$', } as const // 文件扩展名对应的运行命令 @@ -61,3 +63,12 @@ export const ENV_TYPE = { NORMAL: 'normal', SECRET: 'secret', } as const + +// 任务事件类型 +export const TASK_EVENTS = { + SUCCESS: 'task_success', + FAILED: 'task_failed', + TIMEOUT: 'task_timeout', + RUNNING: 'task_running', + QUEUED: 'task_queued', +} as const diff --git a/web/src/types/ws.ts b/web/src/types/ws.ts new file mode 100644 index 0000000..7bb21f8 --- /dev/null +++ b/web/src/types/ws.ts @@ -0,0 +1,16 @@ +export interface WSMessage { + type: string + timestamp: number + payload: any +} + +export interface TaskStatusPayload { + id: string + task_id: string + task_name: string + status: string + duration: number + start_time: string | null + end_time: string | null + error?: string | null +} diff --git a/web/src/utils/repo-parser.ts b/web/src/utils/repo-parser.ts index d039188..ac8f464 100644 --- a/web/src/utils/repo-parser.ts +++ b/web/src/utils/repo-parser.ts @@ -1,4 +1,5 @@ import type { RepoConfig, Task } from '@/api' +import { PATHS } from '@/constants' /** * 仓库命令解析结果 @@ -64,10 +65,10 @@ export function parseBaihuCommand(command: string): ParsedRepoResult | null { } break case '--target-path': - // 处理 $SCRIPTS_DIR$ 占位符 - if (value.startsWith('$SCRIPTS_DIR$/')) { - repoConfig.target_path = value.replace('$SCRIPTS_DIR$/', '') - } else if (value === '$SCRIPTS_DIR$') { + // 处理脚本目录占位符 + if (value.startsWith(`${PATHS.SCRIPTS_DIR_PLACEHOLDER}/`)) { + repoConfig.target_path = value.replace(`${PATHS.SCRIPTS_DIR_PLACEHOLDER}/`, '') + } else if (value === PATHS.SCRIPTS_DIR_PLACEHOLDER) { repoConfig.target_path = '' } else { repoConfig.target_path = value diff --git a/web/src/views/history/History.vue b/web/src/views/history/History.vue index 1af5453..98b0070 100644 --- a/web/src/views/history/History.vue +++ b/web/src/views/history/History.vue @@ -1,7 +1,7 @@