-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
48 lines (41 loc) · 913 Bytes
/
util.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
package conexec
import (
"context"
"fmt"
"runtime/debug"
"sync"
"time"
)
// DurationPtr helps to make a duration ptr
func DurationPtr(t time.Duration) *time.Duration {
return &t
}
// wrapperTask will wrapper the task in order to notice execution result
// to the main process
func wrapperTask(ctx context.Context, task Task,
wg *sync.WaitGroup, resChan chan error) func() {
return func() {
defer func() {
if r := recover(); r != nil {
err := fmt.Errorf("conexec panic:%v\n%s", r, string(debug.Stack()))
resChan <- err
}
wg.Done()
}()
select {
case <-ctx.Done():
return // fast return
case resChan <- task():
}
}
}
// setOptions set the options for actuator
func setOptions(c TimedActuator, options ...*Options) {
if options == nil || len(options) == 0 || options[0] == nil {
return
}
opt := options[0]
if opt.TimeOut != nil {
c.setTimeout(opt.TimeOut)
}
}