forked from longtao.wu/droboticflow
827 lines
32 KiB
Go
827 lines
32 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"net/http"
|
||
"regexp"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"droboticflow/internal/domain"
|
||
|
||
"github.com/go-chi/chi/v5"
|
||
)
|
||
|
||
type captureAutomationResponse struct {
|
||
Config *domain.CaptureAutomationConfig `json:"config,omitempty"`
|
||
Counts domain.CaptureRecordCounts `json:"counts"`
|
||
Records []domain.CaptureRecord `json:"records"`
|
||
}
|
||
|
||
func normalizeCaptureAutomationConfig(config domain.CaptureAutomationConfig) domain.CaptureAutomationConfig {
|
||
config.ConnectionID = strings.TrimSpace(config.ConnectionID)
|
||
config.Bucket = strings.TrimSpace(config.Bucket)
|
||
config.Prefix = strings.Trim(strings.TrimSpace(config.Prefix), "/")
|
||
config.DeliveryPrefix = strings.Trim(strings.TrimSpace(config.DeliveryPrefix), "/")
|
||
config.Schema = strings.TrimSpace(config.Schema)
|
||
if config.Schema == "" {
|
||
config.Schema = "orbbec_ego_stereo_v1"
|
||
}
|
||
// Legacy saved configurations must not require an upload completion file.
|
||
config.CompletionMode = "required_files"
|
||
config.ManifestName = strings.Trim(strings.TrimSpace(config.ManifestName), "/")
|
||
if config.ManifestName == "" {
|
||
config.ManifestName = "source-manifest.json"
|
||
}
|
||
config.WorkflowInputID = strings.TrimSpace(config.WorkflowInputID)
|
||
if config.WorkflowInputID == "" {
|
||
config.WorkflowInputID = "sourceDirectory"
|
||
}
|
||
if config.PollIntervalSeconds <= 0 {
|
||
config.PollIntervalSeconds = 30
|
||
}
|
||
if config.StabilityWindowSeconds <= 0 {
|
||
config.StabilityWindowSeconds = 30
|
||
}
|
||
return config
|
||
}
|
||
|
||
func captureAutomationCounts(records []domain.CaptureRecord) domain.CaptureRecordCounts {
|
||
counts := domain.CaptureRecordCounts{Total: int64(len(records))}
|
||
for _, record := range records {
|
||
switch record.BusinessStage {
|
||
case domain.CaptureStageWaitingAnnotation:
|
||
counts.WaitingAnnotation++
|
||
case domain.CaptureStageWaitingQuality:
|
||
counts.WaitingQuality++
|
||
case domain.CaptureStageSampling:
|
||
counts.Sampling++
|
||
case domain.CaptureStageWaitingAcceptance:
|
||
counts.WaitingAcceptance++
|
||
case domain.CaptureStagePackaging:
|
||
counts.Packaging++
|
||
case domain.CaptureStageDelivered:
|
||
counts.Delivered++
|
||
case domain.CaptureStageNoDeliverables:
|
||
counts.NoDeliverables++
|
||
}
|
||
switch record.Status {
|
||
case domain.CaptureRecordIncomplete:
|
||
counts.Incomplete++
|
||
case domain.CaptureRecordStabilizing:
|
||
counts.Ready++
|
||
case domain.CaptureRecordReady, domain.CaptureRecordLaunching:
|
||
counts.Ready++
|
||
case domain.CaptureRecordRequeueing, domain.CaptureRecordQueued:
|
||
counts.Queued++
|
||
case domain.CaptureRecordRunning:
|
||
counts.Running++
|
||
case domain.CaptureRecordWaiting:
|
||
counts.WaitingForHuman++
|
||
case domain.CaptureRecordCompleted:
|
||
counts.Completed++
|
||
case domain.CaptureRecordFailed:
|
||
counts.Failed++
|
||
case domain.CaptureRecordCancelled:
|
||
counts.Cancelled++
|
||
case domain.CaptureRecordDiscarded:
|
||
counts.Discarded++
|
||
}
|
||
}
|
||
return counts
|
||
}
|
||
|
||
func (server *server) captureAutomationPayload(ctx context.Context, ctxWorkflow *domain.WorkflowRecord, records []domain.CaptureRecord) captureAutomationResponse {
|
||
var config *domain.CaptureAutomationConfig
|
||
if ctxWorkflow != nil && ctxWorkflow.CaptureAutomation != nil {
|
||
copy := normalizeCaptureAutomationConfig(*ctxWorkflow.CaptureAutomation)
|
||
copy.MaxRetryAttempts = ctxWorkflow.MaxRetryAttempts
|
||
copy.RetryBackoffSeconds = ctxWorkflow.RetryBackoffSeconds
|
||
config = ©
|
||
}
|
||
for index := range records {
|
||
record := &records[index]
|
||
if record.BusinessStage == "" {
|
||
record.BusinessStage = domain.CaptureBusinessStageForStatus(record.Status)
|
||
}
|
||
record.MaxAttempts = 1
|
||
if ctxWorkflow != nil {
|
||
record.MaxAttempts += ctxWorkflow.MaxRetryAttempts
|
||
}
|
||
var run *domain.RunRecord
|
||
if ctxWorkflow != nil {
|
||
run, _ = server.store.GetRunByWorkflowTaskID(ctx, ctxWorkflow.ID, record.ID)
|
||
}
|
||
if run == nil && strings.TrimSpace(record.RunID) != "" {
|
||
run, _ = server.store.GetRun(ctx, record.RunID)
|
||
}
|
||
if run == nil {
|
||
continue
|
||
}
|
||
record.RunID = run.ID
|
||
attemptRun := run
|
||
for depth := 0; depth < 16 && attemptRun.Trigger == domain.RunTriggerHumanRelease && attemptRun.ParentRunID != ""; depth++ {
|
||
parent, parentErr := server.store.GetRun(ctx, attemptRun.ParentRunID)
|
||
if parentErr != nil || parent == nil || parent.WorkflowID != run.WorkflowID {
|
||
break
|
||
}
|
||
attemptRun = parent
|
||
}
|
||
record.Attempt = attemptRun.Attempt
|
||
if record.Attempt <= 0 {
|
||
record.Attempt = 1
|
||
}
|
||
if run.Trigger == domain.RunTriggerRetry && run.Status == domain.RunStatusQueued {
|
||
record.NextRetryAt = run.AvailableAt
|
||
}
|
||
record.ParentRunID = run.ParentRunID
|
||
if run.Failure != nil {
|
||
record.FailedNodeID = strings.TrimSpace(run.Failure.NodeID)
|
||
if strings.TrimSpace(record.Error) == "" {
|
||
record.Error = strings.TrimSpace(run.Failure.Message)
|
||
}
|
||
}
|
||
record.BusinessStage = server.captureBusinessStage(ctx, *record, run)
|
||
}
|
||
return captureAutomationResponse{Config: config, Counts: captureAutomationCounts(records), Records: records}
|
||
}
|
||
|
||
func (server *server) captureBusinessStage(ctx context.Context, record domain.CaptureRecord, run *domain.RunRecord) domain.CaptureBusinessStage {
|
||
if domain.RunHasNoDeliverables(run) || (run == nil && record.Status == domain.CaptureRecordCompleted && record.BusinessStage == domain.CaptureStageNoDeliverables) {
|
||
return domain.CaptureStageNoDeliverables
|
||
}
|
||
if run == nil || run.Status != domain.RunStatusWaitingForHuman {
|
||
return domain.CaptureBusinessStageForStatus(record.Status)
|
||
}
|
||
if record.SamplingDecision != nil {
|
||
if record.SamplingDecision.Decision == domain.CaptureSamplingNotSelected {
|
||
return domain.CaptureStagePackaging
|
||
}
|
||
}
|
||
items, err := server.store.ListHumanItems(ctx, record.OrganizationID, domain.HumanItemQuery{RunID: run.ID})
|
||
if err != nil {
|
||
return domain.CaptureStageWaitingAnnotation
|
||
}
|
||
stage := domain.CaptureStageWaitingAnnotation
|
||
for _, item := range items {
|
||
if item.CurrentStageID == fanyuV5PrePackageAcceptanceStageID {
|
||
if item.Status == domain.HumanItemAccepted {
|
||
stage = domain.CaptureStagePackaging
|
||
} else {
|
||
return domain.CaptureStageWaitingAcceptance
|
||
}
|
||
}
|
||
if item.CurrentStageID == domain.FanyuV58StageQualityReview {
|
||
stage = domain.CaptureStageWaitingQuality
|
||
}
|
||
}
|
||
if record.SamplingDecision == nil {
|
||
allAccepted := len(items) > 0
|
||
for _, item := range items {
|
||
if item.CurrentStageID == fanyuV5PrePackageAcceptanceStageID {
|
||
continue
|
||
}
|
||
if item.Status != domain.HumanItemAccepted {
|
||
allAccepted = false
|
||
break
|
||
}
|
||
}
|
||
if allAccepted {
|
||
return domain.CaptureStageSampling
|
||
}
|
||
}
|
||
return stage
|
||
}
|
||
|
||
func (server *server) handleCaptureAutomationGet(writer http.ResponseWriter, request *http.Request) {
|
||
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if workflow == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
records, err := server.store.ListCaptureRecords(request.Context(), workflow.OrganizationID, workflow.ID, 200)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, server.captureAutomationPayload(request.Context(), workflow, records))
|
||
}
|
||
|
||
func validateCaptureAutomationConfig(workflow *domain.WorkflowRecord, config domain.CaptureAutomationConfig) []validationIssue {
|
||
if workflow != nil && workflow.Type != domain.WorkflowTypeStreaming && config.Enabled {
|
||
return []validationIssue{{Field: "type", Code: "continuous_capture_only", Message: "仅持续 Capture 工作流可启动正式处理;历史单次工作流只读"}}
|
||
}
|
||
issues := make([]validationIssue, 0)
|
||
if config.MaxRetryAttempts < 0 {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.maxRetryAttempts", Message: "自动重试次数不能小于 0"})
|
||
}
|
||
if config.RetryBackoffSeconds < 0 {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.retryBackoffSeconds", Message: "重试间隔不能小于 0"})
|
||
}
|
||
if !config.Enabled {
|
||
return issues
|
||
}
|
||
if workflow == nil || workflow.DefinitionState != domain.WorkflowDefinitionStateReleased {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.enabled", Message: "自动处理只能绑定到已发布的正式工作流"})
|
||
}
|
||
if config.ConnectionID == "" {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.connectionId", Message: "请选择对象存储连接"})
|
||
}
|
||
if config.Bucket == "" {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.bucket", Message: "Bucket 不能为空"})
|
||
}
|
||
if config.Prefix == "" {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.prefix", Message: "持续接入目录不能为空"})
|
||
}
|
||
if !domain.ValidDeliveryPrefix(config.DeliveryPrefix) {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.deliveryPrefix", Message: "请选择有效交付保存目录,不能使用系统产物目录"})
|
||
} else if domain.StoragePrefixesOverlap(config.Prefix, config.DeliveryPrefix) {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.deliveryPrefix", Message: "交付目录与监听目录不能相同或互相包含"})
|
||
}
|
||
if config.PollIntervalSeconds < 5 || config.PollIntervalSeconds > 3600 {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.pollIntervalSeconds", Message: "检查间隔必须在5到3600秒之间"})
|
||
}
|
||
if config.StabilityWindowSeconds < 5 || config.StabilityWindowSeconds > 3600 {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.stabilityWindowSeconds", Message: "稳定等待时间必须在5到3600秒之间"})
|
||
}
|
||
for index, pattern := range config.RequiredPatterns {
|
||
if _, err := regexp.Compile(pattern); err != nil {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.requiredPatterns", Message: "第" + strconv.Itoa(index+1) + "个必需文件规则不是有效正则表达式"})
|
||
}
|
||
}
|
||
inputFound := false
|
||
if workflow != nil {
|
||
for _, input := range workflow.WorkflowInputs {
|
||
if input.ID != config.WorkflowInputID {
|
||
continue
|
||
}
|
||
inputFound = true
|
||
if input.PayloadMode != "" && input.PayloadMode != domain.ArtifactTransferModeRef {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.workflowInputId", Message: "自动处理输入必须使用引用传递"})
|
||
}
|
||
break
|
||
}
|
||
}
|
||
if !inputFound {
|
||
issues = append(issues, validationIssue{Field: "captureAutomation.workflowInputId", Message: "工作流中不存在指定的目录输入"})
|
||
}
|
||
return issues
|
||
}
|
||
|
||
func (server *server) handleCaptureAutomationUpdate(writer http.ResponseWriter, request *http.Request) {
|
||
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if workflow == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
return
|
||
}
|
||
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
|
||
return
|
||
}
|
||
var config domain.CaptureAutomationConfig
|
||
if !decodeJSON(writer, request, &config) {
|
||
return
|
||
}
|
||
config = normalizeCaptureAutomationConfig(config)
|
||
// Start history is owned by the server, not by editable client input.
|
||
config.HasStarted = config.Enabled || strings.TrimSpace(workflow.LastRunAt) != ""
|
||
if previous := workflow.CaptureAutomation; previous != nil {
|
||
config.HasStarted = config.HasStarted || previous.HasStarted || previous.Enabled
|
||
}
|
||
if config.Enabled && strings.TrimSpace(workflow.ArchivedAt) != "" {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "工作流已归档,请先恢复后再启用自动处理", "code": "workflow_archived"})
|
||
return
|
||
}
|
||
if config.ConnectionID != "" {
|
||
connection, connectionErr := server.store.GetStorageConnection(request.Context(), config.ConnectionID)
|
||
if connectionErr != nil || connection == nil || normalizedTenantID(connection.OrganizationID) != normalizedTenantID(workflow.OrganizationID) {
|
||
renderValidationError(writer, validationIssue{Field: "captureAutomation.connectionId", Message: "对象存储连接不存在或无权访问"})
|
||
return
|
||
}
|
||
if config.Bucket == "" {
|
||
config.Bucket = strings.TrimSpace(connection.Bucket)
|
||
}
|
||
if connection.Provider != domain.StorageProviderBOS {
|
||
renderValidationError(writer, validationIssue{Field: "captureAutomation.connectionId", Message: "当前版本的 Capture 自动处理仅支持 BOS"})
|
||
return
|
||
}
|
||
}
|
||
if issues := validateCaptureAutomationConfig(workflow, config); len(issues) > 0 {
|
||
renderValidationError(writer, issues...)
|
||
return
|
||
}
|
||
// The project path is server-owned and remains stable after a rename.
|
||
config.DeliveryProjectDirectory = ""
|
||
workflows, listErr := server.store.ListAllWorkflowsByOrganization(request.Context(), workflow.OrganizationID)
|
||
if listErr != nil {
|
||
renderError(writer, 500, listErr)
|
||
return
|
||
}
|
||
for _, other := range workflows {
|
||
if other.CaptureAutomation == nil {
|
||
continue
|
||
}
|
||
old := other.CaptureAutomation
|
||
if other.ProjectID == workflow.ProjectID && old.DeliveryProjectDirectory != "" {
|
||
config.DeliveryProjectDirectory = old.DeliveryProjectDirectory
|
||
}
|
||
if other.ID != workflow.ID && old.Bucket == config.Bucket && config.Enabled {
|
||
if (old.Enabled && config.DeliveryPrefix != "" && domain.StoragePrefixesOverlap(config.DeliveryPrefix, old.Prefix)) || (old.DeliveryPrefix != "" && domain.StoragePrefixesOverlap(config.Prefix, old.DeliveryPrefix)) {
|
||
renderValidationError(writer, validationIssue{Field: "captureAutomation.deliveryPrefix", Message: "交付目录与其他工作流监听目录重叠"})
|
||
return
|
||
}
|
||
}
|
||
}
|
||
if config.DeliveryProjectDirectory == "" && config.DeliveryPrefix != "" {
|
||
project, e := server.store.GetProject(request.Context(), workflow.ProjectID)
|
||
if e != nil || project == nil {
|
||
renderJSON(writer, 400, map[string]string{"error": "项目不存在"})
|
||
return
|
||
}
|
||
config.DeliveryProjectDirectory = domain.DeliveryProjectDirectory(project.Name, project.ID)
|
||
}
|
||
if config.Enabled {
|
||
captures, e := server.store.ListCaptureRecords(request.Context(), workflow.OrganizationID, "", 0)
|
||
if e != nil {
|
||
renderError(writer, 500, e)
|
||
return
|
||
}
|
||
for _, capture := range captures {
|
||
if d := capture.Delivery; d != nil && d.Bucket == config.Bucket && domain.StoragePrefixesOverlap(config.Prefix, d.Prefix) {
|
||
renderValidationError(writer, validationIssue{Field: "captureAutomation.prefix", Message: "监听目录与已有任务的交付位置重叠"})
|
||
return
|
||
}
|
||
}
|
||
browser, _, cleanup, e := server.resolveObjectStorageBrowser(request.Context(), config.ConnectionID)
|
||
if cleanup != nil {
|
||
defer cleanup()
|
||
}
|
||
if e != nil {
|
||
renderError(writer, 400, e)
|
||
return
|
||
}
|
||
probe := config.DeliveryPrefix + "/.write-probe-" + domain.NewID("delivery")
|
||
if _, e = browser.PutObject(request.Context(), config.Bucket, probe, []byte("delivery-write-check"), "text/plain"); e != nil {
|
||
renderJSON(writer, 400, map[string]string{"error": "交付目录不可写:" + e.Error()})
|
||
return
|
||
}
|
||
if e = browser.DeleteObject(request.Context(), config.Bucket, probe); e != nil {
|
||
renderJSON(writer, 400, map[string]string{"error": "交付目录校验文件无法清理:" + probe})
|
||
return
|
||
}
|
||
}
|
||
workflow.CaptureAutomation = &config
|
||
workflow.MaxRetryAttempts = config.MaxRetryAttempts
|
||
workflow.RetryBackoffSeconds = config.RetryBackoffSeconds
|
||
workflow.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertWorkflow(request.Context(), *workflow); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
records, err := server.store.ListCaptureRecords(request.Context(), workflow.OrganizationID, workflow.ID, 200)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, server.captureAutomationPayload(request.Context(), workflow, records))
|
||
}
|
||
|
||
type captureActionRequest struct {
|
||
Reason string `json:"reason"`
|
||
Mode string `json:"mode,omitempty"`
|
||
}
|
||
|
||
func captureActionActorID(request *http.Request) string {
|
||
if principal, ok := currentPrincipal(request); ok && principal != nil && strings.TrimSpace(principal.User.ID) != "" {
|
||
return principal.User.ID
|
||
}
|
||
if apiTokenAuthenticated(request) {
|
||
return "api-token"
|
||
}
|
||
return "system"
|
||
}
|
||
|
||
func decodeCaptureAction(writer http.ResponseWriter, request *http.Request) (captureActionRequest, bool) {
|
||
var payload captureActionRequest
|
||
if !decodeJSON(writer, request, &payload) {
|
||
return captureActionRequest{}, false
|
||
}
|
||
payload.Reason = strings.TrimSpace(payload.Reason)
|
||
payload.Mode = strings.ToLower(strings.TrimSpace(payload.Mode))
|
||
if payload.Reason == "" {
|
||
renderValidationError(writer, validationIssue{Field: "reason", Message: "请填写操作原因"})
|
||
return captureActionRequest{}, false
|
||
}
|
||
if len([]rune(payload.Reason)) > 500 {
|
||
renderValidationError(writer, validationIssue{Field: "reason", Message: "操作原因不能超过 500 个字符"})
|
||
return captureActionRequest{}, false
|
||
}
|
||
return payload, true
|
||
}
|
||
|
||
func decodeCaptureActionReason(writer http.ResponseWriter, request *http.Request) (string, bool) {
|
||
payload, ok := decodeCaptureAction(writer, request)
|
||
return payload.Reason, ok
|
||
}
|
||
|
||
func newCaptureAction(action domain.CaptureActionType, fromStatus, toStatus domain.CaptureRecordStatus, actorID, reason, runID string) domain.CaptureActionRecord {
|
||
return domain.CaptureActionRecord{
|
||
ID: domain.NewID("capture-action"), Action: action, FromStatus: fromStatus, ToStatus: toStatus,
|
||
ActorUserID: actorID, Reason: reason, RunID: runID, CreatedAt: nowISO(),
|
||
}
|
||
}
|
||
|
||
func appendCaptureAction(record *domain.CaptureRecord, action domain.CaptureActionRecord) {
|
||
if record == nil {
|
||
return
|
||
}
|
||
record.Actions = append(record.Actions, action)
|
||
if len(record.Actions) > 100 {
|
||
record.Actions = append([]domain.CaptureActionRecord(nil), record.Actions[len(record.Actions)-100:]...)
|
||
}
|
||
}
|
||
|
||
func terminalHumanItemStatus(status domain.HumanItemStatus) bool {
|
||
return status == domain.HumanItemAccepted || status == domain.HumanItemRejected || status == domain.HumanItemEscalated || status == domain.HumanItemCancelled
|
||
}
|
||
|
||
func (server *server) cancelCaptureHumanWork(ctx context.Context, record *domain.CaptureRecord, actorID, reason string) error {
|
||
if record == nil || strings.TrimSpace(record.RunID) == "" {
|
||
return nil
|
||
}
|
||
batches, err := server.store.ListHumanBatches(ctx, record.OrganizationID, domain.HumanBatchQuery{RunID: record.RunID})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
for _, batch := range batches {
|
||
if batch.Status == domain.HumanBatchActive {
|
||
batch.Status = domain.HumanBatchBlocked
|
||
batch.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertHumanBatch(ctx, batch); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
}
|
||
items, err := server.store.ListHumanItems(ctx, record.OrganizationID, domain.HumanItemQuery{RunID: record.RunID})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
for _, item := range items {
|
||
if terminalHumanItemStatus(item.Status) {
|
||
continue
|
||
}
|
||
updated := false
|
||
for attempt := 0; attempt < 2; attempt++ {
|
||
expectedVersion := item.Version
|
||
item.Status = domain.HumanItemCancelled
|
||
item.AssignedUserID = ""
|
||
item.ClaimedAt = ""
|
||
item.CancelledAt = nowISO()
|
||
item.CancelledByUserID = actorID
|
||
item.CancellationReason = reason
|
||
item.Version = expectedVersion + 1
|
||
item.UpdatedAt = nowISO()
|
||
updated, err = server.store.UpdateHumanItemVersioned(ctx, item, expectedVersion)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if updated {
|
||
break
|
||
}
|
||
latest, loadErr := server.store.GetHumanItem(ctx, item.ID)
|
||
if loadErr != nil {
|
||
return loadErr
|
||
}
|
||
if latest == nil {
|
||
return fmt.Errorf("human item %s disappeared while cancelling the Capture", item.ID)
|
||
}
|
||
if terminalHumanItemStatus(latest.Status) {
|
||
item = *latest
|
||
updated = true
|
||
break
|
||
}
|
||
item = *latest
|
||
}
|
||
if !updated {
|
||
return fmt.Errorf("human item %s changed repeatedly while cancelling the Capture", item.ID)
|
||
}
|
||
if item.Status != domain.HumanItemCancelled {
|
||
continue
|
||
}
|
||
if err := server.syncLegacyTaskFromHumanItem(ctx, item); err != nil {
|
||
return err
|
||
}
|
||
_ = server.appendHumanEvent(ctx, item, "run_cancelled", actorID, map[string]any{
|
||
"captureRecordId": record.ID,
|
||
"runId": record.RunID,
|
||
"reason": reason,
|
||
})
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (server *server) captureActionContext(writer http.ResponseWriter, request *http.Request) (*domain.WorkflowRecord, *domain.CaptureRecord, bool) {
|
||
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return nil, nil, false
|
||
}
|
||
if workflow == nil || !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
|
||
if workflow == nil {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
|
||
}
|
||
return nil, nil, false
|
||
}
|
||
record, err := server.store.GetCaptureRecord(request.Context(), chi.URLParam(request, "recordId"))
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return nil, nil, false
|
||
}
|
||
if record == nil || record.WorkflowID != workflow.ID || !server.ensureTenantAccess(writer, request, record.OrganizationID, "Capture") {
|
||
if record == nil || record.WorkflowID != workflow.ID {
|
||
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Capture not found"})
|
||
}
|
||
return nil, nil, false
|
||
}
|
||
return workflow, record, true
|
||
}
|
||
|
||
func (server *server) renderCaptureAutomationAfterAction(writer http.ResponseWriter, request *http.Request, workflow *domain.WorkflowRecord) {
|
||
records, err := server.store.ListCaptureRecords(request.Context(), workflow.OrganizationID, workflow.ID, 200)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
renderJSON(writer, http.StatusOK, server.captureAutomationPayload(request.Context(), workflow, records))
|
||
}
|
||
|
||
func (server *server) handleCaptureRecordCancel(writer http.ResponseWriter, request *http.Request) {
|
||
workflow, record, ok := server.captureActionContext(writer, request)
|
||
if !ok {
|
||
return
|
||
}
|
||
payload, ok := decodeCaptureAction(writer, request)
|
||
if !ok {
|
||
return
|
||
}
|
||
if payload.Mode == "" {
|
||
payload.Mode = string(domain.RunStopModeGraceful)
|
||
}
|
||
if payload.Mode != string(domain.RunStopModeGraceful) {
|
||
renderValidationError(writer, validationIssue{Field: "mode", Message: "Capture 取消仅支持 graceful 模式"})
|
||
return
|
||
}
|
||
switch record.Status {
|
||
case domain.CaptureRecordQueued, domain.CaptureRecordRunning, domain.CaptureRecordWaiting:
|
||
default:
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "当前 Capture 状态不允许取消处理"})
|
||
return
|
||
}
|
||
if strings.TrimSpace(record.RunID) == "" {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "Capture 缺少正在处理的 Run"})
|
||
return
|
||
}
|
||
run, err := server.store.RequestRunStop(request.Context(), record.RunID, domain.RunStopModeGraceful)
|
||
if err != nil || run == nil {
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
} else {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "Capture 对应的 Run 不存在"})
|
||
}
|
||
return
|
||
}
|
||
actorID := captureActionActorID(request)
|
||
if err := server.cancelCaptureHumanWork(request.Context(), record, actorID, payload.Reason); err != nil {
|
||
renderError(writer, http.StatusConflict, err)
|
||
return
|
||
}
|
||
action := newCaptureAction(domain.CaptureActionCancelled, record.Status, domain.CaptureRecordCancelled, actorID, payload.Reason, run.ID)
|
||
transitioned, err := server.store.TransitionCaptureRecord(request.Context(), record.ID, record.Status, domain.CaptureRecordCancelled, "", action)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !transitioned {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "Capture 状态已变化,请刷新后重试"})
|
||
return
|
||
}
|
||
server.renderCaptureAutomationAfterAction(writer, request, workflow)
|
||
}
|
||
|
||
func (server *server) handleCaptureRecordDiscard(writer http.ResponseWriter, request *http.Request) {
|
||
workflow, record, ok := server.captureActionContext(writer, request)
|
||
if !ok {
|
||
return
|
||
}
|
||
reason, ok := decodeCaptureActionReason(writer, request)
|
||
if !ok {
|
||
return
|
||
}
|
||
switch record.Status {
|
||
case domain.CaptureRecordIncomplete, domain.CaptureRecordReady, domain.CaptureRecordFailed, domain.CaptureRecordCancelled:
|
||
default:
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "当前 Capture 状态不允许永久放弃"})
|
||
return
|
||
}
|
||
action := newCaptureAction(domain.CaptureActionDiscarded, record.Status, domain.CaptureRecordDiscarded, captureActionActorID(request), reason, record.RunID)
|
||
if err := server.cancelCaptureHumanWork(request.Context(), record, action.ActorUserID, reason); err != nil {
|
||
renderError(writer, http.StatusConflict, err)
|
||
return
|
||
}
|
||
transitioned, err := server.store.TransitionCaptureRecord(request.Context(), record.ID, record.Status, domain.CaptureRecordDiscarded, record.Status, action)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !transitioned {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "Capture 状态已变化,请刷新后重试"})
|
||
return
|
||
}
|
||
server.renderCaptureAutomationAfterAction(writer, request, workflow)
|
||
}
|
||
|
||
func restorableCaptureStatus(status domain.CaptureRecordStatus) domain.CaptureRecordStatus {
|
||
switch status {
|
||
case domain.CaptureRecordIncomplete, domain.CaptureRecordReady, domain.CaptureRecordFailed, domain.CaptureRecordCancelled:
|
||
return status
|
||
default:
|
||
return domain.CaptureRecordIncomplete
|
||
}
|
||
}
|
||
|
||
func (server *server) handleCaptureRecordRestore(writer http.ResponseWriter, request *http.Request) {
|
||
workflow, record, ok := server.captureActionContext(writer, request)
|
||
if !ok {
|
||
return
|
||
}
|
||
reason, ok := decodeCaptureActionReason(writer, request)
|
||
if !ok {
|
||
return
|
||
}
|
||
if record.Status != domain.CaptureRecordDiscarded {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "只有已永久放弃的 Capture 可以恢复"})
|
||
return
|
||
}
|
||
targetStatus := restorableCaptureStatus(record.StatusBeforeDiscard)
|
||
action := newCaptureAction(domain.CaptureActionRestored, record.Status, targetStatus, captureActionActorID(request), reason, record.RunID)
|
||
transitioned, err := server.store.TransitionCaptureRecord(request.Context(), record.ID, domain.CaptureRecordDiscarded, targetStatus, "", action)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !transitioned {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "Capture 状态已变化,请刷新后重试"})
|
||
return
|
||
}
|
||
server.renderCaptureAutomationAfterAction(writer, request, workflow)
|
||
}
|
||
|
||
func captureWorkflowInputDefinition(workflow *domain.WorkflowRecord, inputID string) *domain.WorkflowInputDefinition {
|
||
if workflow == nil {
|
||
return nil
|
||
}
|
||
for index := range workflow.WorkflowInputs {
|
||
if workflow.WorkflowInputs[index].ID == inputID {
|
||
return &workflow.WorkflowInputs[index]
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func captureReprocessInput(workflow *domain.WorkflowRecord, record *domain.CaptureRecord) (map[string]domain.ArtifactPayload, error) {
|
||
if workflow == nil || workflow.CaptureAutomation == nil || record == nil {
|
||
return nil, &captureReprocessError{message: "工作流未配置持续处理"}
|
||
}
|
||
config := normalizeCaptureAutomationConfig(*workflow.CaptureAutomation)
|
||
input := captureWorkflowInputDefinition(workflow, config.WorkflowInputID)
|
||
if input == nil {
|
||
return nil, &captureReprocessError{message: "工作流缺少持续处理目录输入 " + config.WorkflowInputID}
|
||
}
|
||
ref := &domain.ArtifactReference{
|
||
Provider: domain.StorageProviderBOS, ConnectionID: config.ConnectionID, Bucket: config.Bucket,
|
||
Prefix: record.Prefix, VersionID: record.Fingerprint, Kind: domain.ArtifactRefKindDirectory,
|
||
}
|
||
if record.SourcePrefix != "" {
|
||
ref.Prefix = record.SourcePrefix
|
||
ref.ObjectKeys = append([]string(nil), record.SourceKeys...)
|
||
ref.CaptureUnit = record.CaptureID
|
||
}
|
||
return map[string]domain.ArtifactPayload{
|
||
config.WorkflowInputID: {Mode: domain.ArtifactTransferModeRef, Type: input.Type, Ref: ref},
|
||
}, nil
|
||
}
|
||
|
||
type captureReprocessError struct {
|
||
message string
|
||
}
|
||
|
||
func (err *captureReprocessError) Error() string { return err.message }
|
||
|
||
func (server *server) rollbackCaptureReprocess(request *http.Request, recordID string, previousStatus domain.CaptureRecordStatus, actorID, reason, runID string, cause error) {
|
||
failureReason := "重新处理创建失败"
|
||
if cause != nil && strings.TrimSpace(cause.Error()) != "" {
|
||
failureReason += ":" + strings.TrimSpace(cause.Error())
|
||
}
|
||
action := newCaptureAction(domain.CaptureActionReprocessFailed, domain.CaptureRecordRequeueing, previousStatus, actorID, failureReason, runID)
|
||
_, _ = server.store.TransitionCaptureRecord(request.Context(), recordID, domain.CaptureRecordRequeueing, previousStatus, "", action)
|
||
if current, _ := server.store.GetCaptureRecord(request.Context(), recordID); current != nil {
|
||
current.Error = failureReason
|
||
current.UpdatedAt = nowISO()
|
||
_ = server.store.UpsertCaptureRecord(request.Context(), *current)
|
||
}
|
||
}
|
||
|
||
func (server *server) handleCaptureRecordReprocess(writer http.ResponseWriter, request *http.Request) {
|
||
workflow, record, ok := server.captureActionContext(writer, request)
|
||
if !ok {
|
||
return
|
||
}
|
||
reason, ok := decodeCaptureActionReason(writer, request)
|
||
if !ok {
|
||
return
|
||
}
|
||
if record.Status != domain.CaptureRecordFailed && record.Status != domain.CaptureRecordCancelled {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "只有失败或已取消的 Capture 可以重新处理"})
|
||
return
|
||
}
|
||
if strings.TrimSpace(record.RunID) == "" {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "Capture 缺少可追溯的原 Run"})
|
||
return
|
||
}
|
||
actorID := captureActionActorID(request)
|
||
previousStatus := record.Status
|
||
requested := newCaptureAction(domain.CaptureActionReprocessRequested, previousStatus, domain.CaptureRecordRequeueing, actorID, reason, record.RunID)
|
||
transitioned, err := server.store.TransitionCaptureRecord(request.Context(), record.ID, previousStatus, domain.CaptureRecordRequeueing, "", requested)
|
||
if err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
if !transitioned {
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "Capture 已经在重新处理中,请勿重复提交"})
|
||
return
|
||
}
|
||
previousRun, err := server.store.GetRun(request.Context(), record.RunID)
|
||
if err != nil || previousRun == nil {
|
||
server.rollbackCaptureReprocess(request, record.ID, previousStatus, actorID, reason, record.RunID, err)
|
||
renderJSON(writer, http.StatusConflict, map[string]string{"error": "原 Run 不存在,无法重新处理"})
|
||
return
|
||
}
|
||
if err := server.cancelCaptureHumanWork(request.Context(), record, actorID, reason); err != nil {
|
||
server.rollbackCaptureReprocess(request, record.ID, previousStatus, actorID, reason, record.RunID, err)
|
||
renderError(writer, http.StatusConflict, err)
|
||
return
|
||
}
|
||
inputs, err := captureReprocessInput(workflow, record)
|
||
if err != nil {
|
||
server.rollbackCaptureReprocess(request, record.ID, previousStatus, actorID, reason, record.RunID, err)
|
||
renderValidationError(writer, validationIssue{Field: "capture", Message: err.Error()})
|
||
return
|
||
}
|
||
retryRun, err := server.createRunWithInputsAndLightweightManifests(request.Context(), workflow, domain.RunTriggerRetry, inputs, record.ID)
|
||
if err != nil {
|
||
server.rollbackCaptureReprocess(request, record.ID, previousStatus, actorID, reason, record.RunID, err)
|
||
if renderRunCreationError(writer, err) {
|
||
return
|
||
}
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
retryRun.ParentRunID = previousRun.ID
|
||
retryRun.Attempt = nextRunAttemptForChild(previousRun)
|
||
retryRun.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertRun(request.Context(), *retryRun); err != nil {
|
||
server.rollbackCaptureReprocess(request, record.ID, previousStatus, actorID, reason, record.RunID, err)
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
current, err := server.store.GetCaptureRecord(request.Context(), record.ID)
|
||
if err != nil || current == nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
current.RunID = retryRun.ID
|
||
current.Status = domain.CaptureRecordQueued
|
||
current.Error = ""
|
||
current.StartedAt = ""
|
||
current.CompletedAt = ""
|
||
current.StatusBeforeDiscard = ""
|
||
appendCaptureAction(current, newCaptureAction(domain.CaptureActionReprocessQueued, domain.CaptureRecordRequeueing, domain.CaptureRecordQueued, actorID, reason, retryRun.ID))
|
||
current.UpdatedAt = nowISO()
|
||
if err := server.store.UpsertCaptureRecord(request.Context(), *current); err != nil {
|
||
renderError(writer, http.StatusInternalServerError, err)
|
||
return
|
||
}
|
||
server.renderCaptureAutomationAfterAction(writer, request, workflow)
|
||
}
|