fix monitor desktop unknown recovery

This commit is contained in:
2026-06-22 15:38:06 +08:00
parent 365a41597e
commit 12681105f2
8 changed files with 162 additions and 39 deletions
@@ -27,6 +27,8 @@ import (
const desktopPublishMaxAttempts = 3
var errDesktopTaskLeaseDeferred = errors.New("desktop task lease deferred")
type DesktopTaskService struct {
pool *pgxpool.Pool
monitoringPool *pgxpool.Pool
@@ -247,7 +249,11 @@ func (s *DesktopTaskService) Lease(ctx context.Context, client *repository.Deskt
if taskID == nil {
return &LeaseDesktopTaskResponse{}, nil
}
return nil, s.classifyLeaseError(ctx, client, taskID)
classifiedErr := s.classifyLeaseError(ctx, client, taskID)
if errors.Is(classifiedErr, errDesktopTaskLeaseDeferred) {
return &LeaseDesktopTaskResponse{}, nil
}
return nil, classifiedErr
}
fields := []zap.Field{
zap.String("client_id", client.ID.String()),
@@ -966,6 +972,9 @@ func (s *DesktopTaskService) Complete(ctx context.Context, client *repository.De
taskRepo := repository.NewDesktopTaskRepository(tx)
previousTask, _ := taskRepo.GetByDesktopID(ctx, desktopID, client.WorkspaceID)
previousAttemptID := activeAttemptID(previousTask)
if previousTask != nil {
finalStatus = normalizeDesktopTaskCompletionStatusForKind(previousTask.Kind, finalStatus)
}
task, err := taskRepo.Complete(ctx, desktopID, HashDesktopClientToken(strings.TrimSpace(req.LeaseToken)), finalStatus, resultJSON, errorJSON)
if err != nil {
@@ -1558,10 +1567,19 @@ func (s *DesktopTaskService) classifyLeaseError(ctx context.Context, client *rep
return response.ErrForbidden(40381, "desktop_task_not_target_client", "desktop task is assigned to another desktop client")
}
return classifyDesktopTaskLeaseState(task)
}
func classifyDesktopTaskLeaseState(task *repository.DesktopTask) error {
if task == nil {
return response.ErrNotFound(40482, "desktop_task_not_found", "desktop task not found")
}
if task.Kind == "monitor" && task.Status == "queued" {
return errDesktopTaskLeaseDeferred
}
if task.Status == "in_progress" {
return response.ErrConflict(40989, "desktop_task_already_in_progress", "desktop task is already in progress")
}
return response.ErrConflict(40991, "desktop_task_not_queued", "desktop task is not queued")
}
@@ -1646,13 +1664,22 @@ func normalizeDesktopTaskCompletionStatus(status string) string {
}
}
func normalizeDesktopTaskViewStatus(kind string, status string) string {
if strings.TrimSpace(kind) == "publish" {
return normalizeDesktopTaskTerminalStatus(status)
func normalizeDesktopTaskCompletionStatusForKind(kind string, status string) string {
if strings.TrimSpace(kind) == "monitor" && strings.TrimSpace(status) == "unknown" {
return "failed"
}
return strings.TrimSpace(status)
}
func normalizeDesktopTaskViewStatus(kind string, status string) string {
switch strings.TrimSpace(kind) {
case "monitor", "publish":
return normalizeDesktopTaskTerminalStatus(status)
default:
return strings.TrimSpace(status)
}
}
type desktopTaskRecoveryMode string
const (