This commit is contained in:
parent
77753da0d3
commit
1ad6367784
1 changed files with 20 additions and 11 deletions
|
|
@ -38,8 +38,6 @@ func (s *service) ProcessTasks(ctx context.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Second * 5) //wait for connect
|
|
||||||
|
|
||||||
if err = s.sendTasks(fetchTasks); err != nil {
|
if err = s.sendTasks(fetchTasks); err != nil {
|
||||||
log.WithError(err).Errorf("%v Failed to send tasks", pkgLogHeader)
|
log.WithError(err).Errorf("%v Failed to send tasks", pkgLogHeader)
|
||||||
return err
|
return err
|
||||||
|
|
@ -49,13 +47,20 @@ func (s *service) ProcessTasks(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) SendResults(ctx context.Context, chanLen uint) error {
|
func (s *service) SendResults(ctx context.Context, chanLen uint) error {
|
||||||
|
log.Debugf("%v Results sender start", pkgLogHeader)
|
||||||
runCtx, cancel := context.WithCancel(ctx)
|
runCtx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
resultsConsumer := rabbit.NewConsumer(rabbit.NewClient(s.brokerAddr, "tasks-results"))
|
consumerClient, err := rabbit.NewClient(s.brokerAddr, "tasks-results")
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
resultsConsumer := rabbit.NewConsumer(consumerClient)
|
||||||
resultChan := resultsConsumer.Start(runCtx, chanLen)
|
resultChan := resultsConsumer.Start(runCtx, chanLen)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
defer cancel()
|
||||||
sendTicker := time.NewTicker(2 * time.Second)
|
sendTicker := time.NewTicker(2 * time.Second)
|
||||||
defer sendTicker.Stop()
|
defer sendTicker.Stop()
|
||||||
|
|
||||||
|
|
@ -70,13 +75,12 @@ func (s *service) SendResults(ctx context.Context, chanLen uint) error {
|
||||||
if r == nil {
|
if r == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
sendResults = append(sendResults, *r)
|
sendResults = append(sendResults, *r)
|
||||||
|
|
||||||
case <-sendTicker.C:
|
case <-sendTicker.C:
|
||||||
l := len(sendResults)
|
l := len(sendResults)
|
||||||
if l > 0 {
|
if l > 0 {
|
||||||
log.Printf("%v Sending results: %v", pkgLogHeader, sendResults)
|
log.Debugf("%v Sending results: %v", pkgLogHeader, sendResults)
|
||||||
|
|
||||||
if err := s.taskAgent.SendResults(runCtx, sendResults); err != nil {
|
if err := s.taskAgent.SendResults(runCtx, sendResults); err != nil {
|
||||||
log.WithError(err).Errorf("%v Failed to send results", pkgLogHeader)
|
log.WithError(err).Errorf("%v Failed to send results", pkgLogHeader)
|
||||||
|
|
@ -84,7 +88,6 @@ func (s *service) SendResults(ctx context.Context, chanLen uint) error {
|
||||||
|
|
||||||
sendResults = sendResults[:0]
|
sendResults = sendResults[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
@ -112,14 +115,14 @@ func (s *service) sendTasks(tasks []structs.Task) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) convertResult(b []byte) *structs.Result {
|
func (s *service) convertResult(b []byte) *structs.Result {
|
||||||
var res *structs.Result
|
var res structs.Result
|
||||||
|
|
||||||
if err := json.Unmarshal(b, res); err != nil {
|
if err := json.Unmarshal(b, &res); err != nil {
|
||||||
log.WithError(err).Error("Failed to unmarshal result")
|
log.WithError(err).Error("Failed to unmarshal result")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return &res
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeTaskPublishers(ctx context.Context, addr string, chanLen uint) map[string]chan<- []byte {
|
func makeTaskPublishers(ctx context.Context, addr string, chanLen uint) map[string]chan<- []byte {
|
||||||
|
|
@ -133,7 +136,13 @@ func makeTaskPublishers(ctx context.Context, addr string, chanLen uint) map[stri
|
||||||
|
|
||||||
for _, origin := range origins {
|
for _, origin := range origins {
|
||||||
qn := fmt.Sprintf("task-publisher-%s", origin)
|
qn := fmt.Sprintf("task-publisher-%s", origin)
|
||||||
publishers[origin] = rabbit.NewPublisher(rabbit.NewClient(addr, qn)).Start(ctx, chanLen)
|
pubClient, err := rabbit.NewClient(addr, qn)
|
||||||
|
if err != nil {
|
||||||
|
log.WithError(err).Error("Failed to create publisher")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
publishers[origin] = rabbit.NewPublisher(pubClient).Start(ctx, chanLen)
|
||||||
log.Debugf("%v Publisher queue created: %v", pkgLogHeader, qn)
|
log.Debugf("%v Publisher queue created: %v", pkgLogHeader, qn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue