Skip to content

Commit

Permalink
ConfigObjectUtility#CreateObject(): Use Defer for config path cle…
Browse files Browse the repository at this point in the history
…anup
  • Loading branch information
yhabteab committed Mar 11, 2024
1 parent a6170cf commit 3d3ad2b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/remote/configobjectutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "config/configitem.hpp"
#include "base/atomic-file.hpp"
#include "base/configwriter.hpp"
#include "base/defer.hpp"
#include "base/exception.hpp"
#include "base/dependencygraph.hpp"
#include "base/tlsutility.hpp"
Expand Down Expand Up @@ -204,6 +205,14 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full

AtomicFile::Write(path, 0644, config);

bool removePath = true;
Defer removeConfigPath([&removePath, &path]{
if (removePath) {
// Something went wrong so remove the object's config accordingly.
Utility::Remove(path);
}
});

std::unique_ptr<Expression> expr = ConfigCompiler::CompileFile(path, String(), "_api");

try {
Expand All @@ -227,8 +236,6 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
Log(LogNotice, "ConfigObjectUtility")
<< "Failed to commit config item '" << fullName << "'. Aborting and removing config path '" << path << "'.";

Utility::Remove(path);

for (const boost::exception_ptr& ex : upq.GetExceptions()) {
errors->Add(DiagnosticInformation(ex, false));

Expand All @@ -250,8 +257,6 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
Log(LogNotice, "ConfigObjectUtility")
<< "Failed to activate config object '" << fullName << "'. Aborting and removing config path '" << path << "'.";

Utility::Remove(path);

for (const boost::exception_ptr& ex : upq.GetExceptions()) {
errors->Add(DiagnosticInformation(ex, false));

Expand All @@ -275,16 +280,16 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
ConfigObject::Ptr obj = ctype->GetObject(fullName);

if (obj) {
// Object is successfully created and activated, so don't remove its config.
removePath = false;

Log(LogInformation, "ConfigObjectUtility")
<< "Created and activated object '" << fullName << "' of type '" << type->GetName() << "'.";
} else {
Log(LogNotice, "ConfigObjectUtility")
<< "Object '" << fullName << "' was not created but ignored due to errors.";
}

} catch (const std::exception& ex) {
Utility::Remove(path);

if (errors)
errors->Add(DiagnosticInformation(ex, false));

Expand Down

0 comments on commit 3d3ad2b

Please sign in to comment.