From bb731efab2b570715d0e7544f06d97f96357d2e5 Mon Sep 17 00:00:00 2001 From: "insanity@loafle.com" Date: Mon, 17 Apr 2017 17:20:21 +0900 Subject: [PATCH] collector --- collector.go | 30 ++++++++++++++++++++++-------- collector_test.go | 29 +++++++++++++++++++++++------ scheduler/cron/cron.go | 6 +++--- scheduler/scheduler.go | 4 ++-- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/collector.go b/collector.go index 4b67c78..9391e3e 100644 --- a/collector.go +++ b/collector.go @@ -68,10 +68,25 @@ func (c *Collector) collect(id string) { //log.Println(out) } -func (c *Collector) AddSensor(path string) { - config := c.readConfig(CONFIG_ROOT + path) - if config != nil { - if err := c.addSensor(config); err != nil { +//func (c *Collector) AddSensor(path string) { +// config := c.readConfig(CONFIG_ROOT + path) +// if config != nil { +// if err := c.addSensor(config); err != nil { +// log.Println(err) +// } +// } +//} + +func (c *Collector) AddSensor(conf *conf.Config) { + + if c.checkExist(conf.Id) { + log.Println("he Same Id already exists.") + return + } + c.configs[conf.Id] = conf + + if conf != nil { + if err := c.addSensor(conf); err != nil { log.Println(err) } } @@ -83,8 +98,7 @@ func (c *Collector) RemoveSensor(id string) { } } -func (c *Collector) UpdateSensor(path string) { - newConf := c.readConfig(CONFIG_ROOT + path) +func (c *Collector) UpdateSensor(newConf *conf.Config) { if newConf != nil { if !c.checkExist(newConf.Id) { log.Println("Cannot update Sensor : ID not exist [" + newConf.Id + "]") @@ -92,12 +106,12 @@ func (c *Collector) UpdateSensor(path string) { } exConf := c.configs[newConf.Id] - c.configs[newConf.Id] = newConf if exConf.Schedule.Interval != newConf.Schedule.Interval { - log.Println("???????????????") c.scheduler.UpdateSchedule(newConf.Id, newConf.Schedule.Interval) } + + c.configs[newConf.Id] = newConf } } diff --git a/collector_test.go b/collector_test.go index d688ded..2a49d86 100644 --- a/collector_test.go +++ b/collector_test.go @@ -1,6 +1,10 @@ package collector_go import ( + "encoding/json" + "fmt" + "io/ioutil" + conf "loafle.com/overflow/crawler_go/config" "log" "testing" "time" @@ -11,11 +15,24 @@ func TestCallGet(t *testing.T) { c.Start() time.Sleep(time.Second * 10) - log.Println("add sensor") - c.AddSensor("/network/smb/t2.conf") - time.Sleep(time.Second * 3) + //log.Println("add sensor") + //c.AddSensor("/network/smb/t2.conf") + //time.Sleep(time.Second * 3) - log.Println("update sonsor") - c.UpdateSensor("/network/smb/t1.conf") - time.Sleep(time.Second * 20) + log.Println("update sensor") + c.UpdateSensor(newConf()) + + time.Sleep(time.Second * 30) +} + +func newConf() *conf.Config { + bytes, err := ioutil.ReadFile("/config/container/network/smb/smb2.conf") + if err != nil { + fmt.Println(err) + return nil + } + conf := conf.Config{} + json.Unmarshal(bytes, &conf) + + return &conf } diff --git a/scheduler/cron/cron.go b/scheduler/cron/cron.go index c1725d3..dd1736d 100644 --- a/scheduler/cron/cron.go +++ b/scheduler/cron/cron.go @@ -71,9 +71,9 @@ func (t *Task) addNextAt() { if t.period == 0 { t.period = time.Duration(t.intervalSec) - t.nextAt = t.lastAt.Add(3 * time.Second) + t.nextAt = t.lastAt.Add(1 * time.Second) } else { - t.nextAt = t.lastAt.Add(t.period * time.Second) + t.nextAt = t.lastAt.Add((t.period * time.Second) - time.Second) } } @@ -177,7 +177,7 @@ func (c *Cron) removeAll() error { func (c *Cron) start() chan bool { stopped := make(chan bool, 1) - ticker := time.NewTicker(1 * time.Second) + ticker := time.NewTicker(time.Second * 1) go func() { for { diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 4bb20ca..c43ea0a 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -44,7 +44,7 @@ func (s *Scheduler) UpdateSchedule(id string, interval string) { if err != nil { i = DEFAULT_INTERVAL } - cron.UpdateTask(id, uint64(i-1)) + cron.UpdateTask(id, uint64(i)) } func (s *Scheduler) newSchedule(id string, interval string, fn interface{}) error { @@ -52,7 +52,7 @@ func (s *Scheduler) newSchedule(id string, interval string, fn interface{}) erro if err != nil { i = DEFAULT_INTERVAL } - return cron.AddTask(id, uint64(i-1)).Invoke(fn, id) + return cron.AddTask(id, uint64(i)).Invoke(fn, id) } func (s *Scheduler) requestGet(id string) {