forked from longtao.wu/droboticflow
37 lines
1.3 KiB
Go
37 lines
1.3 KiB
Go
package objectsync
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"fmt"
|
|
)
|
|
|
|
// FinalizeDirectUpload consumes the trusted observation from the upload
|
|
// completion handler. Worker recovery uses commitItem's normal fresh HEAD.
|
|
func (coordinator *Coordinator) FinalizeDirectUpload(ctx context.Context, id string, observed ObjectMetadata) (*domain.ObjectSyncItemRecord, error) {
|
|
item, err := coordinator.syncStore.ClaimUploadedObjectSyncItem(ctx, id, coordinator.owner, claimTTL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if item == nil {
|
|
return coordinator.syncStore.GetObjectSyncItem(ctx, id)
|
|
}
|
|
if item.StagingKey != item.TargetKey || item.Action != "create" {
|
|
return nil, fmt.Errorf("verified finalize requires direct create")
|
|
}
|
|
token := item.CoordinatorOwner
|
|
err = coordinator.commitItemWithObservation(ctx, item, &observed)
|
|
if err != nil {
|
|
latest, readErr := coordinator.syncStore.GetObjectSyncItem(ctx, id)
|
|
if readErr == nil && latest != nil && latest.Status == domain.ObjectSyncItemCommitting && latest.CoordinatorOwner == token {
|
|
latest.CoordinatorOwner = ""
|
|
latest.CoordinatorLeaseUntil = latest.CoordinatorLeaseUntil.Add(-claimTTL)
|
|
latest.LastError = err.Error()
|
|
_ = coordinator.saveClaimedItem(ctx, *latest, token)
|
|
}
|
|
return latest, err
|
|
}
|
|
|
|
return item, nil
|
|
}
|