90 lines
1.6 KiB
Go
90 lines
1.6 KiB
Go
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
|
|
)
|
|
|
|
const (
|
|
POLLING_ID = "OVERFLOW_LONG_POLLING"
|
|
DEFAULT_INTERVAL = uint64(3)
|
|
CENTRAL_ADDR = "127.0.0.1:50052"
|
|
)
|
|
|
|
type LongPoller struct {
|
|
once sync.Once
|
|
runStat chan bool
|
|
}
|
|
|
|
func (p *LongPoller) Start() {
|
|
p.once.Do(func() {
|
|
p.startPolling()
|
|
})
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (p *LongPoller) polling(agentId string) {
|
|
|
|
conn, err := grpc.Dial(CENTRAL_ADDR, grpc.WithInsecure())
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
|
|
//todo temporary
|
|
client := pb.NewStatusClient(conn)
|
|
|
|
//printStatus(client) //stream
|
|
|
|
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"
|
|
}
|
|
|
|
/*
|
|
func printStassssssssstus(client pb.StatusClient) {
|
|
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("--------------------------")
|
|
}
|
|
*/
|