long poller

This commit is contained in:
insanity@loafle.com 2017-04-25 16:34:03 +09:00
parent 448e2a97b9
commit c2b8b106ab

View File

@ -2,13 +2,20 @@ package long_poller_go
import ( import (
"fmt" "fmt"
"google.golang.org/grpc"
"loafle.com/overflow/cron_go" "loafle.com/overflow/cron_go"
"sync" "sync"
"context"
pb "loafle.com/overflow/crawler_go/grpc" //temp
"log"
"time"
) )
var ( var (
POLLING_ID = "OVERFLOW_LONG_POLLING" POLLING_ID = "OVERFLOW_LONG_POLLING"
DEFAULT_INTERVAL = uint64(3) DEFAULT_INTERVAL = uint64(5)
API_SERVER_ADDR = "127.0.0.1:50052"
) )
type LongPoller struct { type LongPoller struct {
@ -28,7 +35,7 @@ func (p *LongPoller) Start() {
func (p *LongPoller) startPolling() { func (p *LongPoller) startPolling() {
cr := &cron.Cron{} cr := &cron.Cron{}
p.runStat = cr.Start() p.runStat = cr.Start()
cr.AddTask(POLLING_ID, DEFAULT_INTERVAL).Invoke(p.poll, "agent0001") cr.AddTask(POLLING_ID, DEFAULT_INTERVAL).Invoke(p.polling, agentIdentifier())
} }
func (p *LongPoller) Stop() { func (p *LongPoller) Stop() {
@ -36,6 +43,26 @@ func (p *LongPoller) Stop() {
p.wg.Done() p.wg.Done()
} }
func (p *LongPoller) poll(agentId string) { func (p *LongPoller) polling(agentId string) {
fmt.Printf("%s :: LongPolling\n", agentId) fmt.Printf("[%s] %s :: LongPolling\n", time.Now(), agentId)
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)
}
log.Println(out)
}
func agentIdentifier() string {
//todo
return "agent000001"
} }