collector
This commit is contained in:
parent
6b0730f636
commit
bb731efab2
30
collector.go
30
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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user