From 44b13478ccefbc7e6fee75f6131d2a19b3e90d25 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Thu, 31 Jan 2019 08:06:39 -0800 Subject: [PATCH] refactor service store to manager --- api/service/{service.go => manager.go} | 42 +++++++++---------- .../{service_test.go => manager_test.go} | 10 ++--- 2 files changed, 26 insertions(+), 26 deletions(-) rename api/service/{service.go => manager.go} (72%) rename api/service/{service_test.go => manager_test.go} (64%) diff --git a/api/service/service.go b/api/service/manager.go similarity index 72% rename from api/service/service.go rename to api/service/manager.go index c8a12f41b..7e88f0853 100644 --- a/api/service/service.go +++ b/api/service/manager.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 } diff --git a/api/service/service_test.go b/api/service/manager_test.go similarity index 64% rename from api/service/service_test.go rename to api/service/manager_test.go index 0d85813bf..5e83accbf 100644 --- a/api/service/service_test.go +++ b/api/service/manager_test.go @@ -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}) }