forked from mtusi2026/mtusi2026lab6
chore: add lab5 files and merge env.example.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type OutboxRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewOutboxRepo(pool *pgxpool.Pool) *OutboxRepo {
|
||||
return &OutboxRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *OutboxRepo) Enqueue(ctx context.Context, tx domain.Tx, e *domain.OutboxEntry) error {
|
||||
if e.AggregateType == "" || e.AggregateID == "" || e.EventType == "" {
|
||||
return &domain.ValidationError{
|
||||
Field: "outbox",
|
||||
Message: "aggregate_type, aggregate_id и event_type обязательны",
|
||||
}
|
||||
}
|
||||
if len(e.Payload) == 0 {
|
||||
return &domain.ValidationError{
|
||||
Field: "outbox.payload",
|
||||
Message: "не может быть пустым",
|
||||
}
|
||||
}
|
||||
|
||||
if e.ID == "" {
|
||||
e.ID = uuid.NewString()
|
||||
}
|
||||
if e.CreatedAt.IsZero() {
|
||||
e.CreatedAt = time.Now().UTC()
|
||||
}
|
||||
|
||||
const q = `
|
||||
INSERT INTO outbox
|
||||
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at)
|
||||
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8)
|
||||
`
|
||||
if err := tx.Exec(ctx, q,
|
||||
e.ID, e.AggregateType, e.AggregateID, e.EventType,
|
||||
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.OutboxEntry, error) {
|
||||
const q = `
|
||||
WITH claimed AS (
|
||||
SELECT id FROM outbox
|
||||
WHERE published_at IS NULL AND created_at <= now()
|
||||
ORDER BY created_at
|
||||
LIMIT $1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
UPDATE outbox o
|
||||
SET attempts = attempts + 1
|
||||
FROM claimed c
|
||||
WHERE o.id = c.id
|
||||
RETURNING o.id, o.aggregate_type, o.aggregate_id, o.event_type, o.payload, o.attempts, o.created_at, o.published_at
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, limit)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var entries []domain.OutboxEntry
|
||||
for rows.Next() {
|
||||
var e domain.OutboxEntry
|
||||
if err := rows.Scan(
|
||||
&e.ID, &e.AggregateType, &e.AggregateID, &e.EventType,
|
||||
&e.Payload, &e.Attempts, &e.CreatedAt, &e.PublishedAt,
|
||||
); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
entries = append(entries, e)
|
||||
}
|
||||
return entries, mapError(rows.Err())
|
||||
}
|
||||
|
||||
func (r *OutboxRepo) EnqueueDirect(ctx context.Context, e *domain.OutboxEntry) error {
|
||||
if e.ID == "" {
|
||||
e.ID = uuid.NewString()
|
||||
}
|
||||
const q = `
|
||||
INSERT INTO outbox
|
||||
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at)
|
||||
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8)
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q,
|
||||
e.ID, e.AggregateType, e.AggregateID, e.EventType,
|
||||
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt,
|
||||
)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (r *OutboxRepo) MarkPublished(ctx context.Context, ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
const q = `UPDATE outbox SET published_at = now() WHERE id = ANY($1::uuid[])`
|
||||
_, err := r.pool.Exec(ctx, q, ids)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
var _ domain.OutboxRepository = (*OutboxRepo)(nil)
|
||||
Reference in New Issue
Block a user