fix: enforce pipeline lifecycle limits
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab5/internal/model"
|
||||
)
|
||||
|
||||
func (s *Store) RenewAttachmentJobLease(
|
||||
ctx context.Context,
|
||||
lease model.AttachmentJobLease,
|
||||
duration time.Duration,
|
||||
) (bool, error) {
|
||||
if duration <= 0 || lease.Job.ID == "" || lease.Job.LeaseToken == "" || lease.Job.LeaseOwner == "" {
|
||||
return false, model.ErrInvalidInput
|
||||
}
|
||||
const query = `
|
||||
UPDATE attachment_jobs
|
||||
SET lease_until = clock_timestamp() + ($4::bigint * interval '1 microsecond'),
|
||||
updated_at = clock_timestamp()
|
||||
WHERE id = $1 AND lease_token = $2 AND lease_owner = $3
|
||||
AND status = 'processing' AND lease_until > clock_timestamp()`
|
||||
result, err := s.pool.Exec(
|
||||
ctx, query, lease.Job.ID, lease.Job.LeaseToken, lease.Job.LeaseOwner, duration.Microseconds(),
|
||||
)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("renew attachment job lease: %w", err)
|
||||
}
|
||||
return result.RowsAffected() == 1, nil
|
||||
}
|
||||
@@ -111,3 +111,26 @@ func (s *Store) RecoverExpiredOutboxEvents(ctx context.Context, now time.Time) (
|
||||
}
|
||||
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