Files
unibots/shell/logger/cleanup.go
T
agent fc644ecd6e feat: import agents_and_robots platform as unibots (Matrix-out, unibus transport)
Reemplaza el scaffold del echobot por la plataforma completa de bots traida
desde ~/DataProyects/Github/agents_and_robots tras la operacion Matrix-out:
los bots ya no hablan por Matrix sino por el bus unibus (modelo todo-rooms +
E2E via shell/transportunibus sobre github.com/enmanuel/unibus/pkg/client).

- go.mod: replace de unibus -> ../unibus y de fn-registry -> ../../../.. (paths
  relativos reajustados a la nueva ubicacion dentro de fn_registry).
- app.md: bump a 0.2.0, descripcion + arquitectura + comandos + gotchas reales.
- modulo Go conservado como github.com/enmanuel/agents (sin reescribir imports).

agents_and_robots queda archivado como museo de la era Matrix.
2026-06-07 11:50:13 +02:00

85 lines
1.8 KiB
Go

package logger
import (
"context"
"os"
"path/filepath"
"strings"
"time"
)
// runCleanup periodically removes log files older than maxAgeDays for the
// given agent. It runs until ctx is cancelled.
func runCleanup(ctx context.Context, baseDir, agentID string, maxAgeDays int, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
// Run once immediately at startup.
cleanOldLogs(baseDir, agentID, maxAgeDays)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
cleanOldLogs(baseDir, agentID, maxAgeDays)
}
}
}
// cleanOldLogs removes .jsonl and .jsonl.gz files older than maxAgeDays.
func cleanOldLogs(baseDir, agentID string, maxAgeDays int) {
dir := filepath.Join(baseDir, agentID)
entries, err := os.ReadDir(dir)
if err != nil {
return
}
cutoff := time.Now().UTC().AddDate(0, 0, -maxAgeDays)
for _, e := range entries {
if e.IsDir() {
continue
}
name := e.Name()
if !isLogFile(name) {
continue
}
date := parseDateFromFilename(name)
if date.IsZero() {
continue
}
if date.Before(cutoff) {
os.Remove(filepath.Join(dir, name))
}
}
}
// isLogFile returns true for .jsonl and .jsonl.gz files.
func isLogFile(name string) bool {
return strings.HasSuffix(name, ".jsonl") || strings.HasSuffix(name, ".jsonl.gz")
}
// parseDateFromFilename extracts YYYY-MM-DD from filenames like:
//
// 2026-03-06.jsonl
// 2026-03-06.1.jsonl
// 2026-03-06.jsonl.gz
func parseDateFromFilename(name string) time.Time {
// Strip extensions.
base := strings.TrimSuffix(name, ".gz")
base = strings.TrimSuffix(base, ".jsonl")
// Remove numeric suffix (e.g., ".1" from "2026-03-06.1").
if idx := strings.LastIndex(base, "."); idx >= 0 {
candidate := base[:idx]
if t, err := time.Parse("2006-01-02", candidate); err == nil {
return t
}
}
t, _ := time.Parse("2006-01-02", base)
return t
}