diff --git a/core/host_monitor/HostMonitorInputRunner.cpp b/core/host_monitor/HostMonitorInputRunner.cpp index 16fb5f4154..dcea6809ef 100755 --- a/core/host_monitor/HostMonitorInputRunner.cpp +++ b/core/host_monitor/HostMonitorInputRunner.cpp @@ -16,6 +16,8 @@ #include "HostMonitorInputRunner.h" +#include + #include #include #include @@ -100,13 +102,14 @@ bool HostMonitorInputRunner::HasRegisteredPlugins() const { return false; } -bool HostMonitorInputRunner::IsCollectTaskValid(const std::string& collectorName) { +bool HostMonitorInputRunner::IsCollectTaskValid(std::chrono::steady_clock::time_point execTime, + const std::string& collectorName) { std::shared_lock lock(mRegisteredCollectorMapMutex); auto it = mRegisteredCollectorMap.find(collectorName); if (it == mRegisteredCollectorMap.end()) { return false; } - return it->second.IsEnabled(); + return it->second.IsEnabled() && (it->second.mLastEnableTime <= execTime); } void HostMonitorInputRunner::ScheduleOnce(std::chrono::steady_clock::time_point execTime, diff --git a/core/host_monitor/HostMonitorInputRunner.h b/core/host_monitor/HostMonitorInputRunner.h index a9f5fe6d1f..20cffb73f3 100644 --- a/core/host_monitor/HostMonitorInputRunner.h +++ b/core/host_monitor/HostMonitorInputRunner.h @@ -16,6 +16,8 @@ #pragma once +#include + #include #include #include @@ -40,10 +42,14 @@ struct CollectorInstance { std::unique_ptr mCollector; bool mIsEnabled = false; + std::chrono::steady_clock::time_point mLastEnableTime; BaseCollector* GetCollector() const { return mCollector.get(); } bool IsEnabled() const { return mIsEnabled; } - void Enable() { mIsEnabled = true; } + void Enable() { + mIsEnabled = true; + mLastEnableTime = std::chrono::steady_clock::now(); + } void Disable() { mIsEnabled = false; } }; @@ -67,7 +73,7 @@ class HostMonitorInputRunner : public InputRunner { void Stop() override; bool HasRegisteredPlugins() const override; - bool IsCollectTaskValid(const std::string& collectorName); + bool IsCollectTaskValid(std::chrono::steady_clock::time_point execTime, const std::string& collectorName); void ScheduleOnce(std::chrono::steady_clock::time_point execTime, HostMonitorTimerEvent::CollectConfig& collectConfig); diff --git a/core/host_monitor/HostMonitorTimerEvent.cpp b/core/host_monitor/HostMonitorTimerEvent.cpp index 62c46259df..944925a619 100755 --- a/core/host_monitor/HostMonitorTimerEvent.cpp +++ b/core/host_monitor/HostMonitorTimerEvent.cpp @@ -22,7 +22,7 @@ namespace logtail { bool HostMonitorTimerEvent::IsValid() const { - return HostMonitorInputRunner::GetInstance()->IsCollectTaskValid(mCollectConfig.mCollectorName); + return HostMonitorInputRunner::GetInstance()->IsCollectTaskValid(GetExecTime(), mCollectConfig.mCollectorName); } bool HostMonitorTimerEvent::Execute() { diff --git a/core/unittest/host_monitor/HostMonitorInputRunnerUnittest.cpp b/core/unittest/host_monitor/HostMonitorInputRunnerUnittest.cpp index 88504f9d5d..795521d679 100644 --- a/core/unittest/host_monitor/HostMonitorInputRunnerUnittest.cpp +++ b/core/unittest/host_monitor/HostMonitorInputRunnerUnittest.cpp @@ -53,11 +53,13 @@ void HostMonitorInputRunnerUnittest::TestUpdateAndRemoveCollector() const { auto runner = HostMonitorInputRunner::GetInstance(); runner->Init(); runner->UpdateCollector({MockCollector::sName}, QueueKey{}, 0); - APSARA_TEST_TRUE_FATAL(runner->IsCollectTaskValid(MockCollector::sName)); + APSARA_TEST_TRUE_FATAL(runner->IsCollectTaskValid(std::chrono::steady_clock::now(), MockCollector::sName)); + APSARA_TEST_FALSE_FATAL( + runner->IsCollectTaskValid(std::chrono::steady_clock::now() - std::chrono::seconds(60), MockCollector::sName)); APSARA_TEST_TRUE_FATAL(runner->HasRegisteredPlugins()); APSARA_TEST_EQUAL_FATAL(1, Timer::GetInstance()->mQueue.size()); runner->RemoveCollector(); - APSARA_TEST_FALSE_FATAL(runner->IsCollectTaskValid(MockCollector::sName)); + APSARA_TEST_FALSE_FATAL(runner->IsCollectTaskValid(std::chrono::steady_clock::now(), MockCollector::sName)); APSARA_TEST_FALSE_FATAL(runner->HasRegisteredPlugins()); runner->Stop(); }