forked from mtusi2026/mtusi2026lab5
feat(attachments): accept uploads through staging and transactional outbox
This commit is contained in:
@@ -32,13 +32,10 @@ type Report struct {
|
||||
ListPages int `json:"list_pages"`
|
||||
OrphanObjects []string `json:"orphan_objects"`
|
||||
MissingAttachments []uuid.UUID `json:"missing_attachments"`
|
||||
StaleUploads []uuid.UUID `json:"stale_uploads"`
|
||||
CorruptObjects []string `json:"corrupt_objects"`
|
||||
SkippedFresh int `json:"skipped_fresh"`
|
||||
SkippedReferenced int `json:"skipped_referenced"`
|
||||
SkippedInactive int `json:"skipped_inactive"`
|
||||
DeletedObjects int `json:"deleted_objects"`
|
||||
DeletedRows int `json:"deleted_rows"`
|
||||
MarkedFailed int `json:"marked_failed"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
Errors []string `json:"errors"`
|
||||
@@ -52,7 +49,6 @@ type ObjectClient interface {
|
||||
|
||||
type AttachmentQueries interface {
|
||||
ListAttachmentsForReconcile(ctx context.Context) ([]db.ListAttachmentsForReconcileRow, error)
|
||||
DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int64, error)
|
||||
MarkAttachmentFailedIfReady(ctx context.Context, arg db.MarkAttachmentFailedIfReadyParams) (int64, error)
|
||||
}
|
||||
|
||||
@@ -64,13 +60,11 @@ type Reconciler struct {
|
||||
}
|
||||
|
||||
type plan struct {
|
||||
orphans []s3store.ObjectInfo
|
||||
staleUploads []db.ListAttachmentsForReconcileRow
|
||||
missing []db.ListAttachmentsForReconcileRow
|
||||
corrupt []s3store.ObjectInfo
|
||||
skippedFresh int
|
||||
skippedReferenced int
|
||||
skippedInactive int
|
||||
orphans []s3store.ObjectInfo
|
||||
missing []db.ListAttachmentsForReconcileRow
|
||||
corrupt []s3store.ObjectInfo
|
||||
skippedFresh int
|
||||
skippedInactive int
|
||||
}
|
||||
|
||||
func New(store ObjectClient, queries AttachmentQueries, log *slog.Logger) *Reconciler {
|
||||
@@ -93,7 +87,6 @@ func (r *Reconciler) Run(ctx context.Context, opts Options) (Report, error) {
|
||||
report := Report{
|
||||
OrphanObjects: []string{},
|
||||
MissingAttachments: []uuid.UUID{},
|
||||
StaleUploads: []uuid.UUID{},
|
||||
CorruptObjects: []string{},
|
||||
DryRun: opts.DryRun,
|
||||
Errors: []string{},
|
||||
@@ -121,15 +114,11 @@ func (r *Reconciler) Run(ctx context.Context, opts Options) (Report, error) {
|
||||
for _, obj := range p.corrupt {
|
||||
report.CorruptObjects = append(report.CorruptObjects, obj.Key)
|
||||
}
|
||||
for _, row := range p.staleUploads {
|
||||
report.StaleUploads = append(report.StaleUploads, row.ID)
|
||||
}
|
||||
for _, row := range p.missing {
|
||||
report.MissingAttachments = append(report.MissingAttachments, row.ID)
|
||||
}
|
||||
|
||||
report.SkippedFresh = p.skippedFresh
|
||||
report.SkippedReferenced = p.skippedReferenced
|
||||
report.SkippedInactive = p.skippedInactive
|
||||
|
||||
if !opts.DryRun {
|
||||
@@ -146,10 +135,8 @@ func (r *Reconciler) Run(ctx context.Context, opts Options) (Report, error) {
|
||||
"list_pages", report.ListPages,
|
||||
"orphan_objects", len(report.OrphanObjects),
|
||||
"missing_attachments", len(report.MissingAttachments),
|
||||
"stale_uploads", len(report.StaleUploads),
|
||||
"corrupt_objects", len(report.CorruptObjects),
|
||||
"deleted_objects", report.DeletedObjects,
|
||||
"deleted_rows", report.DeletedRows,
|
||||
"marked_failed", report.MarkedFailed,
|
||||
"errors", len(report.Errors),
|
||||
)
|
||||
@@ -189,15 +176,7 @@ func buildPlan(
|
||||
|
||||
switch row.Status {
|
||||
case db.AttachmentStatusQueued:
|
||||
if now.Sub(row.CreatedAt) < grace {
|
||||
p.skippedFresh++
|
||||
continue
|
||||
}
|
||||
if row.Referenced {
|
||||
p.skippedReferenced++
|
||||
continue
|
||||
}
|
||||
p.staleUploads = append(p.staleUploads, row)
|
||||
p.skippedInactive++
|
||||
case db.AttachmentStatusReady:
|
||||
if !exists {
|
||||
p.missing = append(p.missing, row)
|
||||
@@ -227,9 +206,6 @@ func buildPlan(
|
||||
sort.Slice(p.corrupt, func(i, j int) bool {
|
||||
return p.corrupt[i].Key < p.corrupt[j].Key
|
||||
})
|
||||
sort.Slice(p.staleUploads, func(i, j int) bool {
|
||||
return p.staleUploads[i].ID.String() < p.staleUploads[j].ID.String()
|
||||
})
|
||||
sort.Slice(p.missing, func(i, j int) bool {
|
||||
return p.missing[i].ID.String() < p.missing[j].ID.String()
|
||||
})
|
||||
@@ -240,31 +216,6 @@ func buildPlan(
|
||||
func (r *Reconciler) apply(ctx context.Context, opts Options, p plan, report *Report) {
|
||||
deletes := 0
|
||||
|
||||
for _, row := range p.staleUploads {
|
||||
if deletes >= opts.MaxDeletes {
|
||||
report.Errors = append(report.Errors, "max deletes reached")
|
||||
break
|
||||
}
|
||||
|
||||
removed, err := r.q.DeleteQueuedAttachment(ctx, row.ID)
|
||||
if err != nil {
|
||||
report.Errors = append(report.Errors, fmt.Sprintf("delete row %s: %v", row.ID, err))
|
||||
continue
|
||||
}
|
||||
if removed == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
deletes++
|
||||
report.DeletedRows++
|
||||
|
||||
if err := r.store.DeleteAdmin(ctx, row.S3Key); err != nil {
|
||||
report.Errors = append(report.Errors, fmt.Sprintf("delete object %s: %v", row.S3Key, err))
|
||||
continue
|
||||
}
|
||||
report.DeletedObjects++
|
||||
}
|
||||
|
||||
for _, obj := range p.orphans {
|
||||
if deletes >= opts.MaxDeletes {
|
||||
report.Errors = append(report.Errors, "max deletes reached")
|
||||
|
||||
Reference in New Issue
Block a user