collector

This commit is contained in:
insanity@loafle.com 2017-04-17 17:20:21 +09:00
parent 6b0730f636
commit bb731efab2
4 changed files with 50 additions and 19 deletions

View File

@ -68,10 +68,25 @@ func (c *Collector) collect(id string) {
//log.Println(out) //log.Println(out)
} }
func (c *Collector) AddSensor(path string) { //func (c *Collector) AddSensor(path string) {
config := c.readConfig(CONFIG_ROOT + path) // config := c.readConfig(CONFIG_ROOT + path)
if config != nil { // if config != nil {
if err := c.addSensor(config); err != 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) log.Println(err)
} }
} }
@ -83,8 +98,7 @@ func (c *Collector) RemoveSensor(id string) {
} }
} }
func (c *Collector) UpdateSensor(path string) { func (c *Collector) UpdateSensor(newConf *conf.Config) {
newConf := c.readConfig(CONFIG_ROOT + path)
if newConf != nil { if newConf != nil {
if !c.checkExist(newConf.Id) { if !c.checkExist(newConf.Id) {
log.Println("Cannot update Sensor : ID not exist [" + 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] exConf := c.configs[newConf.Id]
c.configs[newConf.Id] = newConf
if exConf.Schedule.Interval != newConf.Schedule.Interval { if exConf.Schedule.Interval != newConf.Schedule.Interval {
log.Println("???????????????")
c.scheduler.UpdateSchedule(newConf.Id, newConf.Schedule.Interval) c.scheduler.UpdateSchedule(newConf.Id, newConf.Schedule.Interval)
} }
c.configs[newConf.Id] = newConf
} }
} }

View File

@ -1,6 +1,10 @@
package collector_go package collector_go
import ( import (
"encoding/json"
"fmt"
"io/ioutil"
conf "loafle.com/overflow/crawler_go/config"
"log" "log"
"testing" "testing"
"time" "time"
@ -11,11 +15,24 @@ func TestCallGet(t *testing.T) {
c.Start() c.Start()
time.Sleep(time.Second * 10) time.Sleep(time.Second * 10)
log.Println("add sensor") //log.Println("add sensor")
c.AddSensor("/network/smb/t2.conf") //c.AddSensor("/network/smb/t2.conf")
time.Sleep(time.Second * 3) //time.Sleep(time.Second * 3)
log.Println("update sonsor") log.Println("update sensor")
c.UpdateSensor("/network/smb/t1.conf") c.UpdateSensor(newConf())
time.Sleep(time.Second * 20)
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
} }

View File

@ -71,9 +71,9 @@ func (t *Task) addNextAt() {
if t.period == 0 { if t.period == 0 {
t.period = time.Duration(t.intervalSec) t.period = time.Duration(t.intervalSec)
t.nextAt = t.lastAt.Add(3 * time.Second) t.nextAt = t.lastAt.Add(1 * time.Second)
} else { } 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 { func (c *Cron) start() chan bool {
stopped := make(chan bool, 1) stopped := make(chan bool, 1)
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(time.Second * 1)
go func() { go func() {
for { for {

View File

@ -44,7 +44,7 @@ func (s *Scheduler) UpdateSchedule(id string, interval string) {
if err != nil { if err != nil {
i = DEFAULT_INTERVAL 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 { 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 { if err != nil {
i = DEFAULT_INTERVAL 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) { func (s *Scheduler) requestGet(id string) {