Remove -and notify- expired downtimes immediately, not every 60s

Don't look for expired downtimes in a timer fired every 60s,
but fire one timer per downtime once at expire time.
This commit is contained in:
Alexander A. Klimov 2023-04-03 15:39:50 +02:00
parent ba7102cae3
commit 6adf2d19e4
2 changed files with 51 additions and 1 deletions

View File

@ -10,6 +10,7 @@
#include "base/timer.hpp"
#include <boost/thread/once.hpp>
#include <cmath>
#include <utility>
using namespace icinga;
@ -159,6 +160,21 @@ void Downtime::Stop(bool runtimeRemoved)
ObjectImpl<Downtime>::Stop(runtimeRemoved);
}
void Downtime::Pause()
{
if (m_CleanupTimer) {
m_CleanupTimer->Stop();
}
ObjectImpl<Downtime>::Pause();
}
void Downtime::Resume()
{
ObjectImpl<Downtime>::Resume();
SetupCleanupTimer();
}
Checkable::Ptr Downtime::GetCheckable() const
{
return static_pointer_cast<Checkable>(m_Checkable);
@ -427,6 +443,28 @@ bool Downtime::CanBeTriggered()
return true;
}
void Downtime::SetupCleanupTimer()
{
if (!m_CleanupTimer) {
m_CleanupTimer = Timer::Create();
auto name (GetName());
m_CleanupTimer->OnTimerExpired.connect([name=std::move(name)](const Timer * const&) {
auto downtime (Downtime::GetByName(name));
if (downtime && downtime->IsExpired()) {
RemoveDowntime(name, false, false, true);
}
});
}
auto triggerTime (GetTriggerTime());
m_CleanupTimer->Reschedule((GetFixed() || triggerTime <= 0 ? GetEndTime() : triggerTime + GetDuration()) + 0.1);
m_CleanupTimer->Start();
}
void Downtime::TriggerDowntime(double triggerTime)
{
if (!CanBeTriggered())
@ -441,6 +479,11 @@ void Downtime::TriggerDowntime(double triggerTime)
SetTriggerTime(triggerTime);
}
{
ObjectLock olock (this);
SetupCleanupTimer();
}
Array::Ptr triggers = GetTriggers();
{
@ -501,7 +544,7 @@ void Downtime::DowntimesExpireTimerHandler()
{
for (const Downtime::Ptr& downtime : ConfigType::GetObjectsByType<Downtime>()) {
/* Only remove downtimes which are activated after daemon start. */
if (downtime->IsActive() && (downtime->IsExpired() || !downtime->HasValidConfigOwner()))
if (downtime->IsActive() && !downtime->HasValidConfigOwner())
RemoveDowntime(downtime->GetName(), false, false, true);
}
}

View File

@ -72,6 +72,9 @@ protected:
void Start(bool runtimeCreated) override;
void Stop(bool runtimeRemoved) override;
void Pause() override;
void Resume() override;
void ValidateStartTime(const Lazy<Timestamp>& lvalue, const ValidationUtils& utils) override;
void ValidateEndTime(const Lazy<Timestamp>& lvalue, const ValidationUtils& utils) override;
@ -81,8 +84,12 @@ private:
std::set<Downtime::Ptr> m_Children;
mutable std::mutex m_ChildrenMutex;
Timer::Ptr m_CleanupTimer;
bool CanBeTriggered();
void SetupCleanupTimer();
static void DowntimesStartTimerHandler();
static void DowntimesExpireTimerHandler();
};