long_poller_go/poller.go

139 lines
2.5 KiB
Go
Raw Normal View History

2017-04-25 06:19:00 +00:00
package long_poller_go
import (
2017-04-28 04:16:39 +00:00
cm "loafle.com/overflow/agent_api/config_manager"
2017-05-16 07:11:50 +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-05-15 10:05:54 +00:00
POLLING_ID = "OVERFLOW_LONG_POLLING"
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-15 05:50:14 +00:00
runStat chan bool
gconf *cm.GlobalConfig
scheduler *s.Scheduler
taskCh chan interface{}
2017-04-25 06:19:00 +00:00
}
2017-05-16 09:04:04 +00:00
func Start(ch chan bool, conf *cm.GlobalConfig) error {
2017-05-15 05:48:08 +00:00
p := GetInstance()
2017-05-16 07:11:50 +00:00
p.gconf = conf
p.startPolling()
ch <- true
return nil
2017-04-28 04:16:39 +00:00
}
2017-05-16 07:11:50 +00:00
func GetTaskCh() chan interface{} {
p := GetInstance()
p.taskCh = make(chan interface{})
return p.taskCh
2017-05-15 10:05:54 +00:00
}
func Stop(polStopped chan bool) error {
GetInstance().stop()
polStopped <- true
return nil
}
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-15 10:05:54 +00:00
func UpdateInterval(interval string) error {
v, err := strconv.Atoi(interval)
if err != nil {
return err
}
GetInstance().updateInterval(v)
2017-05-15 05:48:08 +00:00
return nil
2017-04-28 04:16:39 +00:00
}
2017-05-16 09:04:04 +00:00
func (p *Poller) startPolling() {
2017-05-16 07:11:50 +00:00
2017-04-28 04:16:39 +00:00
go func() {
2017-05-16 07:11:50 +00:00
p.scheduler = &s.Scheduler{}
p.scheduler.Start()
p.scheduler.NewSchedule(POLLING_ID, uint64(p.gconf.IntervalSecond), p.polling)
2017-04-28 04:16:39 +00:00
}()
}
2017-05-16 07:11:50 +00:00
//func (p *Poller) handleInterval(ch chan interface{}) {
// go func() {
// data := <-ch
// interval := data.(string)
// i, err := strconv.Atoi(interval)
// if err != nil {
// log.Println(err)
// return
// }
// p.scheduler.UpdateInterval(POLLING_ID, uint64(i))
// }()
//}
2017-05-15 10:05:54 +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
}
2017-04-28 04:16:39 +00:00
func (p *Poller) polling(agentId string) {
2017-04-25 07:34:03 +00:00
2017-05-16 07:11:50 +00:00
go func() {
log.Println("POLLING....")
/*
2017-05-16 09:04:04 +00:00
addr := p.gconf.Central.Address + ":" + string(p.gconf.Central.Port)
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
log.Println(err)
return
}
defer conn.Close()
//todo temporary
client := pb.NewStatusClient(conn)
//printStatusStream(client) //stream
out, err := client.Status(context.Background(), &pb.Empty{})
if err != nil {
log.Println(err)
}
p.taskCh <- out
2017-05-16 07:11:50 +00:00
*/
}()
2017-04-25 07:34:03 +00:00
}
2017-05-15 10:05:54 +00:00
func (p *Poller) updateInterval(interval int) {
p.scheduler.UpdateInterval(POLLING_ID, uint64(interval))
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("--------------------------")
}
*/