feat(storage): lab5 implement job claim, complete and variants upsert
This commit is contained in:
@@ -19,7 +19,3 @@ const (
|
|||||||
VariantCrop = "crop"
|
VariantCrop = "crop"
|
||||||
VariantWebP = "webp"
|
VariantWebP = "webp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AllVariantNames() []string {
|
|
||||||
return []string{VariantThumbnail, VariantCrop, VariantWebP}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package postgres
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"high-loud-apps-labs/internal/domain"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AttachmentVariantRepo struct {
|
||||||
|
pool *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAttachmentVariantRepo(pool *pgxpool.Pool) *AttachmentVariantRepo {
|
||||||
|
return &AttachmentVariantRepo{pool: pool}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *AttachmentVariantRepo) Upsert(ctx context.Context, tx domain.Tx, v *domain.AttachmentVariant) error {
|
||||||
|
const q = `
|
||||||
|
INSERT INTO attachment_variants
|
||||||
|
(attachment_id, name, object_key, content_type, size_bytes, width, height, sha256, created_at)
|
||||||
|
VALUES ($1::uuid, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
|
ON CONFLICT (attachment_id, name) DO NOTHING
|
||||||
|
`
|
||||||
|
return tx.Exec(ctx, q, v.AttachmentID, v.Name, v.ObjectKey, v.ContentType, v.SizeBytes, v.Width, v.Height, v.SHA256, v.CreatedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *AttachmentVariantRepo) ListByAttachment(ctx context.Context, id string) ([]domain.AttachmentVariant, error) {
|
||||||
|
const q = `SELECT attachment_id, name, object_key, content_type, size_bytes, width, height, sha256, created_at FROM attachment_variants WHERE attachment_id = $1::uuid`
|
||||||
|
rows, err := r.pool.Query(ctx, q, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, mapError(err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var out []domain.AttachmentVariant
|
||||||
|
for rows.Next() {
|
||||||
|
var v domain.AttachmentVariant
|
||||||
|
if err := rows.Scan(&v.AttachmentID, &v.Name, &v.ObjectKey, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256, &v.CreatedAt); err != nil {
|
||||||
|
return nil, mapError(err)
|
||||||
|
}
|
||||||
|
out = append(out, v)
|
||||||
|
}
|
||||||
|
return out, mapError(rows.Err())
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ domain.AttachmentVariantRepository = (*AttachmentVariantRepo)(nil)
|
||||||
@@ -3,6 +3,7 @@ package postgres
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
"high-loud-apps-labs/internal/domain"
|
"high-loud-apps-labs/internal/domain"
|
||||||
@@ -216,4 +217,62 @@ func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]str
|
|||||||
return out, mapError(rows.Err())
|
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)
|
var _ domain.AttachmentRepository = (*AttachmentRepo)(nil)
|
||||||
|
|||||||
@@ -12,9 +12,10 @@ func New(pool *pgxpool.Pool) *Storage {
|
|||||||
|
|
||||||
func (s *Storage) Pool() *pgxpool.Pool { return s.pool }
|
func (s *Storage) Pool() *pgxpool.Pool { return s.pool }
|
||||||
|
|
||||||
func (s *Storage) Users() *UserRepo { return &UserRepo{pool: s.pool} }
|
func (s *Storage) Users() *UserRepo { return &UserRepo{pool: s.pool} }
|
||||||
func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} }
|
func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} }
|
||||||
func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} }
|
func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} }
|
||||||
func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} }
|
func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} }
|
||||||
func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} }
|
func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} }
|
||||||
func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} }
|
func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} }
|
||||||
|
func (s *Storage) Variants() *AttachmentVariantRepo { return &AttachmentVariantRepo{pool: s.pool} }
|
||||||
|
|||||||
Reference in New Issue
Block a user