fix: align business reports and record task migration progress
This commit is contained in:
@@ -216,7 +216,11 @@ func (e *TaskExecutor) runMethod(ctx context.Context, task *taskbiz.TimedTask) e
|
||||
return err
|
||||
}
|
||||
for attempt := 0; attempt <= options.MaxRetryTimes; attempt++ {
|
||||
platformtask.Logf(ctx, "开始第 %d 次执行;单次超时 %s,最多重试 %d 次", attempt+1, options.Timeout, options.MaxRetryTimes)
|
||||
err = e.runMethodAttempt(ctx, task, method, options.Timeout)
|
||||
if err != nil {
|
||||
platformtask.Logf(ctx, "第 %d 次执行失败:%s", attempt+1, truncateTaskText(err.Error()))
|
||||
}
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
if errors.Is(ctxErr, context.DeadlineExceeded) {
|
||||
return errTaskTimeout
|
||||
@@ -278,6 +282,7 @@ func (e *TaskExecutor) Run(ctx context.Context, task *taskbiz.TimedTask, trigger
|
||||
ctx = context.Background()
|
||||
}
|
||||
started := time.Now()
|
||||
ctx, output := platformtask.WithOutput(ctx)
|
||||
log = &taskbiz.TimedTaskLog{TriggerType: trigger, StartedAt: started, Status: "success"}
|
||||
if task != nil {
|
||||
log.TaskID = task.ID
|
||||
@@ -288,9 +293,12 @@ func (e *TaskExecutor) Run(ctx context.Context, task *taskbiz.TimedTask, trigger
|
||||
log.Status = "fail"
|
||||
log.ErrorMsg = truncateTaskText(fmt.Sprint(recovered))
|
||||
}
|
||||
log.Output = truncateTaskText(log.Output)
|
||||
log.FinishedAt = time.Now()
|
||||
log.DurationMS = log.FinishedAt.Sub(started).Milliseconds()
|
||||
if task != nil && task.ExecutorType == taskbiz.TaskExecutorMethod {
|
||||
log.Output = fmt.Sprintf("方法:%s;触发方式:%s\n%s\n执行结束:%s;耗时 %d ms", task.MethodName, trigger, output.String(), log.Status, log.DurationMS)
|
||||
}
|
||||
log.Output = truncateTaskText(log.Output)
|
||||
logCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 3*time.Second)
|
||||
defer cancel()
|
||||
if task != nil {
|
||||
|
||||
@@ -24,6 +24,52 @@ func TestPrivateIP(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMethodExecutionPersistsProgressAndRetryOutcome(t *testing.T) {
|
||||
registry := platformtask.NewRegistry()
|
||||
attempts := 0
|
||||
registry.Register(platformtask.Method{Name: "report", Run: func(ctx context.Context, _ json.RawMessage) error {
|
||||
attempts++
|
||||
if attempts == 1 {
|
||||
return errors.New("temporary storage failure")
|
||||
}
|
||||
platformtask.Logf(ctx, "日期=2026-11-05;已提交 12 行")
|
||||
return nil
|
||||
}})
|
||||
repo := &workerTaskRepo{logs: make(chan *taskbiz.TimedTaskLog, 1)}
|
||||
executor := NewTaskExecutorWithRegistry(taskbiz.NewTaskUsecaseWithRegistry(repo, registry), registry)
|
||||
log := executor.Run(context.Background(), &taskbiz.TimedTask{ID: 1, Name: "日报", ExecutorType: taskbiz.TaskExecutorMethod, MethodName: "report", Params: []byte(`{"_execution":{"maxRetryTimes":1,"retryIntervalSeconds":0}}`)}, "manual")
|
||||
stored := <-repo.logs
|
||||
if log.Status != "success" || stored.Output != log.Output {
|
||||
t.Fatalf("result was not persisted: %+v", log)
|
||||
}
|
||||
for _, detail := range []string{"方法:report", "第 1 次执行失败", "第 2 次执行", "已提交 12 行", "执行结束:success"} {
|
||||
if !strings.Contains(log.Output, detail) {
|
||||
t.Fatalf("missing %q in %s", detail, log.Output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMethodTimeoutKeepsPartialProgressWithoutClaimingCompletion(t *testing.T) {
|
||||
registry := platformtask.NewRegistry()
|
||||
release := make(chan struct{})
|
||||
finished := make(chan struct{})
|
||||
registry.Register(platformtask.Method{Name: "slow", Run: func(ctx context.Context, _ json.RawMessage) error {
|
||||
defer close(finished)
|
||||
platformtask.Logf(ctx, "已处理第一批")
|
||||
<-release
|
||||
platformtask.Logf(ctx, "迟到的结果")
|
||||
return nil
|
||||
}})
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Millisecond)
|
||||
defer cancel()
|
||||
log := (&TaskExecutor{methods: registry}).Run(ctx, &taskbiz.TimedTask{ID: 1, ExecutorType: taskbiz.TaskExecutorMethod, MethodName: "slow"}, "manual")
|
||||
close(release)
|
||||
<-finished
|
||||
if log.Status != "timeout" || !strings.Contains(log.Output, "已处理第一批") || strings.Contains(log.Output, "迟到的结果") {
|
||||
t.Fatalf("timeout did not preserve the execution snapshot: %+v", log)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunMethodReportsParentDeadlineAsTimeout(t *testing.T) {
|
||||
const methodName = "worker-test-parent-deadline"
|
||||
methods := platformtask.NewRegistry()
|
||||
|
||||
Reference in New Issue
Block a user