印度包网
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
1.4 KiB

package call
import (
"fmt"
"runtime"
"server/common"
"time"
"github.com/liangdas/mqant/log"
)
// Go 用协程处理f
func Go(f func()) {
go func() {
defer Recover()
f()
}()
}
func IndexTryCallback(f func() error, cb func()) {
Go(func() {
for i := 0; i <= 6; i++ {
if i > 0 {
next := time.Duration(i*i) * time.Minute
time.Sleep(next)
}
err := f()
if err == nil {
break
}
log.Error("err:%v next:%v", err, i)
if i == 6 {
cb()
}
}
})
}
func IndexTry(f func() error) {
Go(func() {
for i := 0; i <= 6; i++ {
if i > 0 {
next := time.Duration(i*i) * time.Minute
time.Sleep(next)
}
err := f()
if err == nil {
break
}
log.Error("err:%v next:%v", err, i)
}
})
}
func IndexTryS(f func() error) {
Go(func() {
for i := 0; i <= 6; i++ {
if i > 0 {
next := time.Duration(i*i) * time.Second
time.Sleep(next)
}
err := f()
if err == nil {
break
}
log.Error("err:%v next:%v", err, i)
}
})
}
// 捕获异常并打日志
// Usage: defer Recover()
func Recover() {
if err := recover(); err != nil {
buf := make([]byte, 1024)
runtime.Stack(buf, false)
str := fmt.Sprintf("%+v", err)
log.Error("panic(%s), stack:\n%s", str, string(buf))
name := GetCaller().GetType()
InsertToESGO(common.ESIndexBackPanic, common.ESPanic{
Module: name,
Func: str,
Time: time.Now().Unix(),
Error: string(buf),
})
}
}