Files
servicebase/pkg/tools/safe.go
2025-11-18 17:48:20 +08:00

27 lines
357 B
Go

package tools
import (
"runtime/debug"
"servicebase/pkg/log"
)
func Recover(cleanups ...func()) {
for _, cleanup := range cleanups {
cleanup()
}
if p := recover(); p != nil {
log.ErrorF("occur panic: [%+v], stack info [%s]", p, debug.Stack())
}
}
func RunSafe(fn func()) {
defer Recover()
fn()
}
func GoSafe(fn func()) {
go RunSafe(fn)
}