1
0
Fork 0
golang-github-pocketbase-po.../tools/routine/routine.go
Daniel Baumann e28c88ef14
Adding upstream version 0.28.1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-05-22 10:57:38 +02:00

32 lines
631 B
Go

package routine
import (
"log"
"runtime/debug"
"sync"
)
// FireAndForget executes f() in a new go routine and auto recovers if panic.
//
// **Note:** Use this only if you are not interested in the result of f()
// and don't want to block the parent go routine.
func FireAndForget(f func(), wg ...*sync.WaitGroup) {
if len(wg) > 0 && wg[0] != nil {
wg[0].Add(1)
}
go func() {
if len(wg) > 0 && wg[0] != nil {
defer wg[0].Done()
}
defer func() {
if err := recover(); err != nil {
log.Printf("RECOVERED FROM PANIC (safe to ignore): %v", err)
log.Println(string(debug.Stack()))
}
}()
f()
}()
}