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-25 06:19:00 +00:00
|
|
|
"loafle.com/overflow/cron_go"
|
2017-04-25 07:56:04 +00:00
|
|
|
"log"
|
2017-04-25 06:19:00 +00:00
|
|
|
"sync"
|
2017-04-25 07:34:03 +00:00
|
|
|
|
|
|
|
pb "loafle.com/overflow/crawler_go/grpc" //temp
|
2017-04-25 06:19:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
POLLING_ID = "OVERFLOW_LONG_POLLING"
|
2017-04-25 07:56:04 +00:00
|
|
|
DEFAULT_INTERVAL = uint64(3)
|
2017-04-25 07:34:03 +00:00
|
|
|
API_SERVER_ADDR = "127.0.0.1:50052"
|
2017-04-25 06:19:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LongPoller struct {
|
|
|
|
once sync.Once
|
|
|
|
runStat chan bool
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LongPoller) Start() {
|
|
|
|
p.once.Do(func() {
|
|
|
|
p.wg.Add(1)
|
|
|
|
p.startPolling()
|
|
|
|
p.wg.Wait()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LongPoller) startPolling() {
|
|
|
|
cr := &cron.Cron{}
|
|
|
|
p.runStat = cr.Start()
|
2017-04-25 07:34:03 +00:00
|
|
|
cr.AddTask(POLLING_ID, DEFAULT_INTERVAL).Invoke(p.polling, agentIdentifier())
|
2017-04-25 06:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LongPoller) Stop() {
|
|
|
|
p.runStat <- false
|
|
|
|
p.wg.Done()
|
|
|
|
}
|
|
|
|
|
2017-04-25 07:34:03 +00:00
|
|
|
func (p *LongPoller) polling(agentId string) {
|
|
|
|
|
|
|
|
conn, err := grpc.Dial(API_SERVER_ADDR, grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
//todo temporary
|
|
|
|
client := pb.NewStatusClient(conn)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LongPoller) dispatchNotify(result interface{}) {
|
|
|
|
log.Printf("Polling result - %s ", result)
|
2017-04-25 07:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func agentIdentifier() string {
|
|
|
|
//todo
|
|
|
|
return "agent000001"
|
2017-04-25 06:19:00 +00:00
|
|
|
}
|