first commit
This commit is contained in:
26
pkg/tools/safe.go
Normal file
26
pkg/tools/safe.go
Normal file
@ -0,0 +1,26 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user