textmachine/backend/internal/obs/safego.go

22 lines
679 B
Go

package obs
import (
"context"
"log/slog"
"runtime/debug"
)
// SafeGo runs fn in a goroutine with panic recovery: a panic in a background
// task (async telemetry write, retention trim) logs an ERROR with the stack
// and dies alone instead of taking the process down. Вынесено из vojo bot.go —
// без него async-паттерн телеметрии не переносится.
func SafeGo(ctx context.Context, log *slog.Logger, name string, fn func()) {
go func() {
defer func() {
if r := recover(); r != nil {
log.ErrorContext(ctx, "background task panicked", "task", name, "panic", r, "stack", string(debug.Stack()))
}
}()
fn()
}()
}