Skip to content

Commit 9fe1b0c

Browse files
(#2) Call OnReload only if there is changed data
1 parent 7ad1ec9 commit 9fe1b0c

File tree

6 files changed

+40
-7
lines changed

6 files changed

+40
-7
lines changed

src/Example/Example.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.0" />
1616
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
17+
<PackageReference Include="Microsoft.FeatureManagement" Version="2.4.0" />
1718
<PackageReference Include="Stravaig.Configuration.Diagnostics.Logging" Version="1.0.4" />
1819
</ItemGroup>
1920

src/Example/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using Example;
1+
using Example;
42
using Microsoft.Extensions.Configuration;
53
using Microsoft.Extensions.DependencyInjection;
64
using Microsoft.Extensions.Hosting;
75
using Microsoft.Extensions.Logging;
6+
using Microsoft.FeatureManagement;
87
using Stravaig.Configuration.SqlServer;
98

109
await Host.CreateDefaultBuilder(args)
@@ -27,5 +26,6 @@ await Host.CreateDefaultBuilder(args)
2726
services.AddTransient<IHostedService, TheHostedService>();
2827
services.AddSingleton(configRoot);
2928
services.Configure<MyFeatureConfiguration>(configRoot.GetSection("MyConfiguration"));
29+
services.AddFeatureManagement(configRoot.GetSection("FeatureManager"));
3030
})
3131
.RunConsoleAsync();

src/Example/TheHostedService.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Extensions.Hosting;
88
using Microsoft.Extensions.Logging;
99
using Microsoft.Extensions.Options;
10+
using Microsoft.FeatureManagement;
1011
using Stravaig.Configuration.Diagnostics.Logging;
1112
using Timer = System.Timers.Timer;
1213

@@ -17,26 +18,29 @@ public class TheHostedService : IHostedService, IDisposable
1718
{
1819
private readonly ILogger<TheHostedService> _logger;
1920
private readonly IConfigurationRoot _configRoot;
21+
private readonly IFeatureManager _featureManager;
2022
private readonly IOptionsMonitor<MyFeatureConfiguration> _featureValues;
2123
private readonly Timer _timer;
2224

2325
public TheHostedService(
2426
ILogger<TheHostedService> logger,
2527
IConfigurationRoot configRoot,
28+
IFeatureManager featureManager,
2629
IOptionsMonitor<MyFeatureConfiguration> featureValues)
2730
{
2831
_logger = logger;
2932
_configRoot = configRoot;
33+
_featureManager = featureManager;
3034
_featureValues = featureValues;
3135
_timer = new Timer(10000);
3236
_timer.Elapsed += TimerOnElapsed;
3337
featureValues.OnChange((v, s) =>
3438
{
35-
Console.WriteLine($"Change detected. {s}");
39+
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Change Detected. {s}");
3640
});
3741
}
3842

39-
private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
43+
private async void TimerOnElapsed(object? sender, ElapsedEventArgs e)
4044
{
4145
var jsonOptions = new JsonSerializerOptions()
4246
{
@@ -48,6 +52,11 @@ private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
4852
"At {Time} the object looks like:\n{Json}",
4953
e.SignalTime,
5054
json);
55+
await foreach (string feature in _featureManager.GetFeatureNamesAsync())
56+
{
57+
var state = await _featureManager.IsEnabledAsync(feature);
58+
_logger.LogInformation("{Feature} : {State}", feature, state);
59+
}
5160
_logger.LogConfigurationValuesAsInformation(_configRoot.GetSection("MyConfiguration"));
5261
}
5362

src/Example/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"ConnectionString": "*** Found in User Secrets ***"
88
}
99
},
10+
"FeatureManager": {
11+
"FeatureA": true,
12+
"FeatureB": {}
13+
},
1014
"MyConfiguration": {
1115
"SomeNumber": 123,
1216
"SomeString": "A value from appsettings",

src/Stravaig.Configuration.SqlServer/SqlServerConfigurationProvider.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,27 @@ public override void Load()
3636

3737
internal void Reload()
3838
{
39+
// Get existing data
40+
var oldData = Data;
3941
Load();
40-
OnReload();
42+
43+
if (DataDifferent(oldData, Data))
44+
OnReload();
45+
}
46+
47+
private bool DataDifferent(IDictionary<string, string> oldData, IDictionary<string, string> newData)
48+
{
49+
if (oldData.Count != newData.Count)
50+
return true;
51+
foreach (var oldKey in oldData.Keys)
52+
{
53+
if (!newData.ContainsKey(oldKey))
54+
return true;
55+
if (oldData[oldKey] != newData[oldKey])
56+
return true;
57+
}
58+
59+
return false;
4160
}
4261

4362
public override string ToString()

src/Stravaig.Configuration.SqlServer/SqlServerConfigurationWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
1919
{
2020
try
2121
{
22-
Console.WriteLine("Timer elapsed");
22+
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Polling the database.");
2323
_provider.Reload();
2424
}
2525
catch (Exception ex)

0 commit comments

Comments
 (0)