-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunnable.go
More file actions
154 lines (130 loc) · 2.82 KB
/
runnable.go
File metadata and controls
154 lines (130 loc) · 2.82 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package runnable
import (
"context"
"fmt"
"sync"
)
var (
ErrAlreadyRunning = fmt.Errorf("already running")
ErrNotRunning = fmt.Errorf("not running")
)
type Option interface {
apply(*runnable)
}
type Runnable interface {
Run(ctx context.Context) error
Stop(ctx context.Context) error
IsRunning() bool
}
type runnable struct {
runFunc func(ctx context.Context) error
runCtx context.Context
runCancel context.CancelFunc
runStop chan bool
isRunning bool
onStart func()
onStop func()
mu sync.Mutex
}
// New creates a new Runnable with the given runFunc.
//
// Example:
//
// type Monitor struct {
// runnable.Runnable
// }
//
// func (m *Monitor) run(ctx context.Context) error {
// // do something
// return nil
// }
//
// func NewMonitor() Monitor {
// m := Monitor{}
// m.Runnable = runnable.NewRunnable(m.run)
// return m
// }
func New(runFunc func(ctx context.Context) error, options ...Option) Runnable {
r := &runnable{
runFunc: runFunc,
}
for _, option := range options {
option.apply(r)
}
return r
}
// Run starts the runnable, if it is not already running. If the runnable is already running,
// it will return an ErrAlreadyRunning error. If the context is cancelled, it will return the
// context error.
//
// Example:
//
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// if err := runnable.Run(ctx); err != nil {
// log.Error(err)
// }
func (r *runnable) Run(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
}
r.mu.Lock()
if r.isRunning {
r.mu.Unlock()
return ErrAlreadyRunning
}
r.isRunning = true
r.runCtx, r.runCancel = context.WithCancel(ctx)
r.runStop = make(chan bool)
runCtx := r.runCtx
r.mu.Unlock()
defer func() {
if r.onStop != nil {
r.onStop()
}
r.mu.Lock()
r.isRunning = false
close(r.runStop)
r.mu.Unlock()
}()
if r.onStart != nil {
r.onStart()
}
return r.runFunc(runCtx)
}
// Stop stops the runnable, if it is running. If the context is cancelled, it will return the context error.
// If the runnable is not running, it will return an error.
// If the runnable is running, it will wait for the runnable to stop before returning.
//
// Example:
//
// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// if err := runnable.Stop(ctx); err != nil {
// log.Error(err)
// }
func (r *runnable) Stop(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
}
r.mu.Lock()
if !r.isRunning {
r.mu.Unlock()
return ErrNotRunning
}
runStop := r.runStop
r.mu.Unlock()
r.runCancel()
select {
case <-ctx.Done():
return ctx.Err()
case <-runStop:
return nil
}
}
// IsRunning returns true if the runnable is running, false otherwise.
func (r *runnable) IsRunning() bool {
r.mu.Lock()
defer r.mu.Unlock()
return r.isRunning
}