refactor service store to manager

pull/375/head
Minh Doan 6 years ago committed by Minh Doan
parent 24ca370926
commit 44b13478cc
  1. 42
      api/service/manager.go
  2. 10
      api/service/manager_test.go

@ -50,7 +50,7 @@ func (t Type) String() string {
// Constants for timing.
const (
// WaitForStatusUpdate is the delay time to update new status. Currently set 1 second for development. Should be 30 minutes for production.
// WaitForStatusUpdate is the delay time to update new statum. Currently set 1 second for development. Should be 30 minutes for production.
WaitForStatusUpdate = time.Minute * 1
)
@ -67,48 +67,48 @@ type Interface interface {
StopService()
}
// Store stores all services for service manager.
type Store struct {
// Manager stores all services for service manager.
type Manager struct {
services map[Type]Interface
actionChannel chan *Action
}
// Register new service to service store.
func (s *Store) Register(t Type, service Interface) {
if s.services == nil {
s.services = make(map[Type]Interface)
func (m *Manager) Register(t Type, service Interface) {
if m.services == nil {
m.services = make(map[Type]Interface)
}
s.services[t] = service
m.services[t] = service
}
// SetupServiceManager inits service map and start service manager.
func (s *Store) SetupServiceManager() {
s.InitServiceMap()
s.actionChannel = s.StartServiceManager()
func (m *Manager) SetupServiceManager() {
m.InitServiceMap()
m.actionChannel = m.StartServiceManager()
}
// RegisterService is used for testing.
func (s *Store) RegisterService(t Type, service Interface) {
s.Register(t, service)
func (m *Manager) RegisterService(t Type, service Interface) {
m.Register(t, service)
}
// InitServiceMap initializes service map.
func (s *Store) InitServiceMap() {
s.services = make(map[Type]Interface)
func (m *Manager) InitServiceMap() {
m.services = make(map[Type]Interface)
}
// SendAction sends action to action channel which is observed by service manager.
func (s *Store) SendAction(action *Action) {
s.actionChannel <- action
func (m *Manager) SendAction(action *Action) {
m.actionChannel <- action
}
// TakeAction is how service manager handles the action.
func (s *Store) TakeAction(action *Action) {
if s.services == nil {
func (m *Manager) TakeAction(action *Action) {
if m.services == nil {
utils.GetLogInstance().Error("Service store is not initialized.")
return
}
if service, ok := s.services[action.serviceType]; ok {
if service, ok := m.services[action.serviceType]; ok {
switch action.action {
case Start:
fmt.Printf("Start %s\n", action.serviceType)
@ -121,13 +121,13 @@ func (s *Store) TakeAction(action *Action) {
}
// StartServiceManager starts service manager.
func (s *Store) StartServiceManager() chan *Action {
func (m *Manager) StartServiceManager() chan *Action {
ch := make(chan *Action)
go func() {
for {
select {
case action := <-ch:
s.TakeAction(action)
m.TakeAction(action)
if action.serviceType == Done {
return
}

@ -18,16 +18,16 @@ func (s *SupportSyncingTest) StopService() {
// Test TakeAction.
func TestTakeAction(t *testing.T) {
store := &Store{}
store.SetupServiceManager()
store.RegisterService(SupportSyncing, &SupportSyncingTest{})
m := &Manager{}
m.SetupServiceManager()
m.RegisterService(SupportSyncing, &SupportSyncingTest{})
for i := 0; i < 2; i++ {
select {
case <-time.After(WaitForStatusUpdate):
store.SendAction(&Action{action: Start, serviceType: SupportSyncing})
m.SendAction(&Action{action: Start, serviceType: SupportSyncing})
}
}
store.SendAction(&Action{serviceType: Done})
m.SendAction(&Action{serviceType: Done})
}
Loading…
Cancel
Save