1274 lines
43 KiB
Go
1274 lines
43 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"embed"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"mtusi2026lab5_fork/src/internal/model"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var migrationsFS embed.FS
|
|
|
|
var (
|
|
ErrInvalidRequest = errors.New("invalid_request")
|
|
ErrNotFound = errors.New("not_found")
|
|
ErrConflict = errors.New("conflict")
|
|
ErrUnauthorized = errors.New("unauthorized")
|
|
)
|
|
|
|
const migrationAdvisoryLockID int64 = 7272727272727272
|
|
|
|
type Store interface {
|
|
CreateUser(ctx context.Context, username, displayName, passwordHash string) (*model.User, error)
|
|
GetUserByID(ctx context.Context, id string) (*model.User, error)
|
|
GetUserByUsername(ctx context.Context, username string) (*model.User, error)
|
|
|
|
CreateChat(ctx context.Context, title string, memberIDs []string) (*model.Chat, error)
|
|
GetChat(ctx context.Context, chatID, userID string) (*model.Chat, error)
|
|
ListChats(ctx context.Context, userID string, limit int, cursor string) ([]*model.Chat, *string, error)
|
|
|
|
AddMessage(ctx context.Context, chatID, senderID, text string) (*model.Message, error)
|
|
SendMessageIdempotent(ctx context.Context, chatID, senderID, text string, attachmentIDs []string, idemKey, path, requestHash string) (*model.Message, error)
|
|
GetMessages(ctx context.Context, chatID, userID, cursor string, limit int) ([]*model.Message, *string, error)
|
|
|
|
CreateSession(ctx context.Context, userID, tokenHash string, expiresAt time.Time) (*model.Session, error)
|
|
GetSession(ctx context.Context, tokenHash string) (*model.Session, error)
|
|
DeleteSession(ctx context.Context, tokenHash string) error
|
|
|
|
CreateAttachmentAndJob(ctx context.Context, chatID, ownerID, filename, objectKey, contentType string, sizeBytes int64, sha256Hex string) (*model.Attachment, *model.Job, error)
|
|
GetAttachmentForMetadata(ctx context.Context, attachmentID, userID string) (*model.Attachment, error)
|
|
GetAttachmentForDownload(ctx context.Context, attachmentID, userID string) (*model.Attachment, error)
|
|
GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error)
|
|
MarkAttachmentReadyWithVariant(ctx context.Context, attachmentID, thumbnailKey string, thumbSize int64, thumbSHA string, thumbWidth, thumbHeight int) error
|
|
MarkAttachmentFailed(ctx context.Context, attachmentID string, errorCode string) error
|
|
|
|
PublishJobToBroker(ctx context.Context, jobID string) error
|
|
GetPendingJobs(ctx context.Context, limit int) ([]model.Job, error)
|
|
UpdateJobStatus(ctx context.Context, jobID string, status string, attempts int, lastError string) error
|
|
|
|
GetReadyAttachmentBySHA256(ctx context.Context, sha256Hex string) (*model.Attachment, error)
|
|
GetAttachmentBySHA256InChat(ctx context.Context, chatID, sha256Hex string) (*model.Attachment, error)
|
|
|
|
ClaimStaleQueued(ctx context.Context, olderThan time.Time) ([]model.Attachment, error)
|
|
MarkAttachmentDeleting(ctx context.Context, id string) error
|
|
DeleteFailedStaleAttachment(ctx context.Context, id string) error
|
|
GetAttachmentVariant(ctx context.Context, attachmentID, userID, variantName string) (*model.Variant, error)
|
|
|
|
GetJobByID(ctx context.Context, jobID string) (*model.Job, error)
|
|
ClaimJob(ctx context.Context, jobID, workerID string, lease time.Duration) (*model.Job, error)
|
|
RequeueExpiredLeases(ctx context.Context) (int64, error)
|
|
RequeueAttachment(ctx context.Context, attachmentID string) error
|
|
CompleteAttachment(ctx context.Context, attachmentID, jobID, finalKey string, variants []model.Variant) error
|
|
FailJob(ctx context.Context, jobID, attachmentID, errorCode, detail string) error
|
|
RetryJob(ctx context.Context, jobID, attachmentID, errorCode, detail string, delay time.Duration) error
|
|
ListCompletedStaging(ctx context.Context, olderThan time.Time) ([]model.StagingRef, error)
|
|
ClearStagingKey(ctx context.Context, attachmentID string) error
|
|
}
|
|
|
|
type PostgresStore struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewPostgres(connString string) (*PostgresStore, error) {
|
|
config, err := pgxpool.ParseConfig(connString)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse config: %w", err)
|
|
}
|
|
config.MaxConns = 25
|
|
config.MinConns = 5
|
|
config.MaxConnLifetime = 30 * time.Minute
|
|
config.MaxConnIdleTime = 5 * time.Minute
|
|
config.HealthCheckPeriod = 30 * time.Second
|
|
|
|
pool, err := pgxpool.NewWithConfig(context.Background(), config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create pool: %w", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("ping database: %w", err)
|
|
}
|
|
|
|
store := &PostgresStore{pool: pool}
|
|
if err := store.applyMigrations(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("apply migrations: %w", err)
|
|
}
|
|
|
|
return store, nil
|
|
}
|
|
|
|
func (s *PostgresStore) Close() {
|
|
s.pool.Close()
|
|
}
|
|
|
|
func (s *PostgresStore) applyMigrations(ctx context.Context) error {
|
|
conn, err := s.pool.Acquire(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("acquire conn for lock: %w", err)
|
|
}
|
|
defer conn.Release()
|
|
|
|
if _, err := conn.Exec(ctx, `SELECT pg_advisory_lock($1)`, migrationAdvisoryLockID); err != nil {
|
|
return fmt.Errorf("advisory lock: %w", err)
|
|
}
|
|
defer func() {
|
|
_, _ = conn.Exec(context.Background(), `SELECT pg_advisory_unlock($1)`, migrationAdvisoryLockID)
|
|
}()
|
|
|
|
_, err = conn.Exec(ctx, `CREATE TABLE IF NOT EXISTS schema_migrations (version INTEGER PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW())`)
|
|
if err != nil {
|
|
return fmt.Errorf("create migrations table: %w", err)
|
|
}
|
|
|
|
var lastVersion int
|
|
err = conn.QueryRow(ctx, `SELECT COALESCE(MAX(version), 0) FROM schema_migrations`).Scan(&lastVersion)
|
|
if err != nil {
|
|
return fmt.Errorf("get last version: %w", err)
|
|
}
|
|
|
|
entries, err := migrationsFS.ReadDir("migrations")
|
|
if err != nil {
|
|
return fmt.Errorf("read migrations: %w", err)
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
continue
|
|
}
|
|
var version int
|
|
if _, err := fmt.Sscanf(entry.Name(), "%d_", &version); err != nil {
|
|
continue
|
|
}
|
|
if version <= lastVersion {
|
|
log.Printf("INFO: Skipping migration %s (already applied)", entry.Name())
|
|
continue
|
|
}
|
|
content, err := migrationsFS.ReadFile("migrations/" + entry.Name())
|
|
if err != nil {
|
|
return fmt.Errorf("read migration %s: %w", entry.Name(), err)
|
|
}
|
|
if _, err := conn.Exec(ctx, string(content)); err != nil {
|
|
return fmt.Errorf("execute migration %s: %w", entry.Name(), err)
|
|
}
|
|
if _, err := conn.Exec(ctx, `INSERT INTO schema_migrations (version) VALUES ($1)`, version); err != nil {
|
|
return fmt.Errorf("insert migration version: %w", err)
|
|
}
|
|
log.Printf("INFO: Applied migration %s", entry.Name())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PostgresStore) CreateUser(ctx context.Context, username, displayName, passwordHash string) (*model.User, error) {
|
|
var user model.User
|
|
err := s.pool.QueryRow(ctx, `INSERT INTO users (username, display_name, password_hash) VALUES ($1, $2, $3) RETURNING id, username, display_name, created_at`, username, displayName, passwordHash).
|
|
Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
|
|
if err != nil {
|
|
if isUniqueViolation(err) {
|
|
return nil, ErrConflict
|
|
}
|
|
return nil, fmt.Errorf("insert user: %w", err)
|
|
}
|
|
user.CreatedAt = user.CreatedAt.UTC()
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetUserByID(ctx context.Context, id string) (*model.User, error) {
|
|
var user model.User
|
|
err := s.pool.QueryRow(ctx, `SELECT id, username, display_name, password_hash, created_at FROM users WHERE id = $1`, id).
|
|
Scan(&user.ID, &user.Username, &user.DisplayName, &user.PasswordHash, &user.CreatedAt)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("get user by id: %w", err)
|
|
}
|
|
user.CreatedAt = user.CreatedAt.UTC()
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetUserByUsername(ctx context.Context, username string) (*model.User, error) {
|
|
var user model.User
|
|
err := s.pool.QueryRow(ctx, `SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = $1`, username).
|
|
Scan(&user.ID, &user.Username, &user.DisplayName, &user.PasswordHash, &user.CreatedAt)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("get user by username: %w", err)
|
|
}
|
|
user.CreatedAt = user.CreatedAt.UTC()
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *PostgresStore) CreateChat(ctx context.Context, title string, memberIDs []string) (*model.Chat, error) {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("begin tx: %w", err)
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
var chat model.Chat
|
|
if err := tx.QueryRow(ctx, `INSERT INTO chats (title) VALUES ($1) RETURNING id, created_at`, title).Scan(&chat.ID, &chat.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("insert chat: %w", err)
|
|
}
|
|
for _, userID := range memberIDs {
|
|
if _, err := tx.Exec(ctx, `INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)`, chat.ID, userID); err != nil {
|
|
return nil, fmt.Errorf("insert member %s: %w", userID, err)
|
|
}
|
|
}
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return nil, fmt.Errorf("commit tx: %w", err)
|
|
}
|
|
|
|
chat.Title = title
|
|
chat.MemberIDs = memberIDs
|
|
if chat.MemberIDs == nil {
|
|
chat.MemberIDs = []string{}
|
|
}
|
|
chat.CreatedAt = chat.CreatedAt.UTC()
|
|
return &chat, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetChat(ctx context.Context, chatID, userID string) (*model.Chat, error) {
|
|
var isMember bool
|
|
if err := s.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`, chatID, userID).Scan(&isMember); err != nil {
|
|
return nil, fmt.Errorf("check membership: %w", err)
|
|
}
|
|
if !isMember {
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
var chat model.Chat
|
|
if err := s.pool.QueryRow(ctx, `SELECT id, title, created_at FROM chats WHERE id = $1`, chatID).Scan(&chat.ID, &chat.Title, &chat.CreatedAt); err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("get chat: %w", err)
|
|
}
|
|
chat.CreatedAt = chat.CreatedAt.UTC()
|
|
chat.MemberIDs = []string{}
|
|
|
|
rows, err := s.pool.Query(ctx, `SELECT user_id FROM chat_members WHERE chat_id = $1`, chatID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get members: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var m string
|
|
if err := rows.Scan(&m); err != nil {
|
|
return nil, fmt.Errorf("scan member: %w", err)
|
|
}
|
|
chat.MemberIDs = append(chat.MemberIDs, m)
|
|
}
|
|
return &chat, nil
|
|
}
|
|
|
|
func (s *PostgresStore) ListChats(ctx context.Context, userID string, limit int, cursor string) ([]*model.Chat, *string, error) {
|
|
fetchLimit := limit + 1
|
|
var query string
|
|
var args []interface{}
|
|
|
|
if cursor == "" {
|
|
query = `SELECT c.id, c.title, c.created_at FROM chats c INNER JOIN chat_members cm ON cm.chat_id = c.id WHERE cm.user_id = $1 ORDER BY c.created_at, c.id LIMIT $2`
|
|
args = []interface{}{userID, fetchLimit}
|
|
} else {
|
|
query = `
|
|
SELECT c.id, c.title, c.created_at
|
|
FROM chats c
|
|
INNER JOIN chat_members cm ON cm.chat_id = c.id
|
|
WHERE cm.user_id = $1
|
|
AND (c.created_at, c.id) > (
|
|
SELECT c2.created_at, c2.id FROM chats c2 WHERE c2.id = $2
|
|
)
|
|
ORDER BY c.created_at, c.id
|
|
LIMIT $3
|
|
`
|
|
args = []interface{}{userID, cursor, fetchLimit}
|
|
}
|
|
|
|
rows, err := s.pool.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("query chats: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var chats []*model.Chat
|
|
for rows.Next() {
|
|
var chat model.Chat
|
|
if err := rows.Scan(&chat.ID, &chat.Title, &chat.CreatedAt); err != nil {
|
|
return nil, nil, fmt.Errorf("scan chat: %w", err)
|
|
}
|
|
chat.CreatedAt = chat.CreatedAt.UTC()
|
|
chat.MemberIDs = []string{}
|
|
chats = append(chats, &chat)
|
|
}
|
|
|
|
if len(chats) > 0 {
|
|
ids := make([]string, len(chats))
|
|
for i, c := range chats {
|
|
ids[i] = c.ID
|
|
}
|
|
mrows, err := s.pool.Query(ctx, `SELECT chat_id, user_id FROM chat_members WHERE chat_id = ANY($1::uuid[])`, ids)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("query members: %w", err)
|
|
}
|
|
defer mrows.Close()
|
|
mm := make(map[string][]string)
|
|
for mrows.Next() {
|
|
var cid, mid string
|
|
if err := mrows.Scan(&cid, &mid); err != nil {
|
|
return nil, nil, fmt.Errorf("scan member: %w", err)
|
|
}
|
|
mm[cid] = append(mm[cid], mid)
|
|
}
|
|
for _, c := range chats {
|
|
if members, ok := mm[c.ID]; ok {
|
|
c.MemberIDs = members
|
|
}
|
|
}
|
|
}
|
|
|
|
var nextCursor *string
|
|
if len(chats) > limit {
|
|
chats = chats[:limit]
|
|
lastID := chats[len(chats)-1].ID
|
|
nextCursor = &lastID
|
|
}
|
|
if chats == nil {
|
|
chats = []*model.Chat{}
|
|
}
|
|
return chats, nextCursor, nil
|
|
}
|
|
|
|
func (s *PostgresStore) AddMessage(ctx context.Context, chatID, senderID, text string) (*model.Message, error) {
|
|
return s.insertMessage(ctx, chatID, senderID, text)
|
|
}
|
|
|
|
func (s *PostgresStore) SendMessageIdempotent(ctx context.Context, chatID, senderID, text string, attachmentIDs []string, idemKey, path, requestHash string) (*model.Message, error) {
|
|
if attachmentIDs == nil {
|
|
attachmentIDs = []string{}
|
|
}
|
|
if len(attachmentIDs) > 10 {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
if utf8.RuneCountInString(text) > 4000 {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
if strings.TrimSpace(text) == "" && len(attachmentIDs) == 0 {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
|
|
seen := make(map[string]struct{}, len(attachmentIDs))
|
|
for _, id := range attachmentIDs {
|
|
if _, exists := seen[id]; exists {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
seen[id] = struct{}{}
|
|
}
|
|
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("begin tx: %w", err)
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
scope := fmt.Sprintf("%s|%s|%s|%s", senderID, "POST", path, idemKey)
|
|
lockID := hashScope(scope)
|
|
|
|
if _, err := tx.Exec(ctx, `SELECT pg_advisory_xact_lock($1)`, lockID); err != nil {
|
|
return nil, fmt.Errorf("advisory xact lock: %w", err)
|
|
}
|
|
|
|
var existingHash string
|
|
var existingBody []byte
|
|
err = tx.QueryRow(ctx, `SELECT request_hash, response_body FROM idempotency_keys WHERE user_id = $1 AND method = $2 AND path = $3 AND key = $4`, senderID, "POST", path, idemKey).Scan(&existingHash, &existingBody)
|
|
|
|
switch {
|
|
case err == nil:
|
|
if existingHash != requestHash {
|
|
return nil, ErrConflict
|
|
}
|
|
var msg model.Message
|
|
if err := json.Unmarshal(existingBody, &msg); err != nil {
|
|
return nil, fmt.Errorf("decode replay body: %w", err)
|
|
}
|
|
if msg.AttachmentIDs == nil {
|
|
msg.AttachmentIDs = []string{}
|
|
}
|
|
msg.CreatedAt = msg.CreatedAt.UTC()
|
|
return &msg, nil
|
|
|
|
case errors.Is(err, pgx.ErrNoRows):
|
|
// Lab 5: обработка вложения асинхронная (queued → processing → ready).
|
|
// Сообщение принимается сразу после 202 Accepted, не дожидаясь worker.
|
|
// Отклоняем только терминальные плохие состояния: failed, deleting.
|
|
for _, attID := range attachmentIDs {
|
|
var attChatID string
|
|
var status string
|
|
qerr := tx.QueryRow(ctx, `SELECT chat_id::text, status FROM attachments WHERE id = $1`, attID).Scan(&attChatID, &status)
|
|
if qerr != nil {
|
|
if errors.Is(qerr, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if isUUIDSyntaxError(qerr) {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
return nil, fmt.Errorf("select attachment: %w", qerr)
|
|
}
|
|
if attChatID != chatID {
|
|
return nil, ErrNotFound
|
|
}
|
|
switch status {
|
|
case "queued", "processing", "ready":
|
|
// допустимо: вложение принадлежит чату и не в плохом состоянии
|
|
default:
|
|
return nil, ErrConflict
|
|
}
|
|
}
|
|
|
|
msg, err := s.insertMessageTx(ctx, tx, chatID, senderID, text)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
msg.AttachmentIDs = attachmentIDs
|
|
|
|
for i, attID := range attachmentIDs {
|
|
if _, err := tx.Exec(ctx, `INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1, $2, $3)`, msg.ID, attID, i); err != nil {
|
|
if isUniqueViolation(err) {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
return nil, fmt.Errorf("insert message attachment: %w", err)
|
|
}
|
|
}
|
|
|
|
body, err := json.Marshal(msg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("marshal message: %w", err)
|
|
}
|
|
if _, err := tx.Exec(ctx, `INSERT INTO idempotency_keys (user_id, method, path, key, request_hash, response_code, response_body) VALUES ($1, $2, $3, $4, $5, $6, $7)`, senderID, "POST", path, idemKey, requestHash, 201, body); err != nil {
|
|
return nil, fmt.Errorf("insert idempotency key: %w", err)
|
|
}
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return nil, fmt.Errorf("commit tx: %w", err)
|
|
}
|
|
return msg, nil
|
|
|
|
default:
|
|
return nil, fmt.Errorf("select idempotency key: %w", err)
|
|
}
|
|
}
|
|
|
|
func (s *PostgresStore) insertMessage(ctx context.Context, chatID, senderID, text string) (*model.Message, error) {
|
|
var isMember bool
|
|
if err := s.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`, chatID, senderID).Scan(&isMember); err != nil {
|
|
return nil, fmt.Errorf("check membership: %w", err)
|
|
}
|
|
if !isMember {
|
|
return nil, ErrNotFound
|
|
}
|
|
var msg model.Message
|
|
if err := s.pool.QueryRow(ctx, `INSERT INTO messages (chat_id, sender_id, text) VALUES ($1, $2, $3) RETURNING id, chat_id, sender_id, text, created_at`, chatID, senderID, text).Scan(&msg.ID, &msg.ChatID, &msg.SenderID, &msg.Text, &msg.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("insert message: %w", err)
|
|
}
|
|
msg.CreatedAt = msg.CreatedAt.UTC()
|
|
msg.AttachmentIDs = []string{}
|
|
return &msg, nil
|
|
}
|
|
|
|
func (s *PostgresStore) insertMessageTx(ctx context.Context, tx pgx.Tx, chatID, senderID, text string) (*model.Message, error) {
|
|
var isMember bool
|
|
if err := tx.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`, chatID, senderID).Scan(&isMember); err != nil {
|
|
return nil, fmt.Errorf("check membership: %w", err)
|
|
}
|
|
if !isMember {
|
|
return nil, ErrNotFound
|
|
}
|
|
var msg model.Message
|
|
if err := tx.QueryRow(ctx, `INSERT INTO messages (chat_id, sender_id, text) VALUES ($1, $2, $3) RETURNING id, chat_id, sender_id, text, created_at`, chatID, senderID, text).Scan(&msg.ID, &msg.ChatID, &msg.SenderID, &msg.Text, &msg.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("insert message: %w", err)
|
|
}
|
|
msg.CreatedAt = msg.CreatedAt.UTC()
|
|
msg.AttachmentIDs = []string{}
|
|
return &msg, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetMessages(ctx context.Context, chatID, userID, cursor string, limit int) ([]*model.Message, *string, error) {
|
|
var isMember bool
|
|
if err := s.pool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`, chatID, userID).Scan(&isMember); err != nil {
|
|
return nil, nil, fmt.Errorf("check membership: %w", err)
|
|
}
|
|
if !isMember {
|
|
return nil, nil, ErrNotFound
|
|
}
|
|
|
|
fetchLimit := limit + 1
|
|
var query string
|
|
var args []interface{}
|
|
|
|
if cursor == "" {
|
|
query = `SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = $1 ORDER BY created_at, id LIMIT $2`
|
|
args = []interface{}{chatID, fetchLimit}
|
|
} else {
|
|
query = `
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = $1
|
|
AND (created_at, id) > (
|
|
SELECT created_at, id FROM messages WHERE id = $2 AND chat_id = $1
|
|
)
|
|
ORDER BY created_at, id
|
|
LIMIT $3
|
|
`
|
|
args = []interface{}{chatID, cursor, fetchLimit}
|
|
}
|
|
|
|
rows, err := s.pool.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("query messages: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var messages []*model.Message
|
|
for rows.Next() {
|
|
var msg model.Message
|
|
if err := rows.Scan(&msg.ID, &msg.ChatID, &msg.SenderID, &msg.Text, &msg.CreatedAt); err != nil {
|
|
return nil, nil, fmt.Errorf("scan message: %w", err)
|
|
}
|
|
msg.CreatedAt = msg.CreatedAt.UTC()
|
|
msg.AttachmentIDs = []string{}
|
|
messages = append(messages, &msg)
|
|
}
|
|
|
|
var nextCursor *string
|
|
if len(messages) > limit {
|
|
messages = messages[:limit]
|
|
lastID := messages[len(messages)-1].ID
|
|
nextCursor = &lastID
|
|
}
|
|
if messages == nil {
|
|
messages = []*model.Message{}
|
|
}
|
|
|
|
if len(messages) > 0 {
|
|
ids := make([]string, len(messages))
|
|
for i, m := range messages {
|
|
ids[i] = m.ID
|
|
}
|
|
arows, err := s.pool.Query(ctx, `SELECT message_id::text, attachment_id::text FROM message_attachments WHERE message_id = ANY($1::uuid[]) ORDER BY message_id, position`, ids)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("query message attachments: %w", err)
|
|
}
|
|
defer arows.Close()
|
|
|
|
byMessage := make(map[string][]string)
|
|
for arows.Next() {
|
|
var msgID, attID string
|
|
if err := arows.Scan(&msgID, &attID); err != nil {
|
|
return nil, nil, fmt.Errorf("scan message attachment: %w", err)
|
|
}
|
|
byMessage[msgID] = append(byMessage[msgID], attID)
|
|
}
|
|
if err := arows.Err(); err != nil {
|
|
return nil, nil, fmt.Errorf("message attachments rows: %w", err)
|
|
}
|
|
for _, m := range messages {
|
|
if atts, ok := byMessage[m.ID]; ok && len(atts) > 0 {
|
|
m.AttachmentIDs = atts
|
|
} else {
|
|
m.AttachmentIDs = []string{}
|
|
}
|
|
}
|
|
}
|
|
return messages, nextCursor, nil
|
|
}
|
|
|
|
func (s *PostgresStore) CreateSession(ctx context.Context, userID, tokenHash string, expiresAt time.Time) (*model.Session, error) {
|
|
var session model.Session
|
|
if err := s.pool.QueryRow(ctx, `INSERT INTO sessions (user_id, token_hash, expires_at) VALUES ($1, $2, $3) RETURNING id, user_id, token_hash, expires_at, created_at`, userID, tokenHash, expiresAt).Scan(&session.ID, &session.UserID, &session.TokenHash, &session.ExpiresAt, &session.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("insert session: %w", err)
|
|
}
|
|
session.ExpiresAt = session.ExpiresAt.UTC()
|
|
session.CreatedAt = session.CreatedAt.UTC()
|
|
return &session, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetSession(ctx context.Context, tokenHash string) (*model.Session, error) {
|
|
var session model.Session
|
|
if err := s.pool.QueryRow(ctx, `SELECT id, user_id, token_hash, expires_at, created_at FROM sessions WHERE token_hash = $1 AND expires_at > NOW()`, tokenHash).Scan(&session.ID, &session.UserID, &session.TokenHash, &session.ExpiresAt, &session.CreatedAt); err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("get session: %w", err)
|
|
}
|
|
session.ExpiresAt = session.ExpiresAt.UTC()
|
|
session.CreatedAt = session.CreatedAt.UTC()
|
|
return &session, nil
|
|
}
|
|
|
|
func (s *PostgresStore) DeleteSession(ctx context.Context, tokenHash string) error {
|
|
if _, err := s.pool.Exec(ctx, `DELETE FROM sessions WHERE token_hash = $1`, tokenHash); err != nil {
|
|
return fmt.Errorf("delete session: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// max_attempts берётся из конфигурации, а не зашит в SQL.
|
|
func (s *PostgresStore) CreateAttachmentAndJob(ctx context.Context, chatID, ownerID, filename, objectKey, contentType string, sizeBytes int64, sha256Hex string) (*model.Attachment, *model.Job, error) {
|
|
maxAttempts := envInt("JOB_MAX_ATTEMPTS", 5)
|
|
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("begin tx: %w", err)
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
var att model.Attachment
|
|
err = tx.QueryRow(ctx, `
|
|
INSERT INTO attachments (chat_id, owner_id, filename, object_key, staging_key, content_type, size_bytes, sha256, status)
|
|
VALUES ($1,$2,$3,$4,$4,$5,$6,$7,'queued')
|
|
RETURNING id::text, chat_id::text, owner_id::text, filename, object_key, content_type, size_bytes, sha256, status, error_code, created_at, updated_at
|
|
`, chatID, ownerID, filename, objectKey, contentType, sizeBytes, sha256Hex).Scan(
|
|
&att.ID, &att.ChatID, &att.OwnerID, &att.Filename, &att.ObjectKey, &att.ContentType,
|
|
&att.SizeBytes, &att.SHA256, &att.Status, &att.ErrorCode, &att.CreatedAt, &att.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("insert attachment: %w", err)
|
|
}
|
|
|
|
var job model.Job
|
|
err = tx.QueryRow(ctx, `
|
|
INSERT INTO attachment_jobs (attachment_id, status, attempts, max_attempts, next_run_at, pipeline_version)
|
|
VALUES ($1,'pending',0,$2,NOW(),1)
|
|
RETURNING id::text, attachment_id::text, status, attempts, max_attempts, next_run_at, last_error, created_at, lease_until, leased_by, pipeline_version
|
|
`, att.ID, maxAttempts).Scan(
|
|
&job.ID, &job.AttachmentID, &job.Status, &job.Attempts, &job.MaxAttempts,
|
|
&job.NextRunAt, &job.LastError, &job.CreatedAt, &job.LeaseUntil, &job.LeasedBy, &job.PipelineVersion,
|
|
)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("insert job: %w", err)
|
|
}
|
|
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return nil, nil, fmt.Errorf("commit tx: %w", err)
|
|
}
|
|
|
|
att.CreatedAt, att.UpdatedAt = att.CreatedAt.UTC(), att.UpdatedAt.UTC()
|
|
att.Variants = []any{}
|
|
job.CreatedAt, job.NextRunAt = job.CreatedAt.UTC(), job.NextRunAt.UTC()
|
|
return &att, &job, nil
|
|
}
|
|
|
|
// 'deleting' — внутреннее состояние, наружу отдаём контрактный enum.
|
|
func (s *PostgresStore) GetAttachmentForMetadata(ctx context.Context, attachmentID, userID string) (*model.Attachment, error) {
|
|
var a model.Attachment
|
|
err := s.pool.QueryRow(ctx, `
|
|
SELECT a.id::text, a.chat_id::text, a.owner_id::text, a.filename, a.object_key,
|
|
a.content_type, a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
|
FROM attachments a JOIN chat_members cm ON cm.chat_id = a.chat_id
|
|
WHERE a.id = $1 AND cm.user_id = $2
|
|
`, attachmentID, userID).Scan(
|
|
&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey,
|
|
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if isUUIDSyntaxError(err) {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
return nil, fmt.Errorf("get attachment for metadata: %w", err)
|
|
}
|
|
|
|
if a.Status == "deleting" {
|
|
a.Status = "failed"
|
|
code := "stale_cleanup"
|
|
a.ErrorCode = &code
|
|
}
|
|
|
|
variants, err := s.loadVariants(ctx, a.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
a.CreatedAt, a.UpdatedAt = a.CreatedAt.UTC(), a.UpdatedAt.UTC()
|
|
a.Variants = variants
|
|
return &a, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetAttachmentVariant(ctx context.Context, attachmentID, userID, variantName string) (*model.Variant, error) {
|
|
kind := variantNameToKind(variantName)
|
|
if kind == "" {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
|
|
var v model.Variant
|
|
var gotKind string
|
|
err := s.pool.QueryRow(ctx, `
|
|
SELECT v.kind, v.object_key, v.content_type, v.size_bytes, v.width, v.height, v.sha256
|
|
FROM attachment_variants v
|
|
JOIN attachments a ON a.id = v.attachment_id
|
|
JOIN chat_members cm ON cm.chat_id = a.chat_id
|
|
WHERE v.attachment_id = $1
|
|
AND cm.user_id = $2
|
|
AND a.status = 'ready'
|
|
AND v.kind = $3
|
|
`, attachmentID, userID, kind).Scan(
|
|
&gotKind, &v.ObjectKey, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if isUUIDSyntaxError(err) {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
return nil, fmt.Errorf("get attachment variant: %w", err)
|
|
}
|
|
|
|
v.Name = variantKindToName(gotKind)
|
|
return &v, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetAttachmentForDownload(ctx context.Context, attachmentID, userID string) (*model.Attachment, error) {
|
|
var a model.Attachment
|
|
err := s.pool.QueryRow(ctx, `
|
|
SELECT a.id::text, a.chat_id::text, a.owner_id::text, a.filename, a.object_key,
|
|
a.content_type, a.size_bytes, a.sha256, a.status, a.error_code,
|
|
a.created_at, a.updated_at
|
|
FROM attachments a
|
|
JOIN chat_members cm ON cm.chat_id = a.chat_id
|
|
WHERE a.id = $1 AND cm.user_id = $2 AND a.status = 'ready'
|
|
`, attachmentID, userID).Scan(
|
|
&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey,
|
|
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status,
|
|
&a.ErrorCode, &a.CreatedAt, &a.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if isUUIDSyntaxError(err) {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
return nil, fmt.Errorf("get attachment for download: %w", err)
|
|
}
|
|
a.CreatedAt = a.CreatedAt.UTC()
|
|
a.UpdatedAt = a.UpdatedAt.UTC()
|
|
a.Variants = []any{}
|
|
return &a, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetAttachmentByID(ctx context.Context, id string) (*model.Attachment, error) {
|
|
var a model.Attachment
|
|
err := s.pool.QueryRow(ctx, `
|
|
SELECT id::text, chat_id::text, owner_id::text, filename, object_key,
|
|
content_type, size_bytes, sha256, status, error_code,
|
|
created_at, updated_at
|
|
FROM attachments
|
|
WHERE id = $1
|
|
`, id).Scan(
|
|
&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey,
|
|
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status,
|
|
&a.ErrorCode, &a.CreatedAt, &a.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("get attachment by id: %w", err)
|
|
}
|
|
a.CreatedAt = a.CreatedAt.UTC()
|
|
a.UpdatedAt = a.UpdatedAt.UTC()
|
|
a.Variants = []any{}
|
|
return &a, nil
|
|
}
|
|
|
|
func (s *PostgresStore) MarkAttachmentReadyWithVariant(ctx context.Context, attachmentID, thumbnailKey string, thumbSize int64, thumbSHA string, thumbWidth, thumbHeight int) error {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("begin tx: %w", err)
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
_, err = tx.Exec(ctx, `
|
|
INSERT INTO attachment_variants
|
|
(attachment_id, kind, object_key, content_type, size_bytes, width, height, sha256)
|
|
VALUES ($1, 'thumbnail_256', $2, 'image/jpeg', $3, $4, $5, $6)
|
|
ON CONFLICT (attachment_id, kind) DO UPDATE SET
|
|
object_key = EXCLUDED.object_key,
|
|
content_type = EXCLUDED.content_type,
|
|
size_bytes = EXCLUDED.size_bytes,
|
|
width = EXCLUDED.width,
|
|
height = EXCLUDED.height,
|
|
sha256 = EXCLUDED.sha256
|
|
`, attachmentID, thumbnailKey, thumbSize, thumbWidth, thumbHeight, thumbSHA)
|
|
if err != nil {
|
|
return fmt.Errorf("upsert thumbnail variant: %w", err)
|
|
}
|
|
|
|
// Статус ready выставляем только после записи всех обещанных вариантов.
|
|
_, err = tx.Exec(ctx, `
|
|
UPDATE attachments
|
|
SET status = 'ready', error_code = NULL, updated_at = NOW()
|
|
WHERE id = $1 AND status IN ('queued', 'processing')
|
|
`, attachmentID)
|
|
if err != nil {
|
|
return fmt.Errorf("update attachment status: %w", err)
|
|
}
|
|
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
func (s *PostgresStore) MarkAttachmentFailed(ctx context.Context, attachmentID string, errorCode string) error {
|
|
_, err := s.pool.Exec(ctx, `UPDATE attachments SET status = 'failed', error_code = $1, updated_at = NOW() WHERE id = $2 AND status IN ('queued', 'processing')`, errorCode, attachmentID)
|
|
if err != nil {
|
|
return fmt.Errorf("mark attachment failed: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PostgresStore) PublishJobToBroker(ctx context.Context, jobID string) error {
|
|
_, err := s.pool.Exec(ctx, `UPDATE attachment_jobs SET status = 'published' WHERE id = $1 AND status = 'pending'`, jobID)
|
|
return err
|
|
}
|
|
|
|
func (s *PostgresStore) GetPendingJobs(ctx context.Context, limit int) ([]model.Job, error) {
|
|
rows, err := s.pool.Query(ctx, `
|
|
SELECT id::text, attachment_id::text, status, attempts, max_attempts, next_run_at, last_error, created_at
|
|
FROM attachment_jobs
|
|
WHERE status IN ('pending', 'published') AND next_run_at <= NOW()
|
|
ORDER BY next_run_at ASC
|
|
LIMIT $1
|
|
`, limit)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query pending jobs: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var jobs []model.Job
|
|
for rows.Next() {
|
|
var j model.Job
|
|
if err := rows.Scan(&j.ID, &j.AttachmentID, &j.Status, &j.Attempts, &j.MaxAttempts, &j.NextRunAt, &j.LastError, &j.CreatedAt); err != nil {
|
|
return nil, fmt.Errorf("scan job: %w", err)
|
|
}
|
|
j.CreatedAt = j.CreatedAt.UTC()
|
|
j.NextRunAt = j.NextRunAt.UTC()
|
|
jobs = append(jobs, j)
|
|
}
|
|
return jobs, rows.Err()
|
|
}
|
|
|
|
func (s *PostgresStore) UpdateJobStatus(ctx context.Context, jobID string, status string, attempts int, lastError string) error {
|
|
var nextRun time.Time
|
|
if status == "retry" {
|
|
// Экспоненциальный backoff: 10s, 20s, 40s, 60s (cap)
|
|
delay := time.Duration(10*(1<<attempts)) * time.Second
|
|
if delay > 60*time.Second {
|
|
delay = 60 * time.Second
|
|
}
|
|
nextRun = time.Now().Add(delay)
|
|
status = "pending"
|
|
} else {
|
|
nextRun = time.Now()
|
|
}
|
|
_, err := s.pool.Exec(ctx, `
|
|
UPDATE attachment_jobs
|
|
SET status = $1, attempts = $2, last_error = $3, next_run_at = $4, updated_at = NOW()
|
|
WHERE id = $5
|
|
`, status, attempts, lastError, nextRun, jobID)
|
|
return err
|
|
}
|
|
|
|
func (s *PostgresStore) GetReadyAttachmentBySHA256(ctx context.Context, sha256Hex string) (*model.Attachment, error) {
|
|
var a model.Attachment
|
|
err := s.pool.QueryRow(ctx, `
|
|
SELECT id::text, chat_id::text, owner_id::text, filename, object_key,
|
|
content_type, size_bytes, sha256, status, error_code,
|
|
created_at, updated_at
|
|
FROM attachments
|
|
WHERE sha256 = $1 AND status = 'ready'
|
|
ORDER BY created_at
|
|
LIMIT 1
|
|
`, sha256Hex).Scan(
|
|
&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey,
|
|
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status,
|
|
&a.ErrorCode, &a.CreatedAt, &a.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("get ready attachment by sha256: %w", err)
|
|
}
|
|
a.CreatedAt = a.CreatedAt.UTC()
|
|
a.UpdatedAt = a.UpdatedAt.UTC()
|
|
a.Variants = []any{}
|
|
return &a, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetAttachmentBySHA256InChat(ctx context.Context, chatID, sha256Hex string) (*model.Attachment, error) {
|
|
var a model.Attachment
|
|
err := s.pool.QueryRow(ctx, `
|
|
SELECT id::text, chat_id::text, owner_id::text, filename, object_key,
|
|
content_type, size_bytes, sha256, status, error_code,
|
|
created_at, updated_at
|
|
FROM attachments
|
|
WHERE chat_id = $1 AND sha256 = $2 AND status = 'ready'
|
|
ORDER BY created_at
|
|
LIMIT 1
|
|
`, chatID, sha256Hex).Scan(
|
|
&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey,
|
|
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.Status,
|
|
&a.ErrorCode, &a.CreatedAt, &a.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, fmt.Errorf("get attachment by sha256 in chat: %w", err)
|
|
}
|
|
a.CreatedAt = a.CreatedAt.UTC()
|
|
a.UpdatedAt = a.UpdatedAt.UTC()
|
|
a.Variants = []any{}
|
|
return &a, nil
|
|
}
|
|
|
|
func (s *PostgresStore) ClaimStaleQueued(ctx context.Context, olderThan time.Time) ([]model.Attachment, error) {
|
|
rows, err := s.pool.Query(ctx, `
|
|
UPDATE attachments SET status='failed', error_code='stale_cleanup', updated_at=NOW()
|
|
WHERE status='queued' AND created_at < $1
|
|
RETURNING id::text, object_key
|
|
`, olderThan)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("claim stale queued: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
out := []model.Attachment{}
|
|
for rows.Next() {
|
|
var a model.Attachment
|
|
if err := rows.Scan(&a.ID, &a.ObjectKey); err != nil {
|
|
return nil, fmt.Errorf("scan claimed attachment: %w", err)
|
|
}
|
|
out = append(out, a)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *PostgresStore) MarkAttachmentDeleting(ctx context.Context, id string) error {
|
|
_, err := s.pool.Exec(ctx, `UPDATE attachments SET status = 'deleting', updated_at = NOW() WHERE id = $1 AND status IN ('queued', 'failed')`, id)
|
|
return err
|
|
}
|
|
|
|
func (s *PostgresStore) DeleteFailedStaleAttachment(ctx context.Context, id string) error {
|
|
_, err := s.pool.Exec(ctx,
|
|
`DELETE FROM attachments WHERE id=$1 AND status='failed' AND error_code='stale_cleanup'`, id)
|
|
return err
|
|
}
|
|
|
|
func hashScope(s string) int64 {
|
|
h := sha256.Sum256([]byte(s))
|
|
return int64(binary.BigEndian.Uint64(h[:8]))
|
|
}
|
|
|
|
func isUniqueViolation(err error) bool {
|
|
return err != nil && (contains(err.Error(), "23505") || contains(err.Error(), "duplicate key") || contains(err.Error(), "unique constraint"))
|
|
}
|
|
|
|
func isForeignKeyViolation(err error) bool {
|
|
return err != nil && (contains(err.Error(), "23503") || contains(err.Error(), "violates foreign key"))
|
|
}
|
|
|
|
func isUUIDSyntaxError(err error) bool {
|
|
return err != nil && contains(err.Error(), "invalid input syntax for type uuid")
|
|
}
|
|
|
|
func contains(s, substr string) bool {
|
|
for i := 0; i+len(substr) <= len(s); i++ {
|
|
if s[i:i+len(substr)] == substr {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func variantKindToName(kind string) string {
|
|
switch kind {
|
|
case "thumbnail_256":
|
|
return "thumbnail"
|
|
case "crop_128":
|
|
return "crop"
|
|
case "optimized_webp":
|
|
return "optimized"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func variantNameToKind(name string) string {
|
|
switch name {
|
|
case "thumbnail":
|
|
return "thumbnail_256"
|
|
case "crop":
|
|
return "crop_128"
|
|
case "optimized":
|
|
return "optimized_webp"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (s *PostgresStore) loadVariants(ctx context.Context, attachmentID string) ([]any, error) {
|
|
rows, err := s.pool.Query(ctx, `
|
|
SELECT kind, object_key, content_type, size_bytes, width, height, sha256
|
|
FROM attachment_variants
|
|
WHERE attachment_id = $1
|
|
AND kind IN ('thumbnail_256', 'crop_128', 'optimized_webp')
|
|
ORDER BY kind
|
|
`, attachmentID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query variants: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := []any{}
|
|
for rows.Next() {
|
|
var v model.Variant
|
|
var kind string
|
|
if err := rows.Scan(&kind, &v.ObjectKey, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256); err != nil {
|
|
return nil, fmt.Errorf("scan variant: %w", err)
|
|
}
|
|
v.Name = variantKindToName(kind)
|
|
if v.Name == "" {
|
|
continue
|
|
}
|
|
out = append(out, v)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("variants rows: %w", err)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func envInt(key string, def int) int {
|
|
if v := os.Getenv(key); v != "" {
|
|
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
|
return n
|
|
}
|
|
}
|
|
return def
|
|
}
|
|
|
|
const jobCols = `id::text, attachment_id::text, status, attempts, max_attempts,
|
|
next_run_at, last_error, created_at, lease_until, leased_by, pipeline_version`
|
|
|
|
func scanJob(row pgx.Row) (*model.Job, error) {
|
|
var j model.Job
|
|
err := row.Scan(&j.ID, &j.AttachmentID, &j.Status, &j.Attempts, &j.MaxAttempts,
|
|
&j.NextRunAt, &j.LastError, &j.CreatedAt, &j.LeaseUntil, &j.LeasedBy, &j.PipelineVersion)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
j.CreatedAt, j.NextRunAt = j.CreatedAt.UTC(), j.NextRunAt.UTC()
|
|
return &j, nil
|
|
}
|
|
|
|
func (s *PostgresStore) GetJobByID(ctx context.Context, jobID string) (*model.Job, error) {
|
|
j, err := scanJob(s.pool.QueryRow(ctx,
|
|
`SELECT `+jobCols+` FROM attachment_jobs WHERE id=$1`, jobID))
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if isUUIDSyntaxError(err) {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
return nil, fmt.Errorf("get job by id: %w", err)
|
|
}
|
|
return j, nil
|
|
}
|
|
|
|
// ClaimJob — единственный вход в processing. Атомарно: проверка статуса,
|
|
// инкремент попытки и установка lease. Два worker не получат одну работу.
|
|
func (s *PostgresStore) ClaimJob(ctx context.Context, jobID, workerID string, lease time.Duration) (*model.Job, error) {
|
|
secs := int(lease.Seconds())
|
|
if secs < 1 {
|
|
secs = 60
|
|
}
|
|
j, err := scanJob(s.pool.QueryRow(ctx, `
|
|
UPDATE attachment_jobs
|
|
SET status='processing', attempts=attempts+1, leased_by=$2,
|
|
lease_until=NOW()+($3::int * INTERVAL '1 second'), updated_at=NOW()
|
|
WHERE id=$1
|
|
AND (status IN ('pending','published')
|
|
OR (status='processing' AND lease_until IS NOT NULL AND lease_until < NOW()))
|
|
RETURNING `+jobCols, jobID, workerID, secs))
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrConflict
|
|
}
|
|
return nil, fmt.Errorf("claim job: %w", err)
|
|
}
|
|
return j, nil
|
|
}
|
|
|
|
func (s *PostgresStore) RequeueExpiredLeases(ctx context.Context) (int64, error) {
|
|
tag, err := s.pool.Exec(ctx, `
|
|
UPDATE attachment_jobs
|
|
SET status='pending', lease_until=NULL, leased_by=NULL, next_run_at=NOW(), updated_at=NOW()
|
|
WHERE status='processing' AND lease_until IS NOT NULL AND lease_until < NOW()
|
|
`)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("requeue expired leases: %w", err)
|
|
}
|
|
return tag.RowsAffected(), nil
|
|
}
|
|
|
|
func (s *PostgresStore) RequeueAttachment(ctx context.Context, attachmentID string) error {
|
|
_, err := s.pool.Exec(ctx, `
|
|
UPDATE attachments SET status='queued', error_code=NULL, updated_at=NOW()
|
|
WHERE id=$1 AND status='processing'
|
|
`, attachmentID)
|
|
return err
|
|
}
|
|
|
|
// CompleteAttachment публикует результат атомарно: варианты + final key + ready + job done.
|
|
// Готовый результат не откатывается повторной доставкой.
|
|
func (s *PostgresStore) CompleteAttachment(ctx context.Context, attachmentID, jobID, finalKey string, variants []model.Variant) error {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("begin tx: %w", err)
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
var status string
|
|
var pv int
|
|
if err := tx.QueryRow(ctx,
|
|
`SELECT status, 1 FROM attachments WHERE id=$1 FOR UPDATE`, attachmentID).Scan(&status, &pv); err != nil {
|
|
return fmt.Errorf("lock attachment: %w", err)
|
|
}
|
|
|
|
if status != "ready" {
|
|
for _, v := range variants {
|
|
kind := variantNameToKind(v.Name)
|
|
if kind == "" {
|
|
return fmt.Errorf("unknown variant name %q", v.Name)
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
INSERT INTO attachment_variants
|
|
(attachment_id, kind, object_key, content_type, size_bytes, width, height, sha256, pipeline_version)
|
|
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,1)
|
|
ON CONFLICT (attachment_id, kind) DO UPDATE SET
|
|
object_key=EXCLUDED.object_key, content_type=EXCLUDED.content_type,
|
|
size_bytes=EXCLUDED.size_bytes, width=EXCLUDED.width,
|
|
height=EXCLUDED.height, sha256=EXCLUDED.sha256
|
|
`, attachmentID, kind, v.ObjectKey, v.ContentType, v.SizeBytes, v.Width, v.Height, v.SHA256); err != nil {
|
|
return fmt.Errorf("upsert variant %s: %w", kind, err)
|
|
}
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
UPDATE attachments SET object_key=$2, status='ready', error_code=NULL, updated_at=NOW()
|
|
WHERE id=$1
|
|
`, attachmentID, finalKey); err != nil {
|
|
return fmt.Errorf("publish final key: %w", err)
|
|
}
|
|
}
|
|
|
|
if _, err := tx.Exec(ctx, `
|
|
UPDATE attachment_jobs
|
|
SET status='done', lease_until=NULL, leased_by=NULL, last_error=NULL, updated_at=NOW()
|
|
WHERE id=$1
|
|
`, jobID); err != nil {
|
|
return fmt.Errorf("mark job done: %w", err)
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
func (s *PostgresStore) FailJob(ctx context.Context, jobID, attachmentID, errorCode, detail string) error {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
if _, err := tx.Exec(ctx, `
|
|
UPDATE attachments SET status='failed', error_code=$2, updated_at=NOW()
|
|
WHERE id=$1 AND status <> 'ready'
|
|
`, attachmentID, errorCode); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
UPDATE attachment_jobs
|
|
SET status='failed', last_error=$2, lease_until=NULL, leased_by=NULL, updated_at=NOW()
|
|
WHERE id=$1
|
|
`, jobID, detail); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
func (s *PostgresStore) RetryJob(ctx context.Context, jobID, attachmentID, errorCode, detail string, delay time.Duration) error {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
if _, err := tx.Exec(ctx, `
|
|
UPDATE attachments SET status='queued', error_code=NULL, updated_at=NOW()
|
|
WHERE id=$1 AND status <> 'ready'
|
|
`, attachmentID); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.Exec(ctx, `
|
|
UPDATE attachment_jobs
|
|
SET status='pending', last_error=$2, lease_until=NULL, leased_by=NULL,
|
|
next_run_at=NOW()+($3::bigint * INTERVAL '1 millisecond'), updated_at=NOW()
|
|
WHERE id=$1
|
|
`, jobID, errorCode+": "+detail, int64(delay.Milliseconds())); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
func (s *PostgresStore) ListCompletedStaging(ctx context.Context, olderThan time.Time) ([]model.StagingRef, error) {
|
|
rows, err := s.pool.Query(ctx, `
|
|
SELECT id::text, staging_key FROM attachments
|
|
WHERE status='ready' AND staging_key IS NOT NULL AND updated_at < $1
|
|
LIMIT 100
|
|
`, olderThan)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []model.StagingRef{}
|
|
for rows.Next() {
|
|
var r model.StagingRef
|
|
if err := rows.Scan(&r.ID, &r.StagingKey); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, r)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *PostgresStore) ClearStagingKey(ctx context.Context, attachmentID string) error {
|
|
_, err := s.pool.Exec(ctx, `UPDATE attachments SET staging_key=NULL WHERE id=$1`, attachmentID)
|
|
return err
|
|
}
|