forked from mtusi2026/mtusi2026lab6
chore: import lab5 baseline
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/model"
|
||||
)
|
||||
|
||||
func (s *Store) ClaimOutboxEvents(
|
||||
ctx context.Context,
|
||||
limit int,
|
||||
leaseDuration time.Duration,
|
||||
) ([]model.OutboxEvent, error) {
|
||||
if limit < 1 || limit > 1000 || leaseDuration <= 0 {
|
||||
return nil, model.ErrInvalidInput
|
||||
}
|
||||
claimToken := uuid.NewString()
|
||||
query := `
|
||||
WITH candidates AS (
|
||||
SELECT id
|
||||
FROM outbox_events
|
||||
WHERE published_at IS NULL
|
||||
AND available_at <= clock_timestamp()
|
||||
AND (claim_until IS NULL OR claim_until <= clock_timestamp())
|
||||
ORDER BY available_at, id
|
||||
LIMIT $1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
UPDATE outbox_events event
|
||||
SET claim_token = $2,
|
||||
claim_until = clock_timestamp() + $3 * INTERVAL '1 millisecond',
|
||||
publish_tries = event.publish_tries + 1
|
||||
FROM candidates
|
||||
WHERE event.id = candidates.id
|
||||
RETURNING event.id, event.topic, event.partition_key, event.payload, event.job_id,
|
||||
event.attempt, event.available_at, event.publish_tries, event.claim_token,
|
||||
event.claim_until, event.last_error_code, event.created_at`
|
||||
rows, err := s.pool.Query(ctx, query, limit, claimToken, leaseDuration.Milliseconds())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("claim outbox events: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
events, err := pgx.CollectRows(rows, pgx.RowToFunc[model.OutboxEvent](func(row pgx.CollectableRow) (model.OutboxEvent, error) {
|
||||
var event model.OutboxEvent
|
||||
err := row.Scan(
|
||||
&event.ID, &event.Topic, &event.PartitionKey, &event.Payload, &event.JobID,
|
||||
&event.Attempt, &event.AvailableAt, &event.PublishTries, &event.LeaseToken,
|
||||
&event.LeaseUntil, &event.LastErrorCode, &event.CreatedAt,
|
||||
)
|
||||
return event, err
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan claimed outbox events: %w", err)
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (s *Store) MarkOutboxPublished(ctx context.Context, eventID, leaseToken string) (bool, error) {
|
||||
const query = `
|
||||
UPDATE outbox_events
|
||||
SET published_at = clock_timestamp(), claim_token = NULL, claim_until = NULL,
|
||||
last_error_code = NULL
|
||||
WHERE id = $1 AND claim_token = $2 AND published_at IS NULL
|
||||
AND claim_until > clock_timestamp()`
|
||||
result, err := s.pool.Exec(ctx, query, eventID, leaseToken)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("mark outbox event published: %w", err)
|
||||
}
|
||||
return result.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
func (s *Store) ReleaseOutboxEvent(
|
||||
ctx context.Context,
|
||||
eventID string,
|
||||
leaseToken string,
|
||||
errorCode string,
|
||||
nextAttemptAt time.Time,
|
||||
) (bool, error) {
|
||||
if !validErrorCode(errorCode) || nextAttemptAt.IsZero() {
|
||||
return false, model.ErrInvalidInput
|
||||
}
|
||||
const query = `
|
||||
UPDATE outbox_events
|
||||
SET available_at = $3, claim_token = NULL, claim_until = NULL, last_error_code = $4
|
||||
WHERE id = $1 AND claim_token = $2 AND published_at IS NULL
|
||||
AND claim_until > clock_timestamp()`
|
||||
result, err := s.pool.Exec(ctx, query, eventID, leaseToken, nextAttemptAt.UTC(), errorCode)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("release outbox event: %w", err)
|
||||
}
|
||||
return result.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
func (s *Store) RecoverExpiredOutboxEvents(ctx context.Context, now time.Time) (int, error) {
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
}
|
||||
const query = `
|
||||
UPDATE outbox_events
|
||||
SET claim_token = NULL, claim_until = NULL
|
||||
WHERE published_at IS NULL AND claim_until <= $1`
|
||||
result, err := s.pool.Exec(ctx, query, now.UTC())
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("recover expired outbox events: %w", err)
|
||||
}
|
||||
return int(result.RowsAffected()), nil
|
||||
}
|
||||
|
||||
func (s *Store) DeletePublishedOutboxEvents(ctx context.Context, publishedBefore time.Time, limit int) (int, error) {
|
||||
if publishedBefore.IsZero() || limit < 1 || limit > 1000 {
|
||||
return 0, model.ErrInvalidInput
|
||||
}
|
||||
const query = `
|
||||
WITH candidates AS (
|
||||
SELECT id
|
||||
FROM outbox_events
|
||||
WHERE published_at < $1
|
||||
ORDER BY published_at, id
|
||||
LIMIT $2
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
DELETE FROM outbox_events event
|
||||
USING candidates
|
||||
WHERE event.id = candidates.id`
|
||||
result, err := s.pool.Exec(ctx, query, publishedBefore.UTC(), limit)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("delete published outbox events: %w", err)
|
||||
}
|
||||
return int(result.RowsAffected()), nil
|
||||
}
|
||||
Reference in New Issue
Block a user