1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package rpctasker
- import (
- "context"
- "fmt"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "runtime"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/tasker"
- )
- // 打印调用者函数名
- func callerName() string {
- pc, _, _, _ := runtime.Caller(2)
- return runtime.FuncForPC(pc).Name()
- }
- // Task 任务函数
- type Task func() error
- // Exec 执行任务
- func Exec(ctx context.Context, tasks ...Task) (err error) {
- // 进程中断中,不再处理任务
- if tasker.Interrupted() {
- return tasker.InterruptError
- }
- // 抓异常代码
- defer func() {
- if r := recover(); r != nil {
- if e, ok := r.(error); ok {
- err = e
- } else {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- err = status.Error(10002, fmt.Sprint("内部服务错误:%v", r))
- }
- // 这里必须返回
- return
- }
- }()
- // 按顺序执行任务
- for _, task := range tasks {
- if task != nil {
- if err = task(); err != nil {
- panic(err)
- }
- }
- }
- return
- }
|