Hi Folks!
I am facing a problem in one of my projects. I have created a small service whose job is to calculate the total time taken based on the data pushed into RabbitMQ. The structure of the message is quite simple {JobId uint, Status bool} . I want to calculate and save the total time taken to process a specific Job. For this, I'm pushing 2 instances of the given struct into the queue with Status being **true **when the process is **initiated **and **false **when completed.
Following is the current logic:
func Listener() {
conn, err := amqp.Dial(os.Getenv("RABBITMQ"))
logError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
logError(err, "Failed to open a channel")
defer ch.Close()
msgs := createChannel(ch)
go func() {
for d := range msgs {
var data StatusQueue
err := json.Unmarshal(d.Body, &data)
logError(err, "failed to unmarshal queue data")
var timer time.Time
if data.Status {
timer = time.Now() // Setting the initial value
} else {
totalTime := time.Since(timer)
content := fmt.Sprintf("Total time taken for jobId: %d : %f", data.JobId, totalTime.Seconds())
log.Println(content)
}
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
select {}
}```
Following is the output for the above:
"Total time taken for jobId: 1 : `9223372036.854776`"
The expected output should not be more than a few seconds. I am assuming this has something to do with concurrency. I'm only familiar with waitGroups and Mutexs till now. Thank you for your time.