refactor(maintenance): remove hardcoded retention and wire scheduler
RunPeriodicMaintenance и RunCleanup удалены; scheduler политик в StartBackground; deprecated /postgres/cleanup принимает policy_id. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -3,81 +3,18 @@ package pgmonitor
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// CleanupPolicy names safe retention policies.
|
||||
type CleanupPolicy string
|
||||
|
||||
const (
|
||||
PolicyJobAuditRetention CleanupPolicy = "job_audit_retention"
|
||||
PolicyASNCacheRetention CleanupPolicy = "asn_cache_retention"
|
||||
)
|
||||
|
||||
// CleanupRequest for POST /postgres/cleanup.
|
||||
// CleanupRequest for deprecated POST /postgres/cleanup (use /v1/maintenance/run).
|
||||
type CleanupRequest struct {
|
||||
Policy string `json:"policy"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
|
||||
// RunCleanup executes a named retention policy.
|
||||
func RunCleanup(ctx context.Context, pool *pgxpool.Pool, policy string, dryRun bool, limit int) (map[string]any, error) {
|
||||
if pool == nil {
|
||||
return nil, fmt.Errorf("pgmonitor: postgres not configured")
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = 10000
|
||||
}
|
||||
if limit > 100000 {
|
||||
limit = 100000
|
||||
}
|
||||
detail := map[string]any{"policy": policy, "dry_run": dryRun, "limit": limit}
|
||||
switch CleanupPolicy(policy) {
|
||||
case PolicyJobAuditRetention:
|
||||
cutoff := time.Now().UTC().Add(-90 * 24 * time.Hour)
|
||||
if dryRun {
|
||||
var n int64
|
||||
err := pool.QueryRow(ctx, `
|
||||
SELECT count(*) FROM job_audit
|
||||
WHERE created_at < $1 AND status IN ('succeeded', 'failed', 'cancelled')`, cutoff).Scan(&n)
|
||||
detail["would_delete"] = n
|
||||
return detail, err
|
||||
}
|
||||
tag, err := pool.Exec(ctx, `
|
||||
DELETE FROM job_audit
|
||||
WHERE id IN (
|
||||
SELECT id FROM job_audit
|
||||
WHERE created_at < $1 AND status IN ('succeeded', 'failed', 'cancelled')
|
||||
LIMIT $2
|
||||
)`, cutoff, limit)
|
||||
if err != nil {
|
||||
return detail, err
|
||||
}
|
||||
detail["deleted"] = tag.RowsAffected()
|
||||
return detail, nil
|
||||
case PolicyASNCacheRetention:
|
||||
cutoff := time.Now().UTC().Add(-7 * 24 * time.Hour)
|
||||
if dryRun {
|
||||
var n int64
|
||||
err := pool.QueryRow(ctx, `SELECT count(*) FROM asn_prefix_cache WHERE fetched_at < $1`, cutoff).Scan(&n)
|
||||
detail["would_delete"] = n
|
||||
return detail, err
|
||||
}
|
||||
tag, err := pool.Exec(ctx, `
|
||||
DELETE FROM asn_prefix_cache WHERE fetched_at < $1`, cutoff)
|
||||
if err != nil {
|
||||
return detail, err
|
||||
}
|
||||
detail["deleted"] = tag.RowsAffected()
|
||||
return detail, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("pgmonitor: unknown cleanup policy %q", policy)
|
||||
}
|
||||
PolicyID string `json:"policy_id"`
|
||||
Policy string `json:"policy"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
|
||||
// InsertMaintenanceAudit records an audit row at job start.
|
||||
|
||||
Reference in New Issue
Block a user