123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package task
- import (
- "context"
- "gadm-ods/errors"
- "fmt"
- "time"
- "gadm-ods/common.in/utils"
- )
- type Task func() error
- func Do(ctx context.Context, tasks ...Task) (returnErr error) {
- utils.GC()
- if utils.ExitRecieved == true {
- return errors.SystemError
- }
- // 开始时间
- startTime := uint64(time.Now().UnixNano())
- // 抓异常代码
- defer func() {
- status := "SUCCESS"
- if r := recover(); r != nil {
- if e, ok := r.(error); ok {
- returnErr = e
- } else {
- returnErr = fmt.Errorf("%+v", r)
- }
- }
- if returnErr != nil {
- status = "FAIL"
- }
- printAccessLog(ctx, startTime, status)
- }()
- for _, task := range tasks {
- if task != nil {
- if err := task(); err != nil {
- returnErr = err
- return
- }
- }
- }
- return
- }
|