feat(worker): add safe failed attachment replay CLI

This commit is contained in:
kk10
2026-11-16 20:35:48 +03:00
parent f2000a7883
commit 6e09da0b46
4 changed files with 292 additions and 23 deletions
+5 -4
View File
@@ -12,6 +12,7 @@ import (
"mtusi2026-messenger/internal/db"
"mtusi2026-messenger/internal/model"
s3store "mtusi2026-messenger/internal/s3"
)
var (
@@ -128,13 +129,13 @@ func (w *Worker) duplicate(ctx context.Context, job model.AttachmentJob) error {
return err
}
if err := w.verify(ctx, row.S3Key, row.SizeBytes, row.Sha256); err != nil {
if err := verify(ctx, w.s3, row.S3Key, row.SizeBytes, row.Sha256); err != nil {
return err
}
hasThumbnail := false
for _, variant := range variants {
if err := w.verify(ctx, variant.S3Key, variant.SizeBytes, variant.Sha256); err != nil {
if err := verify(ctx, w.s3, variant.S3Key, variant.SizeBytes, variant.Sha256); err != nil {
return err
}
@@ -161,8 +162,8 @@ func (w *Worker) duplicate(ctx context.Context, job model.AttachmentJob) error {
return errAttemptUnavailable
}
func (w *Worker) verify(ctx context.Context, key string, size int, checksum string) error {
object, err := w.s3.Get(ctx, key)
func verify(ctx context.Context, storage *s3store.Client, key string, size int, checksum string) error {
object, err := storage.Get(ctx, key)
if err != nil {
return err
}
+86
View File
@@ -0,0 +1,86 @@
package worker
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"mtusi2026-messenger/internal/db"
s3store "mtusi2026-messenger/internal/s3"
)
var (
errAttachmentNotReplayable = errors.New("attachment is not replayable: expected failed with staging and no existing variants")
errMissingPipelineJob = errors.New("attachment has no pipeline job")
errReplayNotUpdated = errors.New("attachment replay did not update one row")
)
type txBeginner interface {
Begin(ctx context.Context) (pgx.Tx, error)
}
func Replay(
ctx context.Context,
pool txBeginner,
storage *s3store.Client,
id uuid.UUID,
operator, reason string,
) error {
tx, err := pool.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(context.Background())
q := db.New(tx)
row, err := q.LockFailedAttachmentForReplay(ctx, id)
if errors.Is(err, pgx.ErrNoRows) {
return errAttachmentNotReplayable
}
if err != nil {
return err
}
if row.JobID == nil {
return errMissingPipelineJob
}
if err := verify(ctx, storage, *row.StagingKey, row.SizeBytes, row.Sha256); err != nil {
return fmt.Errorf("verify replay source: %w", err)
}
n, err := q.ReplayAttachmentJob(ctx, id)
if err != nil {
return err
}
if n != 1 {
return errReplayNotUpdated
}
if err := q.CreateAttachmentReplay(ctx, db.CreateAttachmentReplayParams{
AttachmentID: id,
Attempt: row.Attempts + 1,
PreviousErrorCode: row.ErrorCode,
Operator: operator,
Reason: reason,
}); err != nil {
return err
}
if _, err := q.CreateOutboxEntry(ctx, db.CreateOutboxEntryParams{
AttachmentID: id,
Attempt: row.Attempts + 1,
AvailableAt: time.Now(),
}); err != nil {
return err
}
if err := tx.Commit(ctx); err != nil {
return err
}
return nil
}