-
Notifications
You must be signed in to change notification settings - Fork 197
/
runner.go
100 lines (86 loc) · 2.53 KB
/
runner.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package runner
import (
"context"
"errors"
"time"
"k8s.io/klog"
)
// ErrTimeout is returned when Runner's Run method fails due to a timeout event.
var ErrTimeout = errors.New("runner received timeout")
// Runnable is the interface that wraps the basic Run method.
//
// Run should be implemented by any task intended to be executed by the Runner.
type Runnable interface {
Run(ctx context.Context) error
}
// The RunnableFunc type is an adapter to allow the use of ordinary functions as Runnable tasks.
// If f is a function with the appropriate signature, RunnableFunc(f) is a Runnable that calls f.
type RunnableFunc func(ctx context.Context) error
// Run calls f()
func (f RunnableFunc) Run(ctx context.Context) error {
return f(ctx)
}
// Runner is the interface that wraps the basic Run method.
//
// Run executes submitted Runnable tasks.
type Runner interface {
Run(ctx context.Context, task Runnable) error
}
// New constructs a new ready-to-use Runner for running a Runnable task.
func New() Runner {
return &runner{
complete: make(chan error),
timeoutDuration: 0,
}
}
// NewWithTimeout constructs a new ready-to-use Runner with the specified timeout for running a Runnable task.
func NewWithTimeout(d time.Duration) Runner {
return &runner{
complete: make(chan error),
timeoutDuration: d,
timeout: time.After(d),
}
}
type runner struct {
// complete channel reports that processing is done
complete chan error
// timeout duration
timeoutDuration time.Duration
// timeout channel reports that time has run out
timeout <-chan time.Time
}
// Run runs the specified task and monitors channel events.
func (r *runner) Run(ctx context.Context, task Runnable) (err error) {
go func() {
r.complete <- task.Run(ctx)
}()
if r.timeoutDuration > 0 {
err = r.runWithTimeout()
return
} else {
err = r.runAndWaitForever()
return
}
}
func (r *runner) runAndWaitForever() (err error) {
klog.V(3).Info("Running task and waiting forever")
select {
// Signaled when processing is done.
case err := <-r.complete:
klog.V(3).Infof("Stopping runner on task completion with error: %v", err)
return err
}
}
func (r *runner) runWithTimeout() (err error) {
klog.V(3).Infof("Running task with timeout: %v", r.timeoutDuration)
select {
// Signaled when processing is done.
case err := <-r.complete:
klog.V(3).Infof("Stopping runner on task completion with error: %v", err)
return err
// Signaled when we run out of time.
case <-r.timeout:
klog.V(3).Info("Stopping runner on timeout")
return ErrTimeout
}
}