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

ThreeMammals / Ocelot / 20064101838

09 Dec 2025 12:51PM UTC coverage: 93.474% (-0.01%) from 93.488%
20064101838

Pull #2342

github

web-flow
Merge 57ee7d4ea into adc34eccd
Pull Request #2342: Follow up #2339: Fixing unstable tests

6517 of 6972 relevant lines covered (93.47%)

18055.54 hits per line

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

87.23
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 bool _polling;
16
    private readonly IFileConfigurationPollerOptions _options;
17
    private readonly IInternalConfigurationRepository _internalConfigRepo;
18
    private readonly IInternalConfigurationCreator _internalConfigCreator;
19

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

35
    public Task StartAsync(CancellationToken cancellationToken)
36
    {
37
        _logger.LogInformation($"{nameof(FileConfigurationPoller)} is starting.");
16✔
38

39
        _timer = new Timer(async x =>
16✔
40
        {
16✔
41
            if (_polling)
13✔
42
            {
16✔
43
                return;
×
44
            }
16✔
45

16✔
46
            _polling = true;
13✔
47
            await Poll();
13✔
48
            _polling = false;
13✔
49
        }, null, _options.Delay, _options.Delay);
29✔
50

51
        return Task.CompletedTask;
16✔
52
    }
53

54
    public Task StopAsync(CancellationToken cancellationToken)
55
    {
56
        _logger.LogInformation($"{nameof(FileConfigurationPoller)} is stopping.");
×
57

58
        _timer?.Change(Timeout.Infinite, 0);
×
59

60
        return Task.CompletedTask;
×
61
    }
62

63
    private async Task Poll()
64
    {
65
        _logger.LogInformation("Started polling");
13✔
66

67
        var fileConfig = await _repo.Get();
13✔
68

69
        if (fileConfig.IsError)
13✔
70
        {
71
            _logger.LogWarning(() => $"error geting file config, errors are {string.Join(',', fileConfig.Errors.Select(x => x.Message))}");
×
72
            return;
×
73
        }
74

75
        var asJson = ToJson(fileConfig.Data);
13✔
76

77
        if (!fileConfig.IsError && asJson != _previousAsJson)
13✔
78
        {
79
            var config = await _internalConfigCreator.Create(fileConfig.Data);
12✔
80

81
            if (!config.IsError)
12✔
82
            {
83
                _internalConfigRepo.AddOrReplace(config.Data);
12✔
84
            }
85

86
            _previousAsJson = asJson;
12✔
87
        }
88

89
        _logger.LogInformation("Finished polling");
13✔
90
    }
13✔
91

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

102
    public void Dispose()
103
    {
104
        _timer?.Dispose();
24✔
105
        _timer = null;
24✔
106
    }
24✔
107
}
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