Introduce & use enum `DowntimeRemovalReason`

This commit is contained in:
Yonas Habteab 2024-05-16 11:55:43 +02:00
parent cc3965c3ce
commit e0fd0d3df4
5 changed files with 34 additions and 21 deletions

View File

@ -557,7 +557,7 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
childCount += downtime->GetChildren().size();
try {
Downtime::RemoveDowntime(downtime->GetName(), true, true, false, author);
Downtime::RemoveDowntime(downtime->GetName(), true, DowntimeRemovedByUser, author);
} catch (const invalid_downtime_removal_error& error) {
Log(LogWarning, "ApiActions") << error.what();
@ -578,7 +578,7 @@ Dictionary::Ptr ApiActions::RemoveDowntime(const ConfigObject::Ptr& object,
try {
String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, true, true, false, author);
Downtime::RemoveDowntime(downtimeName, true, DowntimeRemovedByUser, author);
return ApiActions::CreateResult(200, "Successfully removed downtime '" + downtimeName +
"' and " + std::to_string(childCount) + " child downtimes.");

View File

@ -359,7 +359,7 @@ Downtime::Ptr Downtime::AddDowntime(const Checkable::Ptr& checkable, const Strin
return downtime;
}
void Downtime::RemoveDowntime(const String& id, bool includeChildren, bool cancelled, bool expired,
void Downtime::RemoveDowntime(const String& id, bool includeChildren, DowntimeRemovalReason removalReason,
const String& removedBy, const MessageOrigin::Ptr& origin)
{
Downtime::Ptr downtime = Downtime::GetByName(id);
@ -369,18 +369,18 @@ void Downtime::RemoveDowntime(const String& id, bool includeChildren, bool cance
String config_owner = downtime->GetConfigOwner();
if (!config_owner.IsEmpty() && !expired) {
if (!config_owner.IsEmpty() && removalReason != DowntimeExpired) {
BOOST_THROW_EXCEPTION(invalid_downtime_removal_error("Cannot remove downtime '" + downtime->GetName() +
"'. It is owned by scheduled downtime object '" + config_owner + "'"));
}
if (includeChildren) {
for (const Downtime::Ptr& child : downtime->GetChildren()) {
Downtime::RemoveDowntime(child->GetName(), true, true);
Downtime::RemoveDowntime(child->GetName(), true, removalReason);
}
}
if (cancelled) {
if (removalReason != DowntimeExpired) {
downtime->SetRemovalInfo(removedBy, Utility::GetTime());
}
@ -396,13 +396,19 @@ void Downtime::RemoveDowntime(const String& id, bool includeChildren, bool cance
}
String reason;
if (expired) {
reason = "expired at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", downtime->GetEndTime());
} else if (cancelled) {
reason = "cancelled by user";
} else {
reason = "<unknown>";
switch (removalReason) {
case DowntimeExpired:
reason = "expired at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", downtime->GetEndTime());
break;
case DowntimeRemovedByUser:
reason = "cancelled by user";
if (!removedBy.IsEmpty()) {
reason += " '" + removedBy + "'";
}
break;
case DowntimeRemovedByConfigOwner:
reason = "cancelled by '" + config_owner + "' of type 'ScheduledDowntime'";
break;
}
Log msg (LogInformation, "Downtime");
@ -465,7 +471,7 @@ void Downtime::SetupCleanupTimer()
auto downtime (Downtime::GetByName(name));
if (downtime && downtime->IsExpired()) {
RemoveDowntime(name, false, false, true);
RemoveDowntime(name, false, DowntimeExpired);
}
});
}
@ -557,7 +563,7 @@ void Downtime::DowntimesOrphanedTimerHandler()
for (const Downtime::Ptr& downtime : ConfigType::GetObjectsByType<Downtime>()) {
/* Only remove downtimes which are activated after daemon start. */
if (downtime->IsActive() && !downtime->HasValidConfigOwner())
RemoveDowntime(downtime->GetName(), false, false, true);
RemoveDowntime(downtime->GetName(), false, DowntimeRemovedByConfigOwner);
}
}

View File

@ -18,6 +18,13 @@ enum DowntimeChildOptions
DowntimeNonTriggeredChildren
};
enum DowntimeRemovalReason
{
DowntimeExpired,
DowntimeRemovedByUser,
DowntimeRemovedByConfigOwner,
};
/**
* A downtime.
*
@ -52,7 +59,7 @@ public:
const String& scheduledBy = String(), const String& parent = String(), const String& id = String(),
const MessageOrigin::Ptr& origin = nullptr);
static void RemoveDowntime(const String& id, bool includeChildren, bool cancelled, bool expired = false,
static void RemoveDowntime(const String& id, bool includeChildren, DowntimeRemovalReason removalReason,
const String& removedBy = "", const MessageOrigin::Ptr& origin = nullptr);
void RegisterChild(const Downtime::Ptr& downtime);

View File

@ -993,7 +993,7 @@ void ExternalCommandProcessor::DelSvcDowntime(double, const std::vector<String>&
}
try {
Downtime::RemoveDowntime(rid, false, true);
Downtime::RemoveDowntime(rid, false, DowntimeRemovedByUser);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime ID " << arguments[0];
@ -1112,7 +1112,7 @@ void ExternalCommandProcessor::DelHostDowntime(double, const std::vector<String>
}
try {
Downtime::RemoveDowntime(rid, false, true);
Downtime::RemoveDowntime(rid, false, DowntimeRemovedByUser);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime ID " << arguments[0];
@ -1147,7 +1147,7 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<S
for (const Downtime::Ptr& downtime : host->GetDowntimes()) {
try {
String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, false, true);
Downtime::RemoveDowntime(downtimeName, false, DowntimeRemovedByUser);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime '" << downtimeName << "'.";
@ -1169,7 +1169,7 @@ void ExternalCommandProcessor::DelDowntimeByHostName(double, const std::vector<S
try {
String downtimeName = downtime->GetName();
Downtime::RemoveDowntime(downtimeName, false, true);
Downtime::RemoveDowntime(downtimeName, false, DowntimeRemovedByUser);
Log(LogNotice, "ExternalCommandProcessor")
<< "Removed downtime '" << downtimeName << "'.";

View File

@ -323,7 +323,7 @@ void ScheduledDowntime::RemoveObsoleteDowntimes()
auto configOwnerHash (downtime->GetConfigOwnerHash());
if (!configOwnerHash.IsEmpty() && configOwnerHash != downtimeOptionsHash)
Downtime::RemoveDowntime(downtime->GetName(), false, true);
Downtime::RemoveDowntime(downtime->GetName(), false, DowntimeRemovedByConfigOwner);
}
}
}