12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package task
- import (
- "context"
- "gd_crontab/common.in/utils"
- "fmt"
- "time"
- )
- type Task func() error
- func Do(ctx context.Context, tasks ...Task) (returnErr error) {
- utils.GC()
- // 开始时间
- 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
- }
|