ConfigManager implements

This commit is contained in:
jackdaw@loafle.com 2017-04-28 11:55:57 +09:00
parent 7c70d2ca1f
commit 19afde84eb
3 changed files with 24 additions and 14 deletions

View File

@ -11,7 +11,6 @@ import (
"os" "os"
"strings" "strings"
"sync" "sync"
"fmt"
) )
type configManager struct { type configManager struct {
@ -36,8 +35,10 @@ func init() {
agentStartHandler() agentStartHandler()
addSensorHandler() addSensorHandler()
removeSensorHandler() removeSensorHandler()
agentEndHandler()
} }
func NewConfigManager() *configManager { func NewConfigManager() *configManager {
c := &configManager{ c := &configManager{
configs: make(map[string]*config_manager.Config, 0), configs: make(map[string]*config_manager.Config, 0),
@ -184,6 +185,8 @@ func (c *configManager) removeConfig() {
} }
} }
// implements methods // implements methods
func (c *configManager) GetGlobalConfig() *config_manager.GlobalConfig { func (c *configManager) GetGlobalConfig() *config_manager.GlobalConfig {
return &c.globalConfig return &c.globalConfig

View File

@ -1,3 +1,5 @@
// +build !test
package config_manager_go package config_manager_go
import ( import (
@ -22,13 +24,6 @@ func TestLoadConfig(t *testing.T) {
data :=<- ch data :=<- ch
c := data.(config_manager.ConfigManager) c := data.(config_manager.ConfigManager)
cc := c.GetSensors() cc := c.GetSensors()
//ccc := cc["75045c53-a78d-4589-b8c1-4482cab720b6"]
//
//mmmm := ccc.Items[0].Keys[0].Metric
//
//fmt.Println(mmmm)
//
assert.NotEqual(t, len(cc),0) assert.NotEqual(t, len(cc),0)
}() }()
@ -44,7 +39,6 @@ func TestLoadConfig(t *testing.T) {
func TestAddConfig(t *testing.T) { func TestAddConfig(t *testing.T) {
_configManager.loadGlobalConfig("/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/global.yaml") _configManager.loadGlobalConfig("/root/gowork/src/loafle.com/overflow/config_manager_go/test_agent/global.yaml")
// 원본 테스트 설정 파일 로드 // 원본 테스트 설정 파일 로드
@ -73,6 +67,6 @@ func TestRemoveConfig(t *testing.T) {
_configManager.loadCrawlerConfigAll() _configManager.loadCrawlerConfigAll()
// remove test // remove test
observer.Notify(messages.REMOVE_SENSOR_0,"289575d2-4a34-4475-b91d-7fb0992cc3de") observer.Notify(messages.REMOVE_SENSOR_0,"d0fcc7b1-43a7-4acd-a7bf-c9572a9d4c9e")
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
} }

View File

@ -7,7 +7,7 @@ import (
func agentStartHandler() { func agentStartHandler() {
agentStart := make(chan interface{}, 0) agentStart := make(chan interface{}, 0)
observer.Add("AGENT_STARTED", agentStart) observer.Add(messages.AGENT_STARTED, agentStart)
go func() { go func() {
data := <-agentStart data := <-agentStart
path := data.(string) path := data.(string)
@ -24,12 +24,25 @@ func agentStartHandler() {
}() }()
} }
func removeSensorHandler() { func agentEndHandler() {
observer.Add(messages.REMOVE_SENSOR_0,_configManager.removeCh) agentEnd := make(chan interface{}, 0)
go _configManager.removeConfig() observer.Add(messages.AGENT_WILL_STOPPED, agentEnd)
go func() {
_ = <-agentEnd
observer.Remove(messages.AGENT_WILL_STOPPED, agentEnd)
observer.Remove(messages.ADD_SENSOR_0,_configManager.addCh)
observer.Remove(messages.REMOVE_SENSOR_0,_configManager.removeCh)
}()
} }
func addSensorHandler() { func addSensorHandler() {
observer.Add(messages.ADD_SENSOR_0,_configManager.addCh) observer.Add(messages.ADD_SENSOR_0,_configManager.addCh)
go _configManager.addConfig() go _configManager.addConfig()
} }
func removeSensorHandler() {
observer.Add(messages.REMOVE_SENSOR_0,_configManager.removeCh)
go _configManager.removeConfig()
}