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

ThreeMammals / Ocelot / 16416226117

21 Jul 2025 11:53AM UTC coverage: 87.855% (-0.1%) from 87.958%
16416226117

Pull #1659

github

web-flow
Merge 7113c26f8 into c721273e2
Pull Request #1659: #1658 Adds global header transforms

5722 of 6513 relevant lines covered (87.86%)

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

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

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

4✔
46
            _polling = true;
5✔
47
            await Poll();
5✔
48
            _polling = false;
5✔
49
        }, null, _options.Delay, _options.Delay);
9✔
50

51
        return Task.CompletedTask;
4✔
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");
5✔
66

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

69
        if (fileConfig.IsError)
5✔
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);
5✔
76

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

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

86
            _previousAsJson = asJson;
3✔
87
        }
88

89
        _logger.LogInformation("Finished polling");
5✔
90
    }
5✔
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);
5✔
99
        return currentHash;
5✔
100
    }
101

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