package long_poller_go import ( "context" "google.golang.org/grpc" "loafle.com/overflow/cron_go" "log" "sync" pb "loafle.com/overflow/crawler_go/grpc" //temp ) var ( POLLING_ID = "OVERFLOW_LONG_POLLING" DEFAULT_INTERVAL = uint64(3) API_SERVER_ADDR = "127.0.0.1:50052" ) 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() cr.AddTask(POLLING_ID, DEFAULT_INTERVAL).Invoke(p.polling, agentIdentifier()) } func (p *LongPoller) Stop() { p.runStat <- false p.wg.Done() } 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) } p.dispatchNotify(out) } func (p *LongPoller) dispatchNotify(result interface{}) { log.Printf("Polling result - %s ", result) } func agentIdentifier() string { //todo return "agent000001" }