From 6cbc368dc1796122c85a1cfa6f9f025a2b2459ee Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 11 Jun 2026 09:03:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/admin/task-executions.tsx | 4 +-- internal/model/task_execution.go | 4 +-- internal/model/task_execution_test.go | 35 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/frontend/components/common/admin/task-executions.tsx b/frontend/components/common/admin/task-executions.tsx index a3538c87..d9f78e55 100644 --- a/frontend/components/common/admin/task-executions.tsx +++ b/frontend/components/common/admin/task-executions.tsx @@ -341,7 +341,7 @@ export function TaskExecutionsManager() {
-
+
{selectedExecution.result || "-"}
@@ -349,7 +349,7 @@ export function TaskExecutionsManager() { {selectedExecution.error_message && (
-
+
{selectedExecution.error_message}
diff --git a/internal/model/task_execution.go b/internal/model/task_execution.go index c6d5fa05..e2ad48f8 100644 --- a/internal/model/task_execution.go +++ b/internal/model/task_execution.go @@ -58,9 +58,9 @@ func CreateTaskExecution(ctx context.Context, execution *TaskExecution) error { return db.DB(ctx).Create(execution).Error } -// UpdateTaskExecution 更新任务执行记录 +// UpdateTaskExecution 更新任务执行记录,忽略 log 字段以防覆写正在追加的日志 func UpdateTaskExecution(ctx context.Context, execution *TaskExecution) error { - return db.DB(ctx).Save(execution).Error + return db.DB(ctx).Omit("log").Save(execution).Error } // GetTaskExecutionByTaskID 根据 TaskID 获取执行记录 diff --git a/internal/model/task_execution_test.go b/internal/model/task_execution_test.go index a7a08e95..58943d12 100644 --- a/internal/model/task_execution_test.go +++ b/internal/model/task_execution_test.go @@ -188,6 +188,41 @@ func TestUpdateTaskExecutionFailed(t *testing.T) { assert.Equal(t, int64(200), found.Duration) } +func TestUpdateTaskExecutionDoesNotOverwriteLog(t *testing.T) { + cleanup := setupTaskExecutionTestEnvironment(t) + defer cleanup() + ctx := context.Background() + + execution := &TaskExecution{ + TaskID: "test_omit_log_001", + TaskType: "upload:cleanup_unused", + TaskName: "清理未使用上传", + Status: TaskExecutionStatusPending, + TriggeredBy: "manual", + } + err := CreateTaskExecution(ctx, execution) + require.NoError(t, err) + + // In a real execution, logs are appended to the DB asynchronously via AppendTaskExecutionLog + err = AppendTaskExecutionLog(ctx, "test_omit_log_001", "第一条执行日志") + require.NoError(t, err) + + // The local struct still has empty Log because it was not reloaded + assert.Empty(t, execution.Log) + + // Now complete/update the execution (e.g. status, duration) + execution.Status = TaskExecutionStatusSucceeded + execution.Duration = 100 + err = UpdateTaskExecution(ctx, execution) + require.NoError(t, err) + + // Get the updated execution record and check that the Log was NOT overwritten/wiped + found, err := GetTaskExecutionByTaskID(ctx, "test_omit_log_001") + require.NoError(t, err) + assert.Equal(t, TaskExecutionStatusSucceeded, found.Status) + assert.Contains(t, found.Log, "第一条执行日志") +} + func TestAppendTaskExecutionLog(t *testing.T) { cleanup := setupTaskExecutionTestEnvironment(t) defer cleanup()