forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// ExpireStaging atomically fences replay and returns old terminal sources in bounded batches.
|
||||
func (s *Store) ExpireStaging(ctx context.Context) ([]string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
rows, err := s.pool.Query(ctx, `WITH candidates AS (
|
||||
SELECT a.id FROM attachments a
|
||||
WHERE a.status IN ('ready','failed') AND a.updated_at<clock_timestamp()-interval '7 days'
|
||||
AND EXISTS(SELECT 1 FROM attachment_jobs j WHERE j.attachment_id=a.id AND NOT j.staging_removed)
|
||||
ORDER BY a.id LIMIT 100 FOR UPDATE OF a SKIP LOCKED
|
||||
), expired AS (
|
||||
UPDATE attachment_jobs SET staging_expired=true WHERE attachment_id IN (SELECT id FROM candidates)
|
||||
RETURNING staging_key
|
||||
) SELECT DISTINCT staging_key FROM expired`)
|
||||
if err != nil {
|
||||
return nil, normalize(err)
|
||||
}
|
||||
keys, err := pgx.CollectRows(rows, pgx.RowTo[string])
|
||||
return keys, normalize(err)
|
||||
}
|
||||
|
||||
// StagingRemoved records confirmed deletion; a failed acknowledgement is safe to repeat.
|
||||
func (s *Store) StagingRemoved(ctx context.Context, key string) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
_, err := s.pool.Exec(ctx, `UPDATE attachment_jobs SET staging_removed=true WHERE staging_key=@key AND staging_expired`, pgx.StrictNamedArgs{"key": key})
|
||||
return normalize(err)
|
||||
}
|
||||
Reference in New Issue
Block a user