2019-10-04 17:08:45 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/logging"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-10-04 17:08:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LeaderRoutine func(ctx context.Context) error
|
|
|
|
|
|
|
|
type leaderRoutine struct {
|
|
|
|
running bool
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
type LeaderRoutineManager struct {
|
|
|
|
lock sync.RWMutex
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
2019-10-04 17:08:45 +00:00
|
|
|
|
|
|
|
routines map[string]*leaderRoutine
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
func NewLeaderRoutineManager(logger hclog.Logger) *LeaderRoutineManager {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return &LeaderRoutineManager{
|
2020-01-28 23:50:41 +00:00
|
|
|
logger: logger.Named(logging.Leader),
|
2019-10-04 17:08:45 +00:00
|
|
|
routines: make(map[string]*leaderRoutine),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *LeaderRoutineManager) IsRunning(name string) bool {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
if routine, ok := m.routines[name]; ok {
|
|
|
|
return routine.running
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *LeaderRoutineManager) Start(name string, routine LeaderRoutine) error {
|
2020-05-14 18:41:20 +00:00
|
|
|
return m.StartWithContext(context.TODO(), name, routine)
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *LeaderRoutineManager) StartWithContext(parentCtx context.Context, name string, routine LeaderRoutine) error {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
if instance, ok := m.routines[name]; ok && instance.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentCtx == nil {
|
|
|
|
parentCtx = context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(parentCtx)
|
|
|
|
instance := &leaderRoutine{
|
|
|
|
running: true,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := routine(ctx)
|
|
|
|
if err != nil && err != context.DeadlineExceeded && err != context.Canceled {
|
2020-01-28 23:50:41 +00:00
|
|
|
m.logger.Error("routine exited with error",
|
|
|
|
"routine", name,
|
|
|
|
"error", err,
|
|
|
|
)
|
2019-10-04 17:08:45 +00:00
|
|
|
} else {
|
2020-01-28 23:50:41 +00:00
|
|
|
m.logger.Debug("stopped routine", "routine", name)
|
2019-10-04 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.lock.Lock()
|
|
|
|
instance.running = false
|
|
|
|
m.lock.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *LeaderRoutineManager) Stop(name string) error {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
instance, ok := m.routines[name]
|
|
|
|
if !ok {
|
|
|
|
// no running instance
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !instance.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *LeaderRoutineManager) StopAll() {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
for name, routine := range m.routines {
|
|
|
|
if !routine.running {
|
|
|
|
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
|
2019-10-04 17:08:45 +00:00
|
|
|
m.routines = make(map[string]*leaderRoutine)
|
|
|
|
}
|