forked from mtusi2026/mtusi2026lab5
feat:report
This commit is contained in:
+8
-1
@@ -459,7 +459,14 @@ func (h *Handler) UploadAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
pendingID, err := h.Store.CreateAttachmentPending(
|
||||
r.Context(), chatID, me.ID, stagingKey, filename)
|
||||
if err != nil {
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable")
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrInvalid):
|
||||
writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid attachment metadata")
|
||||
case errors.Is(err, domain.ErrTimeout):
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout")
|
||||
default:
|
||||
writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -9,4 +9,4 @@ const (
|
||||
|
||||
const PipelineVersion = 1
|
||||
|
||||
var RequiredVariants = []string{"original", "thumbnail", "crop", "optimized"}
|
||||
var RequiredVariants = []string{"thumbnail", "crop", "optimized"}
|
||||
|
||||
+40
-23
@@ -3,6 +3,7 @@ package domain
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
@@ -48,12 +49,12 @@ func (s *Store) GetAttachmentForUser(ctx context.Context, id, userID string) (*A
|
||||
var errCode *string
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, a.size_bytes, a.sha256,
|
||||
a.status, a.error_code, a.created_at, a.updated_at, a.pipeline_version
|
||||
FROM attachments a
|
||||
JOIN chat_members m ON m.chat_id = a.chat_id
|
||||
WHERE a.id=$1 AND m.user_id=$2`, id, userID,
|
||||
a.status, a.error_code, a.created_at, a.updated_at, a.pipeline_version
|
||||
FROM attachments a
|
||||
JOIN chat_members m ON m.chat_id = a.chat_id
|
||||
WHERE a.id=$1 AND m.user_id=$2`, id, userID,
|
||||
).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256,
|
||||
&a.Status, &errCode, &a.CreatedAt, &a.UpdatedAt)
|
||||
&a.Status, &errCode, &a.CreatedAt, &a.UpdatedAt, &a.PipelineVersion)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
@@ -64,10 +65,11 @@ func (s *Store) GetAttachmentForUser(ctx context.Context, id, userID string) (*A
|
||||
a.Variants = []Variant{}
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`SELECT variant, content_type, size_bytes, width, height, sha256, object_key
|
||||
FROM attachment_variants
|
||||
WHERE attachment_id=$1 AND pipeline_version=$2
|
||||
ORDER BY variant`,
|
||||
a.ID, a.PipelineVersion)
|
||||
FROM attachment_variants
|
||||
WHERE attachment_id=$1 AND pipeline_version=$2
|
||||
AND variant = ANY($3)
|
||||
ORDER BY variant`,
|
||||
a.ID, a.PipelineVersion, RequiredVariants)
|
||||
if err != nil {
|
||||
return nil, classifyDBError(err)
|
||||
}
|
||||
@@ -111,10 +113,13 @@ func (s *Store) ValidateAttachmentIDsForChat(ctx context.Context, chatID string,
|
||||
return ErrInvalid
|
||||
}
|
||||
seen[id] = true
|
||||
var ok bool
|
||||
var status string
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT true FROM attachments WHERE id=$1 AND chat_id=$2 AND status='ready'`,
|
||||
id, chatID).Scan(&ok)
|
||||
`SELECT status FROM attachments WHERE id=$1 AND chat_id=$2`,
|
||||
id, chatID).Scan(&status)
|
||||
if err == nil && status == AttachmentFailed {
|
||||
return ErrInvalid
|
||||
}
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
@@ -199,10 +204,10 @@ func (s *Store) EnqueueAttachment(
|
||||
pipeline_version=$7, status='queued', updated_at=now()
|
||||
WHERE id=$1 AND status='queued'
|
||||
RETURNING id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
||||
status, error_code, created_at, updated_at`,
|
||||
status, error_code, created_at, updated_at, pipeline_version`,
|
||||
attachmentID, contentType, size, sha, w, h, pipelineVersion,
|
||||
).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes,
|
||||
&a.SHA256, &a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt)
|
||||
&a.SHA256, &a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt, &a.PipelineVersion)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
@@ -312,10 +317,13 @@ func (s *Store) MarkReady(
|
||||
}
|
||||
|
||||
for _, v := range variants {
|
||||
if v.Name == "original" {
|
||||
continue // original живёт в attachments.object_key, не в variants
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO attachment_variants
|
||||
(attachment_id, pipeline_version, variant, object_key,
|
||||
content_type, size_bytes, width, height, sha256)
|
||||
(attachment_id, pipeline_version, variant, object_key,
|
||||
content_type, size_bytes, width, height, sha256)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)
|
||||
ON CONFLICT (attachment_id, pipeline_version, variant) DO NOTHING`,
|
||||
attachmentID, pipelineVersion, v.Name, "final/"+attachmentID+"/"+v.Name,
|
||||
@@ -374,6 +382,8 @@ func (s *Store) RetryJob(
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return false, classifyDBError(err)
|
||||
}
|
||||
log.Printf("retry job=%s exhausted attempts=%d code=%s",
|
||||
jobID, attempt, errorCode)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -388,6 +398,7 @@ func (s *Store) RetryJob(
|
||||
status = 'queued',
|
||||
last_error_code=$2,
|
||||
next_attempt_at=$3,
|
||||
published_at=NULL,
|
||||
lease_owner=NULL, lease_expires_at=NULL, updated_at=now()
|
||||
WHERE job_id=$1`, jobID, errorCode, next); err != nil {
|
||||
return false, classifyDBError(err)
|
||||
@@ -395,16 +406,21 @@ func (s *Store) RetryJob(
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return false, classifyDBError(err)
|
||||
}
|
||||
// Логируем зафиксированный retry — используется в student-tests/backoff.py
|
||||
log.Printf("retry job=%s attempt=%d next_attempt=%s backoff=%s jitter=%s code=%s",
|
||||
jobID, attempt+1, next.UTC().Format(time.RFC3339Nano),
|
||||
backoff, jitter, errorCode)
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// RequeueStuck — reaper: обрабатывается отдельным тикером в worker/outbox.
|
||||
func (s *Store) RequeueStuck(ctx context.Context) (int64, error) {
|
||||
tag, err := s.pool.Exec(ctx, `
|
||||
UPDATE attachment_jobs
|
||||
SET status='queued', lease_owner=NULL, lease_expires_at=NULL,
|
||||
next_attempt_at = now(), updated_at = now()
|
||||
WHERE status='processing' AND lease_expires_at < now()`)
|
||||
UPDATE attachment_jobs
|
||||
SET status='queued', lease_owner=NULL, lease_expires_at=NULL,
|
||||
next_attempt_at = now(), published_at = NULL, updated_at = now()
|
||||
WHERE status='processing' AND lease_expires_at < now()`)
|
||||
if err != nil {
|
||||
return 0, classifyDBError(err)
|
||||
}
|
||||
@@ -421,9 +437,10 @@ func (s *Store) ReplayFailed(ctx context.Context, attachmentID string) error {
|
||||
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE attachment_jobs
|
||||
SET status='queued', attempt=1, next_attempt_at=now(),
|
||||
last_error_code=NULL, lease_owner=NULL, lease_expires_at=NULL, updated_at=now()
|
||||
WHERE attachment_id=$1 AND status='failed'`, attachmentID)
|
||||
SET status='queued', attempt=1, next_attempt_at=now(),
|
||||
published_at=NULL,
|
||||
last_error_code=NULL, lease_owner=NULL, lease_expires_at=NULL, updated_at=now()
|
||||
WHERE attachment_id=$1 AND status='failed'`, attachmentID)
|
||||
if err != nil {
|
||||
return classifyDBError(err)
|
||||
}
|
||||
|
||||
+14
-3
@@ -526,15 +526,26 @@ func (s *Store) AddMessageIdempotent(
|
||||
return nil, nil, false, ErrInvalid
|
||||
}
|
||||
seen[aid] = true
|
||||
var ok bool
|
||||
var (
|
||||
status string
|
||||
objectKey string
|
||||
)
|
||||
// Вложение из этого же чата; не обязательно ready — клиент увидит картинку,
|
||||
// когда worker завершит обработку. failed — принимаем: сообщение уже создано,
|
||||
// UI покажет «failed» через GET /attachments/{id}.
|
||||
if err := tx.QueryRow(ctx,
|
||||
`SELECT true FROM attachments WHERE id=$1 AND chat_id=$2 AND status='ready'`,
|
||||
aid, chatID).Scan(&ok); err != nil {
|
||||
`SELECT status, object_key FROM attachments WHERE id=$1 AND chat_id=$2`,
|
||||
aid, chatID).Scan(&status, &objectKey); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil, false, ErrNotFound
|
||||
}
|
||||
return nil, nil, false, classifyDBError(err)
|
||||
}
|
||||
if status == AttachmentFailed {
|
||||
// Нельзя прикреплять заведомо неудачные вложения — иначе UI будет
|
||||
// навсегда показывать сломанную картинку без пути к replay.
|
||||
return nil, nil, false, ErrInvalid
|
||||
}
|
||||
}
|
||||
|
||||
// 2) Уже был такой ключ?
|
||||
|
||||
Reference in New Issue
Block a user