Skip to content

Commit 82c4b08

Browse files
author
Ashish Khanal
committed
Everything works now!
1 parent 3d334f7 commit 82c4b08

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Consumer/Domain/HeartRateExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class HeartRateExtensions
1010

1111
public static HeartRateZone GetHeartRateZone(this HeartRate heartRate, int maxHeartRate)
1212
{
13-
var percentage = heartRate.Value / maxHeartRate;
13+
var percentage = (double)heartRate.Value / maxHeartRate; // Integer division, if heartRate.Value is less than maxHeartRate, it'll result in 0. So just cast either of those to have a floating point division.
1414
var zone = HeartRateZone.None;
1515

1616
if (percentage >= Zone5Threshold) zone = HeartRateZone.Zone5;

Consumer/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454

5555
builder.Services.AddHostedService<HeartRateZoneWorker>();
5656

57+
// A host is an object that encapsulates an app's resources and lifetime functionality, such as DI, Logging, Configuration, App shutdown, IHostedService implementations etc.
58+
// The host and console app are not the same thing - the host is just a part that runs inside your console application.
5759
using IHost host = builder.Build();
60+
5861
// https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#create-logs-in-main
5962
var logger = host.Services.GetRequiredService<ILogger<Program>>();
6063
logger.LogInformation("Host created. Now running it.");

Consumer/Workers/HeartRateZoneWorker.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
1919
producer.InitTransactions(_defaultTimeout);
2020
consumer.Subscribe(BiometricsImportedTopicName);
2121

22+
// Methods that take a CancellationToken as an argument periodically check the token to see if they should cancel their operation and return.
2223
while (!stoppingToken.IsCancellationRequested)
2324
{
25+
// If there are no more messages to process, the Consume method will not return null or exit immediately;
26+
// instead, it will continue to wait for new messages (blocking behavior).
27+
// While the Consume method does block the current thread it's running on (waiting for new messages),
28+
// it does not block other threads in your application or make the entire application slow.
29+
// Other background services or tasks will continue to run normally in separate threads.
2430
var result = consumer.Consume(stoppingToken);
2531
logger.LogInformation("Message Received: {0}", result.Message.Value);
2632
await HandleMessage(result.Message.Value, stoppingToken);
@@ -39,6 +45,7 @@ private async Task HandleMessage(Biometrics metrics, CancellationToken stoppingT
3945
consumer.Position(topicPartition))); // consumer.Position gets the current position (offset) for the specified topic / partition.
4046

4147
producer.BeginTransaction();
48+
// Send the current offsets (the position in the topic/partition it has consumed up to) to the transaction.
4249
producer.SendOffsetsToTransaction(offsets, consumer.ConsumerGroupMetadata, _defaultTimeout);
4350

4451
try

0 commit comments

Comments
 (0)