Реализовать уровень 3: очередь RabbitMQ и обработку изображений

Добавить устойчивый приём с ответом 202, durable jobs, dispatcher с publisher confirms и отдельный worker с ручным ACK. Формировать thumbnail, ограничить retry и защитить результат от повторной доставки. Добавить проверки очереди и адаптировать клиент к асинхронным состояниям.
This commit is contained in:
Dima
2026-11-04 19:32:01 +03:00
parent 9dfb3e5fc4
commit 495ad58cb7
27 changed files with 834 additions and 30 deletions
+6 -3
View File
@@ -37,10 +37,13 @@ func (s *AttachmentStore) PublishUpload(ctx context.Context, a Attachment, put f
if err = put(ctx); err != nil {
return err
}
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, "originals/"+a.ID, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
if err != nil {
return err
}
if err = enqueueJob(ctx, tx, a, a.ObjectKey); err != nil {
return err
}
if _, err = tx.Exec(ctx, "DELETE FROM upload_attempts WHERE id=$1", a.ID); err != nil {
return err
}
@@ -120,11 +123,11 @@ func (s *AttachmentStore) cleanupUpload(ctx context.Context, id string, dryRun b
}
// Restrict destructive actions to server-generated keys even if a journal row is malformed.
parsed, err := uuid.Parse(id)
if err != nil || key != "originals/"+parsed.String() {
if err != nil || (key != "originals/"+parsed.String() && key != "staging/jobs/"+parsed.String()) {
return "", errors.New("invalid upload journal key")
}
var ready bool
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM attachments WHERE object_key=$1)", key).Scan(&ready); err != nil {
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM attachments WHERE object_key=$1 UNION ALL SELECT 1 FROM attachment_jobs WHERE staging_key=$1)", key).Scan(&ready); err != nil {
return "", err
}
if dryRun {