ms-tester/internal/tester/delivery/rabbitmq/pubsub.go

108 lines
1.8 KiB
Go
Raw Normal View History

2024-11-01 18:22:43 +00:00
package rabbitmq
import (
"context"
"fmt"
"git.sch9.ru/new_gate/ms-tester/internal/tester"
runnerv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/runner/v1"
"github.com/golang/protobuf/proto"
amqp "github.com/rabbitmq/amqp091-go"
)
func NewTesterProducer(ch *amqp.Channel, tQueueName string, rQueueName string, testerUC tester.TesterUseCase) {
_, err := ch.QueueDeclare(
tQueueName,
true,
false,
false,
false,
nil,
)
if err != nil {
panic(err)
}
go func() {
for d := range testerUC.TestingChannel() {
for i := 0; i < 15; i++ {
msg := runnerv1.Instruction{
Instruction: &runnerv1.Instruction_Run{
Run: &runnerv1.Run{
SolutionId: d,
TestId: 0,
BindingKey: rQueueName,
},
}}
body, err := proto.Marshal(&msg)
if err != nil {
panic(err)
}
err = ch.Publish(
"",
tQueueName,
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: body,
},
)
if err != nil {
panic(err)
}
}
}
}()
return
}
func NewTesterConsumer(ch *amqp.Channel, rQueueName string, instanceName string, testerUC tester.TesterUseCase) {
_, err := ch.QueueDeclare(
rQueueName,
true,
false,
false,
false,
nil,
)
if err != nil {
panic(err)
}
msgs, err := ch.Consume(
rQueueName,
"",
false,
true, // each tester must have exclusive results queue
false,
false,
nil,
)
if err != nil {
panic(err)
}
go func() {
for d := range msgs {
err = d.Ack(false)
if err != nil {
panic(err)
}
msg := runnerv1.Result{}
err = proto.Unmarshal(d.Body, &msg)
if err != nil {
panic(err)
}
fmt.Println(msg.String())
err = testerUC.ProcessResult(context.Background(), msg.GetRun().SolutionId, msg.GetRun().String())
if err != nil {
fmt.Println(err)
}
}
}()
}