2025-01-10 11:12:03 +00:00
|
|
|
package queue
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type PollConfig struct {
|
|
|
|
|
Config *Config
|
|
|
|
|
Controller Controller
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PollMessages(ctx context.Context, queueConfig *PollConfig) {
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
default:
|
|
|
|
|
err := PollMessage(ctx, queueConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Print(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PollMessage(ctx context.Context, queueConfig *PollConfig) error {
|
|
|
|
|
result, err := Receive(ctx, queueConfig.Config, []string{
|
|
|
|
|
"type",
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("message fetch fail: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, message := range result.Messages {
|
|
|
|
|
go func() {
|
2025-01-20 13:51:22 +00:00
|
|
|
err := queueConfig.Controller.Process(ctx, &message)
|
2025-01-10 11:12:03 +00:00
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("message process fail: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = Delete(ctx, queueConfig.Config, &message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("message delete fail: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|