From c300dd6d49f9842a8dd73dc8c5bafed53fca8ba3 Mon Sep 17 00:00:00 2001 From: snoop Date: Thu, 11 May 2017 18:19:06 +0900 Subject: [PATCH] added event --- crawler_manager.go | 26 ++++++++----- crawler_manager_event.go | 80 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 15 deletions(-) diff --git a/crawler_manager.go b/crawler_manager.go index 71804a5..72a9e83 100644 --- a/crawler_manager.go +++ b/crawler_manager.go @@ -89,15 +89,19 @@ func (c *CrawlerManager)GetClient(container string) (*grpc.ClientConn, error) { -func (c *CrawlerManager)init() { +func (c *CrawlerManager)init() error { c.checkPid() - cmap := c.ConfigMgr.GetCrawlers() + cmap := c.ConfigMgr.GetSensors() scm := sortContainer(cmap) + for ctn := range scm { - c.runAndInitContainer(&ctn, scm[ctn]) + err := c.runAndInitContainer(&ctn, scm[ctn]) + if err != nil { + return err + } } } @@ -154,7 +158,7 @@ func (c *CrawlerManager)runAndInitContainerOne(container *string) error { return err } - cmap := c.ConfigMgr.GetCrawlers() + cmap := c.ConfigMgr.GetSensors() scm := sortContainer(cmap) b := callInitConfig(container, scm[*container]) @@ -246,26 +250,28 @@ func (c *CrawlerManager)runContainer(container *string) error { return nil } -func (c *CrawlerManager)addSensor(id string) { +func (c *CrawlerManager)addSensor(id string) error { - conf := c.ConfigMgr.GetCrawlerById(id) + conf := c.ConfigMgr.GetSensorById(id) b := c.checkAndRunContainer(&conf.Crawler.Name) if b == false { - return + return errors.New("run container error") } b = callAdd(&conf.Crawler.Container, conf) - if b { - //FIXME :: Noti ADD_SENSOR_2_END + if b == false { + return errors.New("Call Add Fail") } + + return nil } func (c *CrawlerManager)removeSensor(id string) { - conf := c.ConfigMgr.GetCrawlerById(id) + conf := c.ConfigMgr.GetSensorById(id) callRemove(&conf.Crawler.Name, conf) diff --git a/crawler_manager_event.go b/crawler_manager_event.go index 8fad400..0e4a21a 100644 --- a/crawler_manager_event.go +++ b/crawler_manager_event.go @@ -12,15 +12,19 @@ import ( func listenEvent() { go listenConfigLoaded(); + go listenAgentStop() + go listenAddSensor(); go listenRemoveSensor(); + go listenUpdateSensor(); + go listenUpdateCrawler() } func listenConfigLoaded() { ch := make(chan interface{}, 0) - observer.Add(messages.CONFIGMANAGER_LOADED, ch) + observer.Add(messages.CFG_LOADED, ch) o := <-ch @@ -30,15 +34,36 @@ func listenConfigLoaded() { SettingPath() - GetInstance().init(); + err := GetInstance().init(); + if err != nil { + //FIXME:: noti err + return; + } + observer.Notify(messages.CRM_READY, nil) + +} + +func listenAgentStop() { + + ch := make(chan interface{}, 0) + observer.Add(messages.CLT_STOPPED, ch) + + o := <-ch + + fmt.Println(o) + + GetInstance().stopContainerAll() + + observer.Notify(messages.CRM_STOPPED, nil) + } func listenAddSensor() { ch := make(chan interface{}, 0) - observer.Add(messages.ADD_SENSOR_1, ch); + observer.Add(messages.SCF_SENSOR_ADD_DONE, ch); o := <-ch @@ -46,14 +71,21 @@ func listenAddSensor() { fmt.Println(str) - GetInstance().addSensor(str) + err := GetInstance().addSensor(str) + + if err != nil { + //FIXME:: noti err + return + } + + observer.Notify(messages.CRM_SENSOR_ADD_DONE, nil) } func listenRemoveSensor() { ch := make(chan interface{}, 0) - //observer.Add(messages.REMOVE_SENSOR_2_END, ch); + observer.Add(messages.CLT_SENSOR_REMOVE_DONE, ch); o := <-ch @@ -61,5 +93,43 @@ func listenRemoveSensor() { GetInstance().removeSensor(str) + + observer.Notify(messages.CRM_SENSOR_REMOVE_DONE, nil) + +} + +func listenUpdateSensor() { + + ch := make(chan interface{}, 0) + observer.Add(messages.SCF_SENSOR_UPDATE_DONE, ch); + + o := <-ch + + container := o.(string) + + err := GetInstance().runAndInitContainerOne(&container) + + if err != nil { + //FIXME:: noti err + return + } + + observer.Notify(messages.CRM_SENSOR_UPDATE_DONE, nil) + +} + +func listenUpdateCrawler() { + + ch := make(chan interface{}, 0) + observer.Add(messages.CLT_CRM_UPDATE_DONE, ch); + + o := <-ch + + fmt.Println(o) + //FIXME::update crawler + + + observer.Notify(messages.CRM_UPDATE_DONE, nil) + }