Reload config on change (#527)

* Reload config on change.
Added test case.

* added testing for adding the json with reloadOnChange = false
This commit is contained in:
Marcelo Castagna
2018-08-04 04:17:31 -03:00
committed by Tom Pallister
parent 89f0cc786a
commit ad8025df1b
3 changed files with 122 additions and 7 deletions

View File

@ -113,12 +113,12 @@
{
// make configuration from file system?
// earlier user needed to add ocelot files in startup configuration stuff, asp.net will map it to this
var fileConfig = builder.ApplicationServices.GetService<IOptions<FileConfiguration>>();
var fileConfig = builder.ApplicationServices.GetService<IOptionsMonitor<FileConfiguration>>();
// now create the config
var internalConfigCreator = builder.ApplicationServices.GetService<IInternalConfigurationCreator>();
var internalConfig = await internalConfigCreator.Create(fileConfig.Value);
//Configuration error, throw error message
var internalConfig = await internalConfigCreator.Create(fileConfig.CurrentValue);
//Configuration error, throw error message
if (internalConfig.IsError)
{
ThrowToStopOcelotStarting(internalConfig);
@ -128,6 +128,12 @@
var internalConfigRepo = builder.ApplicationServices.GetService<IInternalConfigurationRepository>();
internalConfigRepo.AddOrReplace(internalConfig.Data);
fileConfig.OnChange(async (config) =>
{
var newInternalConfig = await internalConfigCreator.Create(config);
internalConfigRepo.AddOrReplace(newInternalConfig.Data);
});
var fileConfigRepo = builder.ApplicationServices.GetService<IFileConfigurationRepository>();
var adminPath = builder.ApplicationServices.GetService<IAdministrationPath>();
@ -155,7 +161,7 @@
}
private static async Task SetFileConfigInConsul(IApplicationBuilder builder,
IFileConfigurationRepository fileConfigRepo, IOptions<FileConfiguration> fileConfig,
IFileConfigurationRepository fileConfigRepo, IOptionsMonitor<FileConfiguration> fileConfig,
IInternalConfigurationCreator internalConfigCreator, IInternalConfigurationRepository internalConfigRepo)
{
// get the config from consul.
@ -168,7 +174,7 @@
else if (ConfigNotStoredInConsul(fileConfigFromConsul))
{
//there was no config in consul set the file in config in consul
await fileConfigRepo.Set(fileConfig.Value);
await fileConfigRepo.Set(fileConfig.CurrentValue);
}
else
{
@ -197,9 +203,9 @@
}
}
private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, IOptions<FileConfiguration> fileConfig)
private static async Task SetFileConfig(IFileConfigurationSetter fileConfigSetter, IOptionsMonitor<FileConfiguration> fileConfig)
{
var response = await fileConfigSetter.Set(fileConfig.Value);
var response = await fileConfigSetter.Set(fileConfig.CurrentValue);
if (IsError(response))
{