Core: a reload reads the config directory again
LoadAdditionalDir kept the names of the files worldserver.conf.d held at startup, and Reload read only those. A file added to the directory later was never read by `.reload config` (it needed a restart), and a file removed from it made every later reload report an error. The directory itself is now kept, in its place in the load order, and every reload lists it again in name order: a new file is read, a removed one is dropped. Files added one by one with keepOnReload are kept as before. The order stays worldserver.conf, base files, additional files and directories in the order they were added, environment.
This commit is contained in:
@@ -35,8 +35,45 @@ namespace fs = boost::filesystem;
|
||||
namespace
|
||||
{
|
||||
std::string _filename;
|
||||
std::vector<std::string> _additonalFiles;
|
||||
std::vector<std::string> _baseFiles; // LoadBaseFile: applied after the main file, before _additonalFiles
|
||||
// Additional files and directories kept on reload, in load order. A directory (worldserver.conf.d) is listed again on every
|
||||
// reload, so a file added to it after startup is read and a removed one is dropped; keeping only the file names it held at
|
||||
// startup made a new conf.d file invisible until a restart.
|
||||
struct ReloadEntry
|
||||
{
|
||||
std::string path;
|
||||
bool isDir;
|
||||
};
|
||||
std::vector<ReloadEntry> _additionalEntries;
|
||||
std::vector<std::string> _baseFiles; // LoadBaseFile: applied after the main file, before _additionalEntries
|
||||
|
||||
// The .conf files of a directory and its subdirectories, in name order, so a later file (zz-9-...) always overrides an
|
||||
// earlier one. The order of the directory listing itself depends on the file system and is not alphabetical everywhere.
|
||||
std::vector<std::string> ListConfigFiles(std::string const& dir)
|
||||
{
|
||||
std::vector<std::string> files;
|
||||
fs::path dirPath = dir;
|
||||
if (!fs::exists(dirPath) || !fs::is_directory(dirPath))
|
||||
return files;
|
||||
|
||||
std::vector<fs::path> configFiles;
|
||||
for (fs::directory_entry const& f : fs::recursive_directory_iterator(dirPath))
|
||||
{
|
||||
if (!fs::is_regular_file(f))
|
||||
continue;
|
||||
|
||||
fs::path configFile = fs::absolute(f);
|
||||
if (configFile.extension() != ".conf")
|
||||
continue;
|
||||
|
||||
configFiles.push_back(std::move(configFile));
|
||||
}
|
||||
std::sort(configFiles.begin(), configFiles.end());
|
||||
|
||||
files.reserve(configFiles.size());
|
||||
for (fs::path const& configFile : configFiles)
|
||||
files.push_back(configFile.generic_string());
|
||||
return files;
|
||||
}
|
||||
std::vector<std::string> _args;
|
||||
bpt::ptree _config;
|
||||
std::mutex _configLock;
|
||||
@@ -169,7 +206,7 @@ bool ConfigMgr::LoadAdditionalFile(std::string file, bool keepOnReload, std::str
|
||||
_config.put_child(bpt::ptree::path_type(child.first, '/'), child.second);
|
||||
|
||||
if (keepOnReload)
|
||||
_additonalFiles.emplace_back(std::move(file));
|
||||
_additionalEntries.push_back({ std::move(file), false });
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -203,31 +240,17 @@ bool Trinity::Config::LoadBaseFile(std::string file, std::string& error)
|
||||
|
||||
bool ConfigMgr::LoadAdditionalDir(std::string const& dir, bool keepOnReload, std::vector<std::string>& loadedFiles, std::vector<std::string>& errors)
|
||||
{
|
||||
fs::path dirPath = dir;
|
||||
if (!fs::exists(dirPath) || !fs::is_directory(dirPath))
|
||||
return true;
|
||||
|
||||
// Load in name order, so a later file (zz-9-...) always overrides an earlier one. The order of the directory listing
|
||||
// itself depends on the file system and is not alphabetical everywhere.
|
||||
std::vector<fs::path> configFiles;
|
||||
for (fs::directory_entry const& f : fs::recursive_directory_iterator(dirPath))
|
||||
// the directory itself is kept, not its files: Reload lists it again (see _additionalEntries)
|
||||
if (keepOnReload)
|
||||
{
|
||||
if (!fs::is_regular_file(f))
|
||||
continue;
|
||||
|
||||
fs::path configFile = fs::absolute(f);
|
||||
if (configFile.extension() != ".conf")
|
||||
continue;
|
||||
|
||||
configFiles.push_back(std::move(configFile));
|
||||
std::scoped_lock lock(_configLock);
|
||||
_additionalEntries.push_back({ dir, true });
|
||||
}
|
||||
std::sort(configFiles.begin(), configFiles.end());
|
||||
|
||||
for (fs::path const& configFile : configFiles)
|
||||
for (std::string& fileName : ListConfigFiles(dir))
|
||||
{
|
||||
std::string fileName = configFile.generic_string();
|
||||
std::string error;
|
||||
if (LoadAdditionalFile(fileName, keepOnReload, error))
|
||||
if (LoadAdditionalFile(fileName, false, error))
|
||||
loadedFiles.push_back(std::move(fileName));
|
||||
else
|
||||
errors.push_back(std::move(error));
|
||||
@@ -275,9 +298,19 @@ bool ConfigMgr::Reload(std::vector<std::string>& errors)
|
||||
if (!LoadAdditionalFile(baseFile, false, error))
|
||||
errors.push_back(std::move(error));
|
||||
|
||||
for (std::string const& additionalFile : _additonalFiles)
|
||||
if (!LoadAdditionalFile(additionalFile, false, error))
|
||||
errors.push_back(std::move(error));
|
||||
for (ReloadEntry const& entry : _additionalEntries)
|
||||
{
|
||||
if (!entry.isDir)
|
||||
{
|
||||
if (!LoadAdditionalFile(entry.path, false, error))
|
||||
errors.push_back(std::move(error));
|
||||
continue;
|
||||
}
|
||||
|
||||
for (std::string const& additionalFile : ListConfigFiles(entry.path))
|
||||
if (!LoadAdditionalFile(additionalFile, false, error))
|
||||
errors.push_back(std::move(error));
|
||||
}
|
||||
|
||||
OverrideWithEnvVariablesIfAny();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user