From 19afde84eb24d8177a39083c1dc36aabd8032088 Mon Sep 17 00:00:00 2001 From: "jackdaw@loafle.com" Date: Fri, 28 Apr 2017 11:55:57 +0900 Subject: [PATCH] ConfigManager implements --- config_manager.go | 5 ++++- config_manager_test.go | 12 +++--------- init_method.go | 21 +++++++++++++++++---- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/config_manager.go b/config_manager.go index d4853c6..8276258 100644 --- a/config_manager.go +++ b/config_manager.go @@ -11,7 +11,6 @@ import ( "os" "strings" "sync" - "fmt" ) type configManager struct { @@ -36,8 +35,10 @@ func init() { agentStartHandler() addSensorHandler() removeSensorHandler() + agentEndHandler() } + func NewConfigManager() *configManager { c := &configManager{ configs: make(map[string]*config_manager.Config, 0), @@ -184,6 +185,8 @@ func (c *configManager) removeConfig() { } } + + // implements methods func (c *configManager) GetGlobalConfig() *config_manager.GlobalConfig { return &c.globalConfig diff --git a/config_manager_test.go b/config_manager_test.go index 7fb60e9..f432ba3 100644 --- a/config_manager_test.go +++ b/config_manager_test.go @@ -1,3 +1,5 @@ + +// +build !test package config_manager_go import ( @@ -22,13 +24,6 @@ func TestLoadConfig(t *testing.T) { data :=<- ch c := data.(config_manager.ConfigManager) 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) }() @@ -44,7 +39,6 @@ func TestLoadConfig(t *testing.T) { func TestAddConfig(t *testing.T) { - _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() // 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) } diff --git a/init_method.go b/init_method.go index 170192a..9fb8756 100644 --- a/init_method.go +++ b/init_method.go @@ -7,7 +7,7 @@ import ( func agentStartHandler() { agentStart := make(chan interface{}, 0) - observer.Add("AGENT_STARTED", agentStart) + observer.Add(messages.AGENT_STARTED, agentStart) go func() { data := <-agentStart path := data.(string) @@ -24,12 +24,25 @@ func agentStartHandler() { }() } -func removeSensorHandler() { - observer.Add(messages.REMOVE_SENSOR_0,_configManager.removeCh) - go _configManager.removeConfig() +func agentEndHandler() { + agentEnd := make(chan interface{}, 0) + 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() { observer.Add(messages.ADD_SENSOR_0,_configManager.addCh) go _configManager.addConfig() } + +func removeSensorHandler() { + observer.Add(messages.REMOVE_SENSOR_0,_configManager.removeCh) + go _configManager.removeConfig() +} + +