SPB Git

Repository Public

housekeeping.go 3.3 KB 85 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/internal/orchestrator/housekeeping.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package orchestrator4344import (45	"context"46	"time"4748	"github.com/jackc/pgx/v5/pgxpool"49)5051// DefaultActionEventRetention applies when settings['retention.action_events_days'] is absent or invalid.52const DefaultActionEventRetention = 905354// Housekeep prunes what may be pruned: output and lifecycle events of55// FINISHED actions older than the retention setting (the actions themselves56// are kept), and delivered notification records older than 30 days. The audit57// log is never touched. Deletion is batched so a large backlog cannot hold a58// long lock.59func Housekeep(ctx context.Context, pool *pgxpool.Pool, now time.Time) (events, notifications int64, err error) {60	days := DefaultActionEventRetention61	var v *float6462	if err := pool.QueryRow(ctx, `select case when jsonb_typeof(value)='number' then (value::text)::float8 end from settings where key='retention.action_events_days'`).Scan(&v); err == nil && v != nil && *v >= 1 && *v <= 3650 {63		days = int(*v)64	}65	cutoff := now.Add(-time.Duration(days) * 24 * time.Hour)66	for i := 0; i < 50; i++ {67		tag, err := pool.Exec(ctx, `delete from action_events where id in (68			select e.id from action_events e join actions a on a.id=e.action_id69			where e.ts < $1 and a.state in ('succeeded','failed','cancelled','timed_out','rejected','expired') and a.finished_at < $170			order by e.id limit 5000)`, cutoff)71		if err != nil {72			return events, notifications, err73		}74		events += tag.RowsAffected()75		if tag.RowsAffected() < 5000 {76			break77		}78	}79	tag, err := pool.Exec(ctx, `delete from alert_notifications where done_at is not null and done_at < $1`, now.Add(-30*24*time.Hour))80	if err != nil {81		return events, notifications, err82	}83	return events, tag.RowsAffected(), nil84}85
Files main
Menu

Theme