`ConfigObjectUtility#CreateObject()`: Use `Defer` for config path cleanup

This commit is contained in:
Yonas Habteab 2024-03-08 10:16:33 +01:00
parent 433e2de13a
commit 099f664ce6
1 changed files with 10 additions and 7 deletions

View File

@ -7,6 +7,7 @@
#include "config/configitem.hpp" #include "config/configitem.hpp"
#include "base/atomic-file.hpp" #include "base/atomic-file.hpp"
#include "base/configwriter.hpp" #include "base/configwriter.hpp"
#include "base/defer.hpp"
#include "base/exception.hpp" #include "base/exception.hpp"
#include "base/dependencygraph.hpp" #include "base/dependencygraph.hpp"
#include "base/tlsutility.hpp" #include "base/tlsutility.hpp"
@ -204,6 +205,12 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
AtomicFile::Write(path, 0644, config); AtomicFile::Write(path, 0644, config);
// Remove the just created config file in all the error cases and if the object creation
// succeeds the deferred callback will be cancelled.
Defer removeConfigPath([&path]{
Utility::Remove(path);
});
std::unique_ptr<Expression> expr = ConfigCompiler::CompileFile(path, String(), "_api"); std::unique_ptr<Expression> expr = ConfigCompiler::CompileFile(path, String(), "_api");
try { try {
@ -227,8 +234,6 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
Log(LogNotice, "ConfigObjectUtility") Log(LogNotice, "ConfigObjectUtility")
<< "Failed to commit config item '" << fullName << "'. Aborting and removing config path '" << path << "'."; << "Failed to commit config item '" << fullName << "'. Aborting and removing config path '" << path << "'.";
Utility::Remove(path);
for (const boost::exception_ptr& ex : upq.GetExceptions()) { for (const boost::exception_ptr& ex : upq.GetExceptions()) {
errors->Add(DiagnosticInformation(ex, false)); errors->Add(DiagnosticInformation(ex, false));
@ -250,8 +255,6 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
Log(LogNotice, "ConfigObjectUtility") Log(LogNotice, "ConfigObjectUtility")
<< "Failed to activate config object '" << fullName << "'. Aborting and removing config path '" << path << "'."; << "Failed to activate config object '" << fullName << "'. Aborting and removing config path '" << path << "'.";
Utility::Remove(path);
for (const boost::exception_ptr& ex : upq.GetExceptions()) { for (const boost::exception_ptr& ex : upq.GetExceptions()) {
errors->Add(DiagnosticInformation(ex, false)); errors->Add(DiagnosticInformation(ex, false));
@ -275,16 +278,16 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
ConfigObject::Ptr obj = ctype->GetObject(fullName); ConfigObject::Ptr obj = ctype->GetObject(fullName);
if (obj) { if (obj) {
// Object is successfully created and activated, so don't remove its config.
removeConfigPath.Cancel();
Log(LogInformation, "ConfigObjectUtility") Log(LogInformation, "ConfigObjectUtility")
<< "Created and activated object '" << fullName << "' of type '" << type->GetName() << "'."; << "Created and activated object '" << fullName << "' of type '" << type->GetName() << "'.";
} else { } else {
Log(LogNotice, "ConfigObjectUtility") Log(LogNotice, "ConfigObjectUtility")
<< "Object '" << fullName << "' was not created but ignored due to errors."; << "Object '" << fullName << "' was not created but ignored due to errors.";
} }
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
Utility::Remove(path);
if (errors) if (errors)
errors->Add(DiagnosticInformation(ex, false)); errors->Add(DiagnosticInformation(ex, false));