-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.go
68 lines (59 loc) · 1.61 KB
/
function.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package goschedule
import (
"fmt"
"reflect"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
type function struct {
implementation interface{}
params []interface{}
latestExecutionTime time.Duration
identifier string
res []reflect.Value
}
type functionInfo struct {
LatestExecutionTime time.Duration
Results []interface{}
}
func newFunction(implementation interface{}, params []interface{}, identifier string) *function {
return &function{
implementation: implementation,
params: params,
identifier: identifier,
}
}
func (f *function) runFunc(wg *sync.WaitGroup) {
log.Info("Running function inside job ", f.identifier)
defer wg.Done()
now := time.Now()
fValue := reflect.ValueOf(f.implementation)
if len(f.params) != fValue.Type().NumIn() {
fmt.Println(fValue.Type().NumIn(), len(f.params))
return
}
var params []reflect.Value
for _, param := range f.params {
params = append(params, reflect.ValueOf(param))
}
f.res = fValue.Call(params)
f.latestExecutionTime = time.Since(now)
log.Info("Function inside job ", f.identifier, " execution time is ", f.latestExecutionTime, " results are ", f.res)
}
func (f *function) getLatestExecutionTime() time.Duration {
return f.latestExecutionTime
}
func (f *function) getLatestResults() []interface{} {
var results []interface{}
for _, res := range f.res {
results = append(results, res.Interface())
}
return results
}
func (f *function) GetFuncInfo() *functionInfo {
return &functionInfo{
LatestExecutionTime: f.getLatestExecutionTime(),
Results: f.getLatestResults(),
}
}