long_poller_go/poller.go

148 lines
2.7 KiB
Go
Raw Normal View History

2017-04-25 06:19:00 +00:00
package long_poller_go
import (
2017-04-25 07:56:04 +00:00
"context"
2017-04-25 07:34:03 +00:00
"google.golang.org/grpc"
2017-04-28 04:16:39 +00:00
cm "loafle.com/overflow/agent_api/config_manager"
2017-04-25 07:34:03 +00:00
pb "loafle.com/overflow/crawler_go/grpc" //temp
2017-05-11 09:36:20 +00:00
s "loafle.com/overflow/scheduler_go"
"log"
"strconv"
"sync"
2017-04-25 06:19:00 +00:00
)
2017-04-27 01:25:43 +00:00
const (
2017-04-25 06:19:00 +00:00
POLLING_ID = "OVERFLOW_LONG_POLLING"
2017-04-25 07:56:04 +00:00
DEFAULT_INTERVAL = uint64(3)
2017-04-25 06:19:00 +00:00
)
2017-04-28 04:16:39 +00:00
var (
instance *Poller
once sync.Once
)
type Poller struct {
2017-05-11 09:36:20 +00:00
once sync.Once
runStat chan bool
gconf *cm.GlobalConfig
intervalCh chan interface{}
scheduler *s.Scheduler
2017-04-25 06:19:00 +00:00
}
2017-04-28 04:16:39 +00:00
func init() {
2017-05-11 09:36:20 +00:00
go handleConfigLoaded()
2017-04-28 04:16:39 +00:00
}
func GetInstance() *Poller {
once.Do(func() {
instance = &Poller{}
2017-04-25 06:19:00 +00:00
})
2017-04-28 04:16:39 +00:00
return instance
2017-04-25 06:19:00 +00:00
}
2017-05-11 09:36:20 +00:00
func startPoller(ch chan interface{}) {
p := GetInstance()
go func() {
data := <-ch
p.gconf = data.(cm.ConfigManager).GetGlobalConfig()
p.start()
p.addObservers()
}()
}
func (p *Poller) addObservers() {
go p.handleAgentStop()
2017-04-28 04:16:39 +00:00
}
2017-05-11 09:36:20 +00:00
func (p *Poller) handleInterval(ch chan interface{}) {
2017-04-28 04:16:39 +00:00
go func() {
data := <-ch
2017-05-11 09:36:20 +00:00
interval := data.(string)
i, err := strconv.Atoi(interval)
if err != nil {
log.Println(err)
return
}
p.scheduler.UpdateInterval(POLLING_ID, uint64(i))
2017-04-28 04:16:39 +00:00
}()
}
func (p *Poller) start() {
p.once.Do(func() {
2017-05-11 09:36:20 +00:00
p.scheduler = &s.Scheduler{}
p.scheduler.Start()
p.scheduler.NewSchedule(POLLING_ID, DEFAULT_INTERVAL, p.polling)
2017-04-28 04:16:39 +00:00
})
2017-04-25 06:19:00 +00:00
}
2017-04-28 04:16:39 +00:00
func (p *Poller) Stop() {
2017-05-11 09:36:20 +00:00
p.scheduler.Stop()
2017-04-25 06:19:00 +00:00
p.runStat <- false
}
2017-04-28 04:16:39 +00:00
func (p *Poller) polling(agentId string) {
2017-04-25 07:34:03 +00:00
2017-04-28 04:16:39 +00:00
addr := p.gconf.Central.Address + ":" + string(p.gconf.Central.Port)
conn, err := grpc.Dial(addr, grpc.WithInsecure())
2017-04-25 07:34:03 +00:00
if err != nil {
log.Println(err)
return
}
defer conn.Close()
//todo temporary
client := pb.NewStatusClient(conn)
2017-04-27 01:25:43 +00:00
2017-05-11 09:36:20 +00:00
//printStatusStream(client) //stream
2017-04-27 01:25:43 +00:00
2017-04-25 07:34:03 +00:00
out, err := client.Status(context.Background(), &pb.Empty{})
if err != nil {
log.Println(err)
}
2017-04-25 07:56:04 +00:00
p.dispatchNotify(out)
}
2017-04-28 04:16:39 +00:00
func (p *Poller) dispatchNotify(result interface{}) {
2017-04-25 07:56:04 +00:00
log.Printf("Polling result - %s ", result)
2017-05-11 09:36:20 +00:00
/*
TASK_SENSOR_START
TASK_SENSOR_STOP
TASK_SENSOR_ADD
TASK_SENSOR_REMOVE
TASK_SENSOR_UPDATE
TASK_CRAWLER_UPDATE
TASK_AGENT_UPDATE
TASK_LOG_SEND
*/
2017-04-25 07:34:03 +00:00
}
2017-05-11 09:36:20 +00:00
func (p *Poller) updateInterval(interval string) {
i, err := strconv.Atoi(interval)
if err != nil {
log.Println(err)
return
}
p.scheduler.UpdateInterval(POLLING_ID, uint64(i))
2017-04-25 06:19:00 +00:00
}
2017-04-27 01:25:43 +00:00
/*
2017-05-11 09:36:20 +00:00
func printStatusStream(client pb.StatusClient) {
2017-04-27 01:25:43 +00:00
stream, err := client.Status(context.Background(), &pb.Empty{})
if err != nil {
grpclog.Fatalf("%v.List(_) = _, %v", client, err)
}
for {
feature, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
grpclog.Fatalf("%v.List(_) = _, %v", client, err)
}
grpclog.Println(feature)
}
grpclog.Println("--------------------------")
}
*/