feat(reconciler): lab5 recover expired leases and cleanup staging
This commit is contained in:
@@ -173,47 +173,6 @@ func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) e
|
||||
}
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]domain.Attachment, error) {
|
||||
if limit <= 0 {
|
||||
return []domain.Attachment{}, nil
|
||||
}
|
||||
const q = `
|
||||
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
||||
object_key, status, error_code, created_at, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'pending' AND created_at < $1
|
||||
ORDER BY created_at
|
||||
LIMIT $2
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, olderThan, limit)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]domain.Attachment, 0, limit)
|
||||
for rows.Next() {
|
||||
var a domain.Attachment
|
||||
var status string
|
||||
if err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename,
|
||||
&a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey,
|
||||
&status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
a.Status = domain.AttachmentStatus(status)
|
||||
out = append(out, a)
|
||||
}
|
||||
return out, mapError(rows.Err())
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) DeletePending(ctx context.Context, id string) error {
|
||||
const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'pending'`
|
||||
if _, err := r.pool.Exec(ctx, q, id); err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]string, error) {
|
||||
if len(keys) == 0 {
|
||||
return []string{}, nil
|
||||
@@ -316,4 +275,78 @@ func (r *AttachmentRepo) ResetForReplay(ctx context.Context, tx domain.Tx, id st
|
||||
return tx.Exec(ctx, q, id)
|
||||
}
|
||||
|
||||
// для сверщика.
|
||||
func (r *AttachmentRepo) ListExpiredLeases(ctx context.Context, limit int) ([]domain.Attachment, error) {
|
||||
const q = `
|
||||
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
||||
object_key, status, error_code, created_at, updated_at,
|
||||
attempts, next_attempt_at, locked_until, pipeline_version, staging_key
|
||||
FROM attachments
|
||||
WHERE status = 'processing' AND locked_until < now()
|
||||
ORDER BY locked_until
|
||||
LIMIT $1
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, limit)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []domain.Attachment
|
||||
for rows.Next() {
|
||||
var a domain.Attachment
|
||||
var status string
|
||||
err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType,
|
||||
&a.SizeBytes, &a.SHA256, &a.ObjectKey, &status, &a.ErrorCode,
|
||||
&a.CreatedAt, &a.UpdatedAt, &a.Attempts, &a.NextAttemptAt, &a.LockedUntil,
|
||||
&a.PipelineVersion, &a.StagingKey)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
a.Status = domain.AttachmentStatus(status)
|
||||
out = append(out, a)
|
||||
}
|
||||
return out, mapError(rows.Err())
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ResetLease(ctx context.Context, id string) error {
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'queued', locked_until = NULL, updated_at = now()
|
||||
WHERE id = $1::uuid AND status = 'processing' AND locked_until < now()
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q, id)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ListStagingToCleanup(ctx context.Context, limit int) ([]domain.Attachment, error) {
|
||||
const q = `
|
||||
SELECT id, staging_key
|
||||
FROM attachments
|
||||
WHERE status = 'ready' AND staging_key IS NOT NULL
|
||||
LIMIT $1
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, limit)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []domain.Attachment
|
||||
for rows.Next() {
|
||||
var a domain.Attachment
|
||||
if err := rows.Scan(&a.ID, &a.StagingKey); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
out = append(out, a)
|
||||
}
|
||||
return out, mapError(rows.Err())
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ClearStagingKey(ctx context.Context, id string) error {
|
||||
const q = `UPDATE attachments SET staging_key = NULL WHERE id = $1::uuid`
|
||||
_, err := r.pool.Exec(ctx, q, id)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
var _ domain.AttachmentRepository = (*AttachmentRepo)(nil)
|
||||
|
||||
Reference in New Issue
Block a user