- BIRD filter chunking (500 CIDR); bounded job worker pool
- ListModulesPage SQL push-down; revision diff limit; batch revision prune
- DB pool tuning; Prometheus pipeline/job metrics
- Coalesce module_refresh via idempotency; GET /jobs/{id}/report
- JobAuditWriter foundation for job_audit persistence
Co-authored-by: Cursor <cursoragent@cursor.com>
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// JobAuditWriter persists async job lifecycle rows to job_audit (optional cross-process queue foundation).
|
|
type JobAuditWriter struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewJobAuditWriter(pool *pgxpool.Pool) *JobAuditWriter {
|
|
if pool == nil {
|
|
return nil
|
|
}
|
|
return &JobAuditWriter{pool: pool}
|
|
}
|
|
|
|
// UpsertRunning inserts or updates a running job row (best-effort).
|
|
func (w *JobAuditWriter) UpsertRunning(ctx context.Context, tenantID, jobID, kind string, idempotencyKey *string, meta map[string]any) {
|
|
if w == nil || w.pool == nil {
|
|
return
|
|
}
|
|
metaJSON, _ := json.Marshal(meta)
|
|
var idem any
|
|
if idempotencyKey != nil && *idempotencyKey != "" {
|
|
idem = *idempotencyKey
|
|
}
|
|
_, _ = w.pool.Exec(ctx, `
|
|
INSERT INTO job_audit (id, tenant_id, kind, status, idempotency_key, meta_json, created_at, started_at)
|
|
VALUES ($1::uuid, $2::uuid, $3, 'running', $4, $5::jsonb, now(), now())
|
|
ON CONFLICT (tenant_id, idempotency_key) WHERE idempotency_key IS NOT NULL
|
|
DO UPDATE SET status='running', started_at=now(), meta_json=EXCLUDED.meta_json`,
|
|
jobID, tenantID, kind, idem, metaJSON)
|
|
}
|
|
|
|
// MarkTerminal updates job_audit terminal state (best-effort).
|
|
func (w *JobAuditWriter) MarkTerminal(ctx context.Context, tenantID, jobID, status string, errMsg *string, finishedAt time.Time) {
|
|
if w == nil || w.pool == nil {
|
|
return
|
|
}
|
|
_, _ = w.pool.Exec(ctx, `
|
|
UPDATE job_audit SET status=$3, error_message=$4, finished_at=$5
|
|
WHERE id=$1::uuid AND tenant_id=$2::uuid`,
|
|
jobID, tenantID, status, errMsg, finishedAt.UTC())
|
|
}
|