Skip to content

Commit abfe19e

Browse files
authored
Merge pull request #13 from Trendyol/feat/issue-12-detect-kafka-changes
Feat/issue 12 detect kafka changes
2 parents f730103 + 4969825 commit abfe19e

File tree

9 files changed

+38
-21
lines changed

9 files changed

+38
-21
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v1.4.0 (December 17, 2024)
2+
3+
### Added:
4+
- `ReloadOnChange` flag to Kafka configuration. This allows you to change the configuration without restarting the application.
5+
16
## v1.3.0 (October 26, 2024)
27

38
### Changed:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ set:
119119
120120
| **Key** | **Type** | **Description** |
121121
|----------------------------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
122+
| `Kafka.ReloadOnChange` | bool | The flag indicating whether the Kafka configuration should be reloaded when the configuration file changes. |
122123
| `Kafka.SaslUsername` | string | The username for the SASL authentication of the Kafka cluster. |
123124
| `Kafka.Brokers` | string | The addresses of the Kafka brokers. |
124125
| `Kafka.SaslPassword` | string | The password for the SASL authentication of the Kafka cluster. |

example/Couchbase/config/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
]
2020
},
2121
"Kafka": {
22+
"reloadOnChange": true,
2223
"SaslUsername": "",
2324
"Brokers": "",
2425
"SslCaLocation": "",

example/MsSql/config/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
]
2020
},
2121
"Kafka": {
22+
"reloadOnChange": true,
2223
"SaslUsername": "",
2324
"Brokers": "",
2425
"SslCaLocation": "",

example/Postgres/config/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
]
2020
},
2121
"Kafka": {
22+
"reloadOnChange": true,
2223
"SaslUsername": "",
2324
"Brokers": "",
2425
"SslCaLocation": "",

src/ConfigOptions/Kafka.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ public class Kafka
1919
public Acks? Acks { get; set; }
2020
public double? LingerMs { get; set; }
2121
public string ClientId { get; set; }
22+
public bool ReloadOnChange { get; set; }
2223
}

src/Coordinators/Services/KafkaProducer.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics.CodeAnalysis;
33
using System.Threading.Tasks;
44
using Confluent.Kafka;
5+
using Microsoft.Extensions.Logging;
56
using Microsoft.Extensions.Options;
67
using NewRelic.Api.Agent;
78
using PollingOutboxPublisher.ConfigOptions;
@@ -13,13 +14,26 @@ namespace PollingOutboxPublisher.Coordinators.Services;
1314
public sealed class KafkaProducer : IKafkaProducer
1415
{
1516
private IProducer<string, string> _producer;
17+
private Kafka _kafkaConfiguration;
18+
private readonly ILogger<KafkaProducer> _logger;
1619

17-
public KafkaProducer(IOptions<Kafka> kafkaOptions)
20+
public KafkaProducer(IOptionsMonitor<Kafka> kafkaOptionsMonitor, ILogger<KafkaProducer> logger)
1821
{
19-
var kafkaConfiguration = kafkaOptions.Value;
20-
BuildProducer(kafkaConfiguration);
22+
_logger = logger;
23+
_kafkaConfiguration = kafkaOptionsMonitor.CurrentValue;
24+
kafkaOptionsMonitor.OnChange(OnKafkaConfigChanged);
25+
BuildProducer(_kafkaConfiguration);
2126
}
2227

28+
private void OnKafkaConfigChanged(Kafka newKafkaConfig)
29+
{
30+
if (!newKafkaConfig.ReloadOnChange) return;
31+
32+
_logger.LogInformation("Kafka configuration changed");
33+
_kafkaConfiguration = newKafkaConfig;
34+
BuildProducer(_kafkaConfiguration);
35+
}
36+
2337
private void BuildProducer(Kafka kafka)
2438
{
2539
var config = new ProducerConfig
@@ -38,6 +52,12 @@ private void BuildProducer(Kafka kafka)
3852
MessageMaxBytes = kafka.MessageMaxBytes ?? 30000000,
3953
Acks = kafka.Acks ?? Acks.Leader
4054
};
55+
56+
if (_producer != null)
57+
{
58+
_producer.Flush(TimeSpan.FromSeconds(10));
59+
_producer.Dispose();
60+
}
4161
_producer = new ProducerBuilder<string, string>(config).Build();
4262
}
4363

src/PollingOutboxPublisher.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
66
<IsPackable>false</IsPackable>
7-
<Version>1.3.0</Version>
7+
<Version>1.4.0</Version>
88
<RootNamespace>PollingOutboxPublisher</RootNamespace>
99
</PropertyGroup>
1010

src/Program.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,20 @@ public static class Program
3131
{
3232
public static async Task Main(string[] args)
3333
{
34-
CheckConfigFileExistence();
3534
var host = CreateHostBuilder(args).Build();
36-
3735
//ExampleRunner.RunPostgresExample(host);
38-
3936
await host.RunAsync();
4037
}
41-
42-
private static void CheckConfigFileExistence()
43-
{
44-
var isConfigExist = File.Exists(Directory.GetCurrentDirectory() + "/config/config.json");
45-
if (!isConfigExist)
46-
throw new FileNotFoundException("File not found", "config.json");
47-
48-
var isSecretExist = File.Exists(Directory.GetCurrentDirectory() + "/config/secret.json");
49-
if (!isSecretExist)
50-
throw new FileNotFoundException("File not found", "secret.json");
51-
}
52-
38+
5339
private static IHostBuilder CreateHostBuilder(string[] args)
5440
{
5541
return Host.CreateDefaultBuilder(args)
5642
.ConfigureAppConfiguration(
5743
(_, builder) =>
5844
{
59-
builder.AddJsonFile("config/config.json", true, true)
60-
.AddJsonFile("config/secret.json", true, true)
45+
builder
46+
.AddJsonFile("config/config.json", false, true)
47+
.AddJsonFile("config/secret.json", false, true)
6148
.Build();
6249
})
6350
.ConfigureServices((hostContext, services) =>

0 commit comments

Comments
 (0)