feat: add durable image processing pipeline
This commit is contained in:
@@ -23,6 +23,7 @@ type ObjectStore interface {
|
||||
|
||||
type Config struct {
|
||||
Prefix string
|
||||
OrphanPrefixes []string
|
||||
PageSize int
|
||||
GraceTime time.Duration
|
||||
UnreferencedRetention time.Duration
|
||||
@@ -65,6 +66,11 @@ func New(metadata MetadataStore, objects ObjectStore, config Config) (*Reconcile
|
||||
if config.UnreferencedRetention <= 0 {
|
||||
return nil, errors.New("unreferenced retention must be positive")
|
||||
}
|
||||
for _, prefix := range config.OrphanPrefixes {
|
||||
if prefix == "" || prefix == config.Prefix {
|
||||
return nil, errors.New("orphan prefix must be non-empty and distinct")
|
||||
}
|
||||
}
|
||||
if config.Now == nil {
|
||||
config.Now = time.Now
|
||||
}
|
||||
@@ -113,9 +119,35 @@ func (r *Reconciler) Run(ctx context.Context) (Report, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, prefix := range r.config.OrphanPrefixes {
|
||||
if err := r.scanOrphanPrefix(ctx, prefix, anomalyCutoff, &report); err != nil {
|
||||
return report, err
|
||||
}
|
||||
}
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func (r *Reconciler) scanOrphanPrefix(
|
||||
ctx context.Context,
|
||||
prefix string,
|
||||
cutoff time.Time,
|
||||
report *Report,
|
||||
) error {
|
||||
objects, err := newObjectIterator(ctx, r.objects, prefix, r.config.PageSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for objects.current != nil {
|
||||
if err := r.handleOrphanObject(ctx, *objects.current, cutoff, report); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := objects.advance(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Reconciler) handlePair(
|
||||
ctx context.Context,
|
||||
metadata model.Attachment,
|
||||
@@ -226,6 +258,14 @@ func (r *Reconciler) handleMissingObject(
|
||||
report *Report,
|
||||
) error {
|
||||
report.MetadataScanned++
|
||||
if metadata.Status == model.AttachmentPending {
|
||||
if metadata.CreatedAt.After(cutoff) {
|
||||
report.FreshPendingAttachments++
|
||||
return nil
|
||||
}
|
||||
report.OldPendingAttachments++
|
||||
return r.deletePair(ctx, metadata, cutoff, report)
|
||||
}
|
||||
if metadata.Status == model.AttachmentDeleting {
|
||||
return r.deleteClaimedMetadata(ctx, metadata, report)
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ func TestReconcilerLifecycleAndRepeat(t *testing.T) {
|
||||
"attachments/c/original": attachment("c", "attachments/c/original", model.AttachmentPending, old, false),
|
||||
"attachments/d/original": attachment("d", "attachments/d/original", model.AttachmentReady, fresh, false),
|
||||
"attachments/missing/original": attachment("missing", "attachments/missing/original", model.AttachmentReady, old, false),
|
||||
"staging/pending/original": attachment("pending", "staging/pending/original", model.AttachmentPending, old, false),
|
||||
}}
|
||||
objects := &objectStub{items: map[string]model.StoredObject{
|
||||
"attachments/a/original": {Key: "attachments/a/original", LastModified: old},
|
||||
@@ -28,27 +29,33 @@ func TestReconcilerLifecycleAndRepeat(t *testing.T) {
|
||||
"attachments/c/original": {Key: "attachments/c/original", LastModified: old},
|
||||
"attachments/d/original": {Key: "attachments/d/original", LastModified: fresh},
|
||||
"attachments/orphan/original": {Key: "attachments/orphan/original", LastModified: old},
|
||||
"staging/orphan/original": {Key: "staging/orphan/original", LastModified: old},
|
||||
"staging/pending/original": {Key: "staging/pending/original", LastModified: old},
|
||||
}}
|
||||
reconciler, err := New(metadata, objects, Config{
|
||||
Prefix: "attachments/", PageSize: 2, GraceTime: time.Hour,
|
||||
OrphanPrefixes: []string{"staging/"},
|
||||
UnreferencedRetention: 24 * time.Hour, Apply: true, Now: func() time.Time { return now },
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
report, err := reconciler.Run(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, report.OldPendingAttachments)
|
||||
require.Equal(t, 2, report.OldPendingAttachments)
|
||||
require.Equal(t, 1, report.OldUnreferencedAttachments)
|
||||
require.Equal(t, 1, report.FreshUnreferencedAttachments)
|
||||
require.Equal(t, 1, report.DeletedOrphanObjects)
|
||||
require.Equal(t, 2, report.DeletedAttachmentObjects)
|
||||
require.Equal(t, 3, report.DeletedUnreferencedRows)
|
||||
require.Equal(t, 2, report.DeletedOrphanObjects)
|
||||
require.Equal(t, 3, report.DeletedAttachmentObjects)
|
||||
require.Equal(t, 4, report.DeletedUnreferencedRows)
|
||||
require.Contains(t, metadata.items, "attachments/b/original")
|
||||
require.Contains(t, metadata.items, "attachments/d/original")
|
||||
require.NotContains(t, metadata.items, "attachments/a/original")
|
||||
require.NotContains(t, metadata.items, "attachments/c/original")
|
||||
require.NotContains(t, metadata.items, "attachments/missing/original")
|
||||
require.NotContains(t, metadata.items, "staging/pending/original")
|
||||
require.NotContains(t, objects.items, "attachments/orphan/original")
|
||||
require.NotContains(t, objects.items, "staging/orphan/original")
|
||||
require.NotContains(t, objects.items, "staging/pending/original")
|
||||
|
||||
repeated, err := reconciler.Run(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user