2021-05-20 14:07:23 +00:00
|
|
|
package routine
|
2019-10-04 17:08:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-10-04 17:08:45 +00:00
|
|
|
)
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
type Routine func(ctx context.Context) error
|
2019-10-04 17:08:45 +00:00
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
type routineTracker struct {
|
2020-11-13 19:56:41 +00:00
|
|
|
cancel context.CancelFunc
|
|
|
|
stoppedCh chan struct{} // closed when no longer running
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (r *routineTracker) running() bool {
|
2020-11-13 19:56:41 +00:00
|
|
|
select {
|
|
|
|
case <-r.stoppedCh:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
type Manager struct {
|
2019-10-04 17:08:45 +00:00
|
|
|
lock sync.RWMutex
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
2019-10-04 17:08:45 +00:00
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
routines map[string]*routineTracker
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func NewManager(logger hclog.Logger) *Manager {
|
2019-10-04 17:08:45 +00:00
|
|
|
if logger == nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
logger = hclog.New(&hclog.LoggerOptions{
|
|
|
|
Output: os.Stderr,
|
|
|
|
})
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
return &Manager{
|
|
|
|
logger: logger,
|
|
|
|
routines: make(map[string]*routineTracker),
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) IsRunning(name string) bool {
|
2019-10-04 17:08:45 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
if routine, ok := m.routines[name]; ok {
|
2020-11-13 19:56:41 +00:00
|
|
|
return routine.running()
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) Start(ctx context.Context, name string, routine Routine) error {
|
2019-10-04 17:08:45 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
2020-11-13 19:56:41 +00:00
|
|
|
if instance, ok := m.routines[name]; ok && instance.running() {
|
2019-10-04 17:08:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
rtCtx, cancel := context.WithCancel(ctx)
|
|
|
|
instance := &routineTracker{
|
2020-11-13 19:56:41 +00:00
|
|
|
cancel: cancel,
|
|
|
|
stoppedCh: make(chan struct{}),
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
go m.execute(rtCtx, name, routine, instance.stoppedCh)
|
2019-10-04 17:08:45 +00:00
|
|
|
|
|
|
|
m.routines[name] = instance
|
2020-01-28 23:50:41 +00:00
|
|
|
m.logger.Info("started routine", "routine", name)
|
2019-10-04 17:08:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
// execute will run the given routine in the foreground and close the given channel when its done executing
|
|
|
|
func (m *Manager) execute(ctx context.Context, name string, routine Routine, done chan struct{}) {
|
|
|
|
defer func() {
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := routine(ctx)
|
|
|
|
if err != nil && err != context.DeadlineExceeded && err != context.Canceled {
|
|
|
|
m.logger.Error("routine exited with error",
|
|
|
|
"routine", name,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
m.logger.Debug("stopped routine", "routine", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) Stop(name string) <-chan struct{} {
|
2020-11-13 19:56:41 +00:00
|
|
|
instance := m.stopInstance(name)
|
|
|
|
if instance == nil {
|
|
|
|
// Fabricate a closed channel so it won't block forever.
|
|
|
|
ch := make(chan struct{})
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance.stoppedCh
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) stopInstance(name string) *routineTracker {
|
2019-10-04 17:08:45 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
instance, ok := m.routines[name]
|
|
|
|
if !ok {
|
|
|
|
// no running instance
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:56:41 +00:00
|
|
|
if !instance.running() {
|
|
|
|
return instance
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
m.logger.Debug("stopping routine", "routine", name)
|
2019-10-04 17:08:45 +00:00
|
|
|
instance.cancel()
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2019-10-04 17:08:45 +00:00
|
|
|
delete(m.routines, name)
|
2020-11-13 19:56:41 +00:00
|
|
|
|
|
|
|
return instance
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (m *Manager) StopAll() {
|
2019-10-04 17:08:45 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
for name, routine := range m.routines {
|
2020-11-13 19:56:41 +00:00
|
|
|
if !routine.running() {
|
2019-10-04 17:08:45 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
m.logger.Debug("stopping routine", "routine", name)
|
2019-10-04 17:08:45 +00:00
|
|
|
routine.cancel()
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// just wipe out the entire map
|
2021-05-20 14:07:23 +00:00
|
|
|
m.routines = make(map[string]*routineTracker)
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|