123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package rpctasker
- import (
- "context"
- "fmt"
- "runtime"
- "git.getensh.com/common/gopkgsv2/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 {
- err = fmt.Errorf("%+v", r)
- }
- // 这里必须返回
- return
- }
- }()
- // 按顺序执行任务
- for _, task := range tasks {
- if task != nil {
- if err = task(); err != nil {
- panic(err)
- }
- }
- }
- return
- }
|