279 lines
8.2 KiB
Go
279 lines
8.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"high-loud-apps-labs/internal/domain"
|
|
"time"
|
|
)
|
|
|
|
// pending -> ready (успешный upload)
|
|
// pending -> failed (отказ upload или reconciler)
|
|
type AttachmentRepo struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewAttachmentRepo(pool *pgxpool.Pool) *AttachmentRepo {
|
|
return &AttachmentRepo{pool: pool}
|
|
}
|
|
|
|
func (r *AttachmentRepo) Create(ctx context.Context, a *domain.Attachment) error {
|
|
const q = `
|
|
INSERT INTO attachments
|
|
(id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
|
object_key, status, error_code, created_at, updated_at)
|
|
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL, $10, $10)`
|
|
_, err := r.pool.Exec(ctx, q,
|
|
a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes,
|
|
a.SHA256, a.ObjectKey, string(a.Status), a.CreatedAt,
|
|
)
|
|
if err != nil {
|
|
if uniqueViolation(err) {
|
|
return &domain.AlreadyExistsError{
|
|
Entity: "attachment",
|
|
Field: "object_key",
|
|
Value: a.ObjectKey,
|
|
}
|
|
}
|
|
if foreignKeyViolation(err) {
|
|
return &domain.NotFoundError{Entity: "chat", ID: a.ChatID}
|
|
}
|
|
if checkViolation(err) {
|
|
return &domain.ValidationError{
|
|
Field: "attachment",
|
|
Message: "нарушено ограничение целостности",
|
|
}
|
|
}
|
|
return mapError(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *AttachmentRepo) CreateInTx(ctx context.Context, tx domain.Tx, a *domain.Attachment) error {
|
|
const q = `
|
|
INSERT INTO attachments
|
|
(id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
|
object_key, status, error_code,
|
|
attempts, pipeline_version, staging_key,
|
|
created_at, updated_at)
|
|
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL,
|
|
0, $10, $11, $12, $12)
|
|
`
|
|
return tx.Exec(ctx, q,
|
|
a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes,
|
|
a.SHA256, a.ObjectKey, string(a.Status),
|
|
a.PipelineVersion, a.StagingKey, a.CreatedAt,
|
|
)
|
|
}
|
|
|
|
func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attachment, error) {
|
|
const q = `
|
|
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
|
object_key, status, error_code, created_at, updated_at,
|
|
attempts, next_attempt_at, locked_until, pipeline_version, staging_key
|
|
FROM attachments
|
|
WHERE id = $1::uuid
|
|
`
|
|
a := &domain.Attachment{}
|
|
var status string
|
|
err := r.pool.QueryRow(ctx, q, id).
|
|
Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType,
|
|
&a.SizeBytes, &a.SHA256, &a.ObjectKey, &status, &a.ErrorCode,
|
|
&a.CreatedAt, &a.UpdatedAt,
|
|
&a.Attempts, &a.NextAttemptAt, &a.LockedUntil, &a.PipelineVersion, &a.StagingKey)
|
|
if err != nil {
|
|
return nil, notFound(err, "attachment", id)
|
|
}
|
|
a.Status = domain.AttachmentStatus(status)
|
|
return a, nil
|
|
}
|
|
|
|
// MarkReady переводит pending -> ready. Идемпотентна.
|
|
func (r *AttachmentRepo) MarkReady(ctx context.Context, id string) error {
|
|
const q = `
|
|
UPDATE attachments
|
|
SET status = 'ready', updated_at = now()
|
|
WHERE id = $1::uuid AND status = 'pending'
|
|
`
|
|
tag, err := r.pool.Exec(ctx, q, id)
|
|
if err != nil {
|
|
return mapError(err)
|
|
}
|
|
if tag.RowsAffected() > 0 {
|
|
return nil
|
|
}
|
|
|
|
var status string
|
|
err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id).
|
|
Scan(&status)
|
|
if err != nil {
|
|
return notFound(err, "attachment", id)
|
|
}
|
|
switch domain.AttachmentStatus(status) {
|
|
case domain.AttachmentStatusReady:
|
|
return nil // idempotent no-op
|
|
case domain.AttachmentStatusFailed:
|
|
return &domain.ConflictError{Reason: "нельзя перевести failed-вложение в ready"}
|
|
default:
|
|
return &domain.ConflictError{Reason: "некорректное состояние вложения"}
|
|
}
|
|
}
|
|
|
|
// MarkFailed переводит pending -> failed. Идемпотентна.
|
|
func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) error {
|
|
if errorCode == "" {
|
|
return &domain.ValidationError{Field: "error_code", Message: "не может быть пустым"}
|
|
}
|
|
const q = `
|
|
UPDATE attachments
|
|
SET status = 'failed', error_code = $2, updated_at = now()
|
|
WHERE id = $1::uuid AND status = 'pending'
|
|
`
|
|
tag, err := r.pool.Exec(ctx, q, id, errorCode)
|
|
if err != nil {
|
|
return mapError(err)
|
|
}
|
|
if tag.RowsAffected() > 0 {
|
|
return nil
|
|
}
|
|
|
|
var status string
|
|
err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id).
|
|
Scan(&status)
|
|
if err != nil {
|
|
return notFound(err, "attachment", id)
|
|
}
|
|
switch domain.AttachmentStatus(status) {
|
|
case domain.AttachmentStatusFailed, domain.AttachmentStatusReady:
|
|
return nil
|
|
default:
|
|
return &domain.ConflictError{Reason: "некорректное состояние вложения"}
|
|
}
|
|
}
|
|
|
|
func (r *AttachmentRepo) ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]domain.Attachment, error) {
|
|
if limit <= 0 {
|
|
return []domain.Attachment{}, nil
|
|
}
|
|
const q = `
|
|
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
|
object_key, status, error_code, created_at, updated_at
|
|
FROM attachments
|
|
WHERE status = 'pending' AND created_at < $1
|
|
ORDER BY created_at
|
|
LIMIT $2
|
|
`
|
|
rows, err := r.pool.Query(ctx, q, olderThan, limit)
|
|
if err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := make([]domain.Attachment, 0, limit)
|
|
for rows.Next() {
|
|
var a domain.Attachment
|
|
var status string
|
|
if err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename,
|
|
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey,
|
|
&status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt); err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
a.Status = domain.AttachmentStatus(status)
|
|
out = append(out, a)
|
|
}
|
|
return out, mapError(rows.Err())
|
|
}
|
|
|
|
func (r *AttachmentRepo) DeletePending(ctx context.Context, id string) error {
|
|
const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'pending'`
|
|
if _, err := r.pool.Exec(ctx, q, id); err != nil {
|
|
return mapError(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]string, error) {
|
|
if len(keys) == 0 {
|
|
return []string{}, nil
|
|
}
|
|
const q = `SELECT object_key FROM attachments WHERE object_key = ANY($1::text[])`
|
|
rows, err := r.pool.Query(ctx, q, keys)
|
|
if err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
out := make([]string, 0, len(keys))
|
|
for rows.Next() {
|
|
var k string
|
|
if err := rows.Scan(&k); err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
out = append(out, k)
|
|
}
|
|
return out, mapError(rows.Err())
|
|
}
|
|
|
|
func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*domain.Attachment, error) {
|
|
const q = `
|
|
UPDATE attachments
|
|
SET status = 'processing', locked_until = now() + $2, updated_at = now()
|
|
WHERE id = $1::uuid AND status = 'queued'
|
|
RETURNING id, chat_id, staging_key, pipeline_version
|
|
`
|
|
var a domain.Attachment
|
|
err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion)
|
|
if err != nil {
|
|
if err == pgx.ErrNoRows {
|
|
return nil, nil // Задача уже взята или выполнена
|
|
}
|
|
return nil, mapError(err)
|
|
}
|
|
return &a, nil
|
|
}
|
|
|
|
func (r *AttachmentRepo) CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []domain.AttachmentVariant) (bool, error) {
|
|
tx, err := r.pool.Begin(ctx)
|
|
if err != nil {
|
|
return false, mapError(err)
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
|
|
const q = `
|
|
UPDATE attachments
|
|
SET status = 'ready', locked_until = NULL, updated_at = now()
|
|
WHERE id = $1::uuid AND pipeline_version = $2 AND status = 'processing'
|
|
`
|
|
tag, err := tx.Exec(ctx, q, id, pipelineVersion)
|
|
if err != nil {
|
|
return false, mapError(err)
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return false, nil
|
|
}
|
|
|
|
variantRepo := NewAttachmentVariantRepo(r.pool)
|
|
for _, v := range variants {
|
|
if err := variantRepo.Upsert(ctx, &pgxTx{tx: tx}, &v); err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
|
|
return true, mapError(tx.Commit(ctx))
|
|
}
|
|
|
|
func (r *AttachmentRepo) FailJob(ctx context.Context, id string, errorCode string) error {
|
|
const q = `
|
|
UPDATE attachments
|
|
SET status = 'failed', error_code = $2, locked_until = NULL, updated_at = now()
|
|
WHERE id = $1::uuid AND status = 'processing'
|
|
`
|
|
_, err := r.pool.Exec(ctx, q, id, errorCode)
|
|
return mapError(err)
|
|
}
|
|
|
|
var _ domain.AttachmentRepository = (*AttachmentRepo)(nil)
|