forked from mtusi2026/mtusi2026lab5
fix: preserve attachment pipeline invariants
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
type MetadataStore interface {
|
||||
ListAttachments(context.Context, string, int) ([]model.Attachment, string, error)
|
||||
AttachmentObjectKeys(context.Context, string) ([]string, error)
|
||||
AttachmentExistsByObjectKey(context.Context, string) (bool, error)
|
||||
ClaimAttachmentForDeletion(context.Context, model.Attachment, time.Time) (bool, error)
|
||||
DeleteAttachmentIfDeleting(context.Context, string) (bool, error)
|
||||
@@ -158,6 +159,9 @@ func (r *Reconciler) handlePair(
|
||||
report.MetadataScanned++
|
||||
report.ObjectsScanned++
|
||||
switch metadata.Status {
|
||||
case model.AttachmentQueued, model.AttachmentProcessing, model.AttachmentFailed:
|
||||
report.Consistent++
|
||||
return nil
|
||||
case model.AttachmentPending:
|
||||
if metadata.CreatedAt.After(anomalyCutoff) {
|
||||
report.FreshPendingAttachments++
|
||||
@@ -208,10 +212,16 @@ func (r *Reconciler) deleteClaimedPair(ctx context.Context, attachment model.Att
|
||||
if !r.config.Apply {
|
||||
return nil
|
||||
}
|
||||
if err := r.objects.Delete(ctx, attachment.ObjectKey); err != nil {
|
||||
return fmt.Errorf("delete attachment object %q: %w", attachment.ObjectKey, err)
|
||||
objectKeys, err := r.metadata.AttachmentObjectKeys(ctx, attachment.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list attachment %q object keys: %w", attachment.ID, err)
|
||||
}
|
||||
for _, objectKey := range objectKeys {
|
||||
if err := r.objects.Delete(ctx, objectKey); err != nil {
|
||||
return fmt.Errorf("delete attachment object %q: %w", objectKey, err)
|
||||
}
|
||||
report.DeletedAttachmentObjects++
|
||||
}
|
||||
report.DeletedAttachmentObjects++
|
||||
deleted, err := r.metadata.DeleteAttachmentIfDeleting(ctx, attachment.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete attachment metadata %q: %w", attachment.ID, err)
|
||||
@@ -229,6 +239,14 @@ func (r *Reconciler) handleOrphanObject(
|
||||
report *Report,
|
||||
) error {
|
||||
report.ObjectsScanned++
|
||||
exists, err := r.metadata.AttachmentExistsByObjectKey(ctx, object.Key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("recheck orphan object %q: %w", object.Key, err)
|
||||
}
|
||||
if exists {
|
||||
report.Consistent++
|
||||
return nil
|
||||
}
|
||||
if object.LastModified.After(cutoff) {
|
||||
report.FreshOrphanObjects++
|
||||
return nil
|
||||
@@ -237,13 +255,6 @@ func (r *Reconciler) handleOrphanObject(
|
||||
if !r.config.Apply {
|
||||
return nil
|
||||
}
|
||||
exists, err := r.metadata.AttachmentExistsByObjectKey(ctx, object.Key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("recheck orphan object %q: %w", object.Key, err)
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
if err := r.objects.Delete(ctx, object.Key); err != nil {
|
||||
return fmt.Errorf("delete orphan object %q: %w", object.Key, err)
|
||||
}
|
||||
@@ -258,6 +269,11 @@ func (r *Reconciler) handleMissingObject(
|
||||
report *Report,
|
||||
) error {
|
||||
report.MetadataScanned++
|
||||
if metadata.Status == model.AttachmentQueued ||
|
||||
metadata.Status == model.AttachmentProcessing ||
|
||||
metadata.Status == model.AttachmentFailed {
|
||||
return nil
|
||||
}
|
||||
if metadata.Status == model.AttachmentPending {
|
||||
if metadata.CreatedAt.After(cutoff) {
|
||||
report.FreshPendingAttachments++
|
||||
|
||||
Reference in New Issue
Block a user