2023-08-01 18:50:30 +00:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TaskFunc defines the task to be run. The context is canceled when Stop is
|
|
|
|
// called to early stop scheduled task.
|
|
|
|
type TaskFunc func(ctx context.Context) (done bool)
|
|
|
|
|
|
|
|
const (
|
|
|
|
WorkNotDone = false
|
|
|
|
WorkDone = true
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConditionalRepeater runs a task at regular intervals until the task returns
|
|
|
|
// true. It doesn't allow running task in parallel and can be triggered early
|
|
|
|
// by call to RunUntilDone.
|
|
|
|
type ConditionalRepeater struct {
|
|
|
|
interval time.Duration
|
|
|
|
task TaskFunc
|
|
|
|
// nil if not running
|
|
|
|
ctx context.Context
|
2023-08-30 16:14:57 +00:00
|
|
|
ctxMu sync.Mutex
|
2023-08-01 18:50:30 +00:00
|
|
|
cancel context.CancelFunc
|
|
|
|
runNowCh chan bool
|
|
|
|
runNowMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConditionalRepeater(interval time.Duration, task TaskFunc) *ConditionalRepeater {
|
|
|
|
return &ConditionalRepeater{
|
|
|
|
interval: interval,
|
|
|
|
task: task,
|
|
|
|
runNowCh: make(chan bool, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunUntilDone starts the task immediately and continues to run it at the defined
|
|
|
|
// interval until the task returns true. Can be called multiple times but it
|
|
|
|
// does not allow multiple concurrent executions of the task.
|
|
|
|
func (t *ConditionalRepeater) RunUntilDone() {
|
2023-08-30 16:14:57 +00:00
|
|
|
t.ctxMu.Lock()
|
2023-08-01 18:50:30 +00:00
|
|
|
defer func() {
|
2023-08-30 16:14:57 +00:00
|
|
|
t.runNowMu.Lock()
|
2023-08-01 18:50:30 +00:00
|
|
|
if len(t.runNowCh) == 0 {
|
|
|
|
t.runNowCh <- true
|
|
|
|
}
|
2023-08-30 16:14:57 +00:00
|
|
|
t.runNowMu.Unlock()
|
|
|
|
t.ctxMu.Unlock()
|
2023-08-01 18:50:30 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
if t.ctx != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.ctx, t.cancel = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
2023-08-30 16:14:57 +00:00
|
|
|
t.ctxMu.Lock()
|
|
|
|
defer t.ctxMu.Unlock()
|
2023-08-01 18:50:30 +00:00
|
|
|
t.cancel()
|
|
|
|
t.ctx = nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(t.interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Stop was called or task returned true
|
|
|
|
case <-t.ctx.Done():
|
|
|
|
return
|
|
|
|
// Scheduled execution
|
|
|
|
case <-ticker.C:
|
|
|
|
if t.task(t.ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Start right away if requested
|
|
|
|
case <-t.runNowCh:
|
|
|
|
ticker.Reset(t.interval)
|
|
|
|
if t.task(t.ctx) {
|
2023-08-30 16:14:57 +00:00
|
|
|
t.runNowMu.Lock()
|
|
|
|
if len(t.runNowCh) == 0 {
|
|
|
|
t.runNowMu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.runNowMu.Unlock()
|
2023-08-01 18:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop forcefully stops the running task by canceling its context.
|
|
|
|
func (t *ConditionalRepeater) Stop() {
|
2023-08-30 16:14:57 +00:00
|
|
|
t.ctxMu.Lock()
|
|
|
|
defer t.ctxMu.Unlock()
|
|
|
|
if t.ctx != nil {
|
|
|
|
t.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ConditionalRepeater) IsRunning() bool {
|
|
|
|
t.ctxMu.Lock()
|
|
|
|
defer t.ctxMu.Unlock()
|
|
|
|
return t.ctx != nil
|
2023-08-01 18:50:30 +00:00
|
|
|
}
|