/
executor_single.go
51 lines (45 loc) · 1.06 KB
/
executor_single.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2020 The GoSchedule Authors. All rights reserved.
// Use of this source code is governed by BSD
// license that can be found in the LICENSE file.
package task_worker
import (
"time"
"github.com/jasonjoo2010/goschedule/log"
"github.com/jasonjoo2010/goschedule/types"
"github.com/jasonjoo2010/goschedule/utils"
"github.com/sirupsen/logrus"
)
type SingleExecutor struct {
worker *TaskWorker
task types.TaskSingle
}
func (m *SingleExecutor) execute(item interface{}) {
var (
succ bool
cost int64
)
defer func() {
if r := recover(); r != nil {
logrus.Error("Execute error: ", r)
traceData := utils.StackTraceData()
defer traceData.Recycle()
log.Error("Trace: ", traceData.String())
succ = false
}
m.worker.Statistics.Execute(succ, cost)
}()
t0 := time.Now()
succ = m.task.Execute(item, m.worker.ownSign)
cost = int64(time.Now().Sub(t0) / time.Millisecond)
}
func (m *SingleExecutor) ExecuteOrReturn() bool {
select {
case item, ok := <-m.worker.data:
if ok {
m.execute(item)
}
return true
default:
return false
}
}