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()