droboticflow/internal/domain/slice_processing.go

45 lines
1.6 KiB
Go

package domain
import "time"
const SliceProcessingLeaseDuration = 2 * time.Minute
// ExpireSliceProcessing only changes active, expired work. Callers must persist
// it with the item's version as a fence against a concurrent heartbeat/reset.
func ExpireSliceProcessing(item *HumanItemRecord, now time.Time) bool {
if item == nil || item.CurrentStageID != FanyuV58StageAnnotationCombined || (item.Status != HumanItemClaimed && item.Status != HumanItemInProgress) {
return false
}
w, ok := item.Subject.Payload["sliceWorkflow"].(map[string]any)
if !ok || w["status"] != "processing" {
return false
}
// Both API reads and the background worker enter this boundary. Durable jobs
// have their own renewable lease; this timestamp only belongs to legacy work.
requestID, _ := w["requestId"].(string)
if HasActiveAnnotationJob(item, "slice", requestID) {
return false
}
lease, _ := w["leaseExpiresAt"].(string)
expires, err := time.Parse(time.RFC3339Nano, lease)
if err != nil {
started, _ := w["startedAt"].(string)
t, _ := time.Parse(time.RFC3339Nano, started)
expires = t.Add(SliceProcessingLeaseDuration)
}
if now.Before(expires) {
return false
}
next := make(map[string]any, len(w)+3)
for k, v := range w {
next[k] = v
}
next["status"] = "failed"
next["errorCode"] = "slice_processing_interrupted"
next["error"] = "切片进程已中断或失去心跳,切片边界已保留。请返回调整切片后重新确认。"
next["updatedAt"] = now.Format(time.RFC3339Nano)
item.Subject.Payload["sliceWorkflow"] = next
item.UpdatedAt = now.Format(time.RFC3339Nano)
return true
}