feat: clean up staging objects after processing and during reconciliation

This commit is contained in:
kk10
2026-11-16 11:48:30 +03:00
parent 19152e7d18
commit b35faa5af8
7 changed files with 270 additions and 66 deletions
+42
View File
@@ -238,6 +238,13 @@ func (w *Worker) handle(ctx context.Context, job model.AttachmentJob) error {
return err
}
if row.StagingKey != nil {
err := w.deleteStagingObject(ctx, *row.StagingKey)
if err != nil && ctx.Err() == nil {
w.log.Warn("delete ready attachment staging", "attachment_id", row.ID, "error", err)
}
}
w.log.Info("attachment ready", "job_id", job.JobID, "attachment_id", row.ID, "attempt", job.Attempt)
return nil
}
@@ -391,6 +398,41 @@ func (w *Worker) finishFailure(ctx context.Context, job model.AttachmentJob, cau
return nil
}
func (w *Worker) deleteStagingObject(ctx context.Context, key string) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
tx, err := w.pool.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(context.Background())
q := w.q.WithTx(tx)
row, err := q.LockAttachmentByStagingKey(ctx, key)
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
if err != nil {
return err
}
if row.Status != db.AttachmentStatusReady {
return nil
}
if err := w.s3.DeleteAdmin(ctx, key); err != nil {
return err
}
if err := q.ClearAttachmentStagingKey(ctx, row.ID); err != nil {
return err
}
return tx.Commit(ctx)
}
func (w *Worker) recoverLoop(ctx context.Context) {
for ctx.Err() == nil {
recovered, err := w.recoverOne(ctx)