-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.go
More file actions
59 lines (47 loc) · 1.17 KB
/
Copy pathaction.go
File metadata and controls
59 lines (47 loc) · 1.17 KB
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
package triggerable
import (
"context"
)
func (a *actionImpl) Run(ctx context.Context) error {
return a.runFunc(ctx)
}
func (a *actionImpl) Name() string {
return a.name
}
func (a *actionImpl) RetryOnError(err error) (bool, func(ctx context.Context)) {
return a.retryOnError(err)
}
func Action(runFunc func(ctx context.Context) error, opts ...actionOption) *actionImpl {
a := &actionImpl{
runFunc: runFunc,
name: "unnamed",
retryOnError: func(err error) (bool, func(ctx context.Context)) {
return false, nil
},
}
for _, o := range opts {
o(a)
}
return a
}
func WithName(name string) actionOption {
return func(a *actionImpl) {
a.name = name
}
}
func WithRetryOnError(retryOnError func(err error) (bool, func(ctx context.Context))) actionOption {
return func(a *actionImpl) {
a.retryOnError = retryOnError
}
}
type actionOption func(a *actionImpl)
type actionImpl struct {
runFunc func(ctx context.Context) error
name string
retryOnError func(err error) (bool, func(ctx context.Context))
}
type action interface {
Name() string
Run(ctx context.Context) error
RetryOnError(err error) (retry bool, retryFunc func(ctx context.Context))
}