refactor service function

pull/375/head
Minh Doan 6 years ago committed by Minh Doan
parent 65f3b5191f
commit 400da7b775
  1. 36
      api/service/service.go

@ -74,41 +74,41 @@ type Store struct {
} }
// Register new service to service store. // Register new service to service store.
func (ss *Store) Register(t Type, service Interface) { func (s *Store) Register(t Type, service Interface) {
if ss.services == nil { if s.services == nil {
ss.services = make(map[Type]Interface) s.services = make(map[Type]Interface)
} }
ss.services[t] = service s.services[t] = service
} }
// SetupServiceManager inits service map and start service manager. // SetupServiceManager inits service map and start service manager.
func (ss *Store) SetupServiceManager() { func (s *Store) SetupServiceManager() {
ss.InitServiceMap() s.InitServiceMap()
ss.actionChannel = ss.StartServiceManager() s.actionChannel = s.StartServiceManager()
} }
// RegisterService is used for testing. // RegisterService is used for testing.
func (ss *Store) RegisterService(t Type, service Interface) { func (s *Store) RegisterService(t Type, service Interface) {
ss.Register(t, service) s.Register(t, service)
} }
// InitServiceMap initializes service map. // InitServiceMap initializes service map.
func (ss *Store) InitServiceMap() { func (s *Store) InitServiceMap() {
ss.services = make(map[Type]Interface) s.services = make(map[Type]Interface)
} }
// SendAction sends action to action channel which is observed by service manager. // SendAction sends action to action channel which is observed by service manager.
func (ss *Store) SendAction(action *Action) { func (s *Store) SendAction(action *Action) {
ss.actionChannel <- action s.actionChannel <- action
} }
// TakeAction is how service manager handles the action. // TakeAction is how service manager handles the action.
func (ss *Store) TakeAction(action *Action) { func (s *Store) TakeAction(action *Action) {
if ss.services == nil { if s.services == nil {
utils.GetLogInstance().Error("Service store is not initialized.") utils.GetLogInstance().Error("Service store is not initialized.")
return return
} }
if service, ok := ss.services[action.serviceType]; ok { if service, ok := s.services[action.serviceType]; ok {
switch action.action { switch action.action {
case Start: case Start:
fmt.Printf("Start %s\n", action.serviceType) fmt.Printf("Start %s\n", action.serviceType)
@ -121,13 +121,13 @@ func (ss *Store) TakeAction(action *Action) {
} }
// StartServiceManager starts service manager. // StartServiceManager starts service manager.
func (ss *Store) StartServiceManager() chan *Action { func (s *Store) StartServiceManager() chan *Action {
ch := make(chan *Action) ch := make(chan *Action)
go func() { go func() {
for { for {
select { select {
case action := <-ch: case action := <-ch:
ss.TakeAction(action) s.TakeAction(action)
if action.serviceType == Done { if action.serviceType == Done {
return return
} }

Loading…
Cancel
Save