9
9
"sync/atomic"
10
10
"time"
11
11
12
- monkit "github.com/spacemonkeygo/monkit/v3"
12
+ "github.com/spacemonkeygo/monkit/v3"
13
13
"golang.org/x/sync/errgroup"
14
14
)
15
15
@@ -89,6 +89,9 @@ func (cycle *Cycle) Run(ctx context.Context, fn func(ctx context.Context) error)
89
89
cycle .initialize ()
90
90
defer close (cycle .stopped )
91
91
92
+ if cycle .Disabled () {
93
+ return nil
94
+ }
92
95
currentInterval := cycle .interval
93
96
cycle .ticker = time .NewTicker (currentInterval )
94
97
defer cycle .ticker .Stop ()
@@ -199,29 +202,55 @@ func (cycle *Cycle) Stop() {
199
202
}
200
203
201
204
// ChangeInterval allows to change the ticker interval after it has started.
205
+ // interval=-1 (disabled loop) is not allowed to be changed (it will panic).
202
206
func (cycle * Cycle ) ChangeInterval (interval time.Duration ) {
207
+ if cycle .Disabled () {
208
+ if interval != - 1 {
209
+ panic ("Change interval on a disabled cycle is not supported" )
210
+ }
211
+ return
212
+ }
213
+ if interval == - 1 {
214
+ panic ("Cannot disable an already initialized cycle." )
215
+ }
203
216
cycle .sendControl (cycleChangeInterval {interval })
204
217
}
205
218
206
219
// Pause pauses the cycle.
207
220
func (cycle * Cycle ) Pause () {
221
+ if cycle .Disabled () {
222
+ return
223
+ }
208
224
cycle .sendControl (cyclePause {})
209
225
}
210
226
211
227
// Restart restarts the ticker from 0.
212
228
func (cycle * Cycle ) Restart () {
229
+ if cycle .Disabled () {
230
+ return
231
+ }
213
232
cycle .sendControl (cycleContinue {})
214
233
}
215
234
216
- // Trigger ensures that the loop is done at least once.
235
+ // Trigger ensures that the loop is done at least once. Note that it will not run if
236
+ // the cycle is disabled.
237
+ // TODO: Trigger should probably run the cycle even if the cycle interval is disabled.
217
238
// If it's currently running it waits for the previous to complete and then runs.
218
239
func (cycle * Cycle ) Trigger () {
240
+ if cycle .Disabled () {
241
+ return
242
+ }
219
243
cycle .sendControl (cycleTrigger {})
220
244
}
221
245
222
246
// TriggerWait ensures that the loop is done at least once and waits for completion.
247
+ // Note that it will not run if the cycle is disabled.
248
+ // TODO: Trigger should probably run the cycle even if the cycle interval is disabled.
223
249
// If it's currently running it waits for the previous to complete and then runs.
224
250
func (cycle * Cycle ) TriggerWait () {
251
+ if cycle .Disabled () {
252
+ return
253
+ }
225
254
done := make (chan struct {})
226
255
227
256
cycle .sendControl (cycleTrigger {done })
@@ -231,6 +260,11 @@ func (cycle *Cycle) TriggerWait() {
231
260
}
232
261
}
233
262
263
+ // Disabled means the cycle is not intended to run (interval is -1).
264
+ func (cycle * Cycle ) Disabled () bool {
265
+ return cycle .interval == - 1
266
+ }
267
+
234
268
type cycleManualTriggerTag struct {}
235
269
236
270
func withManualTrigger (ctx context.Context ) context.Context {
0 commit comments