feat: add durable image processing pipeline

This commit is contained in:
2026-10-22 17:22:28 +03:00
parent 8c2ac690a6
commit 42336ce7c2
39 changed files with 4379 additions and 126 deletions
+19 -9
View File
@@ -51,13 +51,16 @@ func (s *Store) CreateMessage(
if !visible {
return model.Message{}, false, ErrNotFound
}
validAttachments, err := attachmentsBelongToChat(ctx, tx, chatID, attachmentIDs)
validAttachments, failedAttachment, err := attachmentsBelongToChat(ctx, tx, chatID, attachmentIDs)
if err != nil {
return model.Message{}, false, err
}
if !validAttachments {
return model.Message{}, false, ErrNotFound
}
if failedAttachment {
return model.Message{}, false, ErrConflict
}
if err = deleteExpiredIdempotencyRecords(ctx, tx, expiredIdempotencyCleanupBatch); err != nil {
return model.Message{}, false, err
}
@@ -92,28 +95,35 @@ func messageRequestHash(text string, attachmentIDs []string) ([sha256.Size]byte,
return sha256.Sum256(payload), nil
}
func attachmentsBelongToChat(ctx context.Context, tx pgx.Tx, chatID string, attachmentIDs []string) (bool, error) {
func attachmentsBelongToChat(ctx context.Context, tx pgx.Tx, chatID string, attachmentIDs []string) (bool, bool, error) {
if len(attachmentIDs) == 0 {
return true, nil
return true, false, nil
}
const query = `
SELECT id
SELECT id, status
FROM attachments
WHERE chat_id = $1 AND id::text = ANY($2) AND status = 'ready'
WHERE chat_id = $1 AND id::text = ANY($2)
AND status IN ('queued', 'processing', 'ready', 'failed')
FOR UPDATE`
rows, err := tx.Query(ctx, query, chatID, attachmentIDs)
if err != nil {
return false, fmt.Errorf("validate message attachments: %w", err)
return false, false, fmt.Errorf("validate message attachments: %w", err)
}
defer rows.Close()
count := 0
var count int
var failed bool
for rows.Next() {
var id, status string
if err := rows.Scan(&id, &status); err != nil {
return false, false, fmt.Errorf("scan message attachment: %w", err)
}
count++
failed = failed || status == model.AttachmentFailed
}
if err := rows.Err(); err != nil {
return false, fmt.Errorf("validate message attachments: %w", err)
return false, false, fmt.Errorf("validate message attachments: %w", err)
}
return count == len(attachmentIDs), nil
return count == len(attachmentIDs), failed, nil
}
func chatVisibleInTransaction(ctx context.Context, tx pgx.Tx, chatID, userID string) (bool, error) {