EnergySubscriber.UseApplicationMessageReceivedHandler(e =>
{
Task.Run(async () =>
{
Console.WriteLine("message received");
Console.WriteLine(DateTimeOffset.UtcNow.ToString());
var message = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
await ProcessEnergyMQTTMessage(message);
});
});
Above given code snippet, working perfectly in vs debug mode, processing every message coming completely from the publisher. But when I run the same code in K8s pods, it received the message but process the message partially. Every time it behaves differently (i mean, it processes the message 30%, 40%, 60%, 90%), a few time it completed the process, save it into DB.
But if I removed the task.run as code given below so it starts to process the message completely but it shows lag in telemetry.
EnergySubscriber.UseApplicationMessageReceivedHandler(e =>
{
Console.WriteLine("message received");
Console.WriteLine(DateTimeOffset.UtcNow.ToString());
var message = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
ProcessEnergyMQTTMessage(message).Wait();
});
Please help to run this code with task.run
Thanks
Which version of the library are you using? What does ProcessEnergyMQTTMessage do in detail? Writing data to a database?
Yes, ProcessEnergyMQTTMessage performing logic and saving different entities in DB, before that we were practicing Azure IoT hub, operating flawlessly. I am using the latest version of MQTTnet '3.0.7'.
i would go without Task.Run as it does not provide any benifit.
than you should add some telemetry collector like application insights and find out what is taking time
like serializing, db access, etc.
if you find that it is mqtt.net that is eating up cpu try MqttClientConnectionContextFactory to create a client. (that uses kestrels network stack to less byte[] copy)
you could also try, so you have the await ie nonblocking but dont do an additional thread change
EnergySubscriber.UseApplicationMessageReceivedHandler(async e =>
{
try {
Console.WriteLine("message received");
Console.WriteLine(DateTimeOffset.UtcNow.ToString());
var message = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
await ProcessEnergyMQTTMessage(message);
catch {}
});
@bilalmalik777 Does this solve your issue?
@SeppPenner I find out the reason, actually DB connection is not getting free, so I am working on optimizing long-running queries. hopefully, it would resolve the problem. If the issue remains the same, I will contact you. But right now I think it is not an MQTT problem. Thanks
Ok, I will close this issue for now.