• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ThreeMammals / Ocelot / 23205793029

17 Mar 2026 04:47PM UTC coverage: 92.432% (+0.02%) from 92.416%
23205793029

Pull #2367

github

web-flow
Merge 59bcb6f4e into 02aaa7691
Pull Request #2367: Bump all NuGet packages to the latest versions for .NET SDK 10.0.201 (aka .NET Runtime 10.0.5)

6412 of 6937 relevant lines covered (92.43%)

2093.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.58
src/Ocelot/Configuration/Repository/FileConfigurationPoller.cs
1
using Microsoft.Extensions.Hosting;
2
using Newtonsoft.Json;
3
using Ocelot.Configuration.Creator;
4
using Ocelot.Configuration.File;
5
using Ocelot.Logging;
6

7
namespace Ocelot.Configuration.Repository;
8

9
public class FileConfigurationPoller : IHostedService, IDisposable
10
{
11
    private readonly IOcelotLogger _logger;
12
    private readonly IFileConfigurationRepository _repo;
13
    private string _previousAsJson;
14
    private Timer _timer;
15
    private volatile bool _polling;
16
    private readonly IFileConfigurationPollerOptions _options;
17
    private readonly IInternalConfigurationRepository _internalConfigRepo;
18
    private readonly IInternalConfigurationCreator _internalConfigCreator;
19

20
    public FileConfigurationPoller(
6✔
21
        IOcelotLoggerFactory factory,
6✔
22
        IFileConfigurationRepository repo,
6✔
23
        IFileConfigurationPollerOptions options,
6✔
24
        IInternalConfigurationRepository internalConfigRepo,
6✔
25
        IInternalConfigurationCreator internalConfigCreator)
6✔
26
    {
27
        _internalConfigRepo = internalConfigRepo;
6✔
28
        _internalConfigCreator = internalConfigCreator;
6✔
29
        _options = options;
6✔
30
        _logger = factory.CreateLogger<FileConfigurationPoller>();
6✔
31
        _repo = repo;
6✔
32
        _previousAsJson = string.Empty;
6✔
33
    }
6✔
34

35
    private void OnTimer(object state)
36
    {
37
        if (_polling)
4✔
38
            return;
×
39

40
        _polling = true;
4✔
41
        PollAsync().GetAwaiter().GetResult(); // TODO This is not good, TimerCallback must be synchronous
4✔
42
        _polling = false;
4✔
43
    }
4✔
44

45
    public Task StartAsync(CancellationToken cancellationToken)
46
    {
47
        if (_timer is not null)
5✔
48
            return Task.CompletedTask;
×
49

50
        _logger.LogInformation($"{nameof(FileConfigurationPoller)} is starting.");
5✔
51
        _timer = new(OnTimer, null, _options.Delay, _options.Delay); // TODO state could be CancellationToken?
5✔
52
        return Task.CompletedTask;
5✔
53
    }
54

55
    public Task StopAsync(CancellationToken cancellationToken)
56
    {
57
        if (_timer is null)
1✔
58
            return Task.CompletedTask;
×
59

60
        _logger.LogInformation($"{nameof(FileConfigurationPoller)} is stopping.");
1✔
61
        _timer.Change(Timeout.Infinite, 0);
1✔
62
        return Task.CompletedTask;
1✔
63
    }
64

65
    private async Task PollAsync()
66
    {
67
        _logger.LogInformation(() => $"{nameof(PollAsync)}: Started polling");
4✔
68

69
        var fileConfig = await _repo.Get();
4✔
70
        if (fileConfig.IsError)
4✔
71
        {
72
            _logger.LogWarning(() => $"{nameof(PollAsync)}: Error getting file config, errors are {string.Join(',', fileConfig.Errors.Select(x => x.Message))}");
×
73
            return;
×
74
        }
75

76
        var asJson = ToJson(fileConfig.Data);
4✔
77
        if (asJson != _previousAsJson)
4✔
78
        {
79
            var config = await _internalConfigCreator.Create(fileConfig.Data);
3✔
80
            if (!config.IsError)
3✔
81
                _internalConfigRepo.AddOrReplace(config.Data);
3✔
82

83
            _previousAsJson = asJson;
3✔
84
        }
85

86
        _logger.LogInformation(() => $"{nameof(PollAsync)}: Finished polling");
4✔
87
    }
4✔
88

89
    /// <summary>
90
    /// We could do object comparison here but performance isnt really a problem. This might be an issue one day!.
91
    /// </summary>
92
    /// <returns>hash of the config.</returns>
93
    private static string ToJson(FileConfiguration config)
94
    {
95
        var currentHash = JsonConvert.SerializeObject(config);
4✔
96
        return currentHash;
4✔
97
    }
98

99
    public void Dispose()
100
    {
101
        _timer?.Dispose();
7✔
102
        _timer = null;
7✔
103
        GC.SuppressFinalize(this);
7✔
104
    }
7✔
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc