feat(storage): lab5 implement delayed outbox and attempts tracking
This commit is contained in:
@@ -51,6 +51,7 @@ type OutboxRepository interface {
|
|||||||
Enqueue(ctx context.Context, tx Tx, e *OutboxEntry) error
|
Enqueue(ctx context.Context, tx Tx, e *OutboxEntry) error
|
||||||
ClaimBatch(ctx context.Context, limit int) ([]OutboxEntry, error)
|
ClaimBatch(ctx context.Context, limit int) ([]OutboxEntry, error)
|
||||||
MarkPublished(ctx context.Context, ids []string) error
|
MarkPublished(ctx context.Context, ids []string) error
|
||||||
|
EnqueueDirect(ctx context.Context, e *OutboxEntry) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type AttachmentJobRepository interface {
|
type AttachmentJobRepository interface {
|
||||||
|
|||||||
@@ -239,12 +239,12 @@ func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]str
|
|||||||
func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*domain.Attachment, error) {
|
func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, lease time.Duration) (*domain.Attachment, error) {
|
||||||
const q = `
|
const q = `
|
||||||
UPDATE attachments
|
UPDATE attachments
|
||||||
SET status = 'processing', locked_until = now() + $2, updated_at = now()
|
SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1 -- <--- ИНКРЕМЕНТ
|
||||||
WHERE id = $1::uuid AND status = 'queued'
|
WHERE id = $1::uuid AND status = 'queued'
|
||||||
RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type
|
RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type, attempts
|
||||||
`
|
`
|
||||||
var a domain.Attachment
|
var a domain.Attachment
|
||||||
err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType)
|
err := r.pool.QueryRow(ctx, q, id, lease).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType, &a.Attempts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == pgx.ErrNoRows {
|
if err == pgx.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.Outbox
|
|||||||
const q = `
|
const q = `
|
||||||
WITH claimed AS (
|
WITH claimed AS (
|
||||||
SELECT id FROM outbox
|
SELECT id FROM outbox
|
||||||
WHERE published_at IS NULL
|
WHERE published_at IS NULL AND created_at <= now()
|
||||||
ORDER BY created_at
|
ORDER BY created_at
|
||||||
LIMIT $1
|
LIMIT $1
|
||||||
FOR UPDATE SKIP LOCKED
|
FOR UPDATE SKIP LOCKED
|
||||||
@@ -88,6 +88,22 @@ func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.Outbox
|
|||||||
return entries, mapError(rows.Err())
|
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 {
|
func (r *OutboxRepo) MarkPublished(ctx context.Context, ids []string) error {
|
||||||
if len(ids) == 0 {
|
if len(ids) == 0 {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user