Skip to content

Commit 61d98ad

Browse files
committed
Introduce CheckResultProducerComponent
1 parent fff9c58 commit 61d98ad

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

lib/remote/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(remote_SOURCES
1313
apilistener.cpp apilistener.hpp apilistener-ti.hpp apilistener-configsync.cpp apilistener-filesync.cpp
1414
apilistener-authority.cpp
1515
apiuser.cpp apiuser.hpp apiuser-ti.hpp
16+
crproducer.cpp crproducer.hpp
1617
configfileshandler.cpp configfileshandler.hpp
1718
configobjectslock.cpp configobjectslock.hpp
1819
configobjectutility.cpp configobjectutility.hpp

lib/remote/crproducer.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
2+
3+
#include "remote/crproducer.hpp"
4+
5+
using namespace icinga;
6+
7+
bool CheckResultProducerComponent::try_lock_shared() noexcept
8+
{
9+
std::unique_lock lock (m_Mutex);
10+
11+
if (m_InstanceIsActive) {
12+
++m_ProcessingCheckResults;
13+
return true;
14+
}
15+
16+
return false;
17+
}
18+
19+
void CheckResultProducerComponent::unlock_shared() noexcept
20+
{
21+
std::unique_lock lock (m_Mutex);
22+
23+
if (!--m_ProcessingCheckResults && !m_InstanceIsActive) {
24+
m_CV.notify_all();
25+
}
26+
}
27+
28+
/**
29+
* Allow processing check results.
30+
*/
31+
void CheckResultProducerComponent::Start()
32+
{
33+
std::unique_lock lock (m_Mutex);
34+
35+
m_InstanceIsActive = true;
36+
}
37+
38+
/**
39+
* Disallow processing new check results, wait for all currently processed ones to finish.
40+
*/
41+
void CheckResultProducerComponent::Stop()
42+
{
43+
std::unique_lock lock (m_Mutex);
44+
45+
m_InstanceIsActive = false;
46+
m_CV.wait(lock, [this] { return !m_ProcessingCheckResults; });
47+
}

lib/remote/crproducer.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
2+
3+
#pragma once
4+
5+
#include "base/object.hpp"
6+
#include <condition_variable>
7+
#include <cstdint>
8+
#include <mutex>
9+
10+
namespace icinga
11+
{
12+
13+
/**
14+
* A component that produces check results and hence is responsible for them, e.g. CheckerComponent.
15+
* I.e. on its shutdown it has to clean up and/or wait for its own ongoing checks if any.
16+
*
17+
* @ingroup icinga
18+
*/
19+
class CheckResultProducer : virtual public Object
20+
{
21+
public:
22+
DECLARE_PTR_TYPEDEFS(CheckResultProducer);
23+
24+
/**
25+
* Requests to delay the producer shutdown (if any) for a CheckResult to be processed.
26+
*
27+
* @return Whether request was accepted.
28+
*/
29+
virtual bool try_lock_shared() noexcept = 0;
30+
31+
/**
32+
* Releases one semaphore slot acquired for CheckResult processing.
33+
*/
34+
virtual void unlock_shared() noexcept = 0;
35+
};
36+
37+
class CheckResultProducerComponent : public CheckResultProducer
38+
{
39+
public:
40+
bool try_lock_shared() noexcept override;
41+
void unlock_shared() noexcept override;
42+
43+
protected:
44+
void Start();
45+
void Stop();
46+
47+
private:
48+
std::mutex m_Mutex;
49+
std::condition_variable m_CV;
50+
bool m_InstanceIsActive = false;
51+
uint_fast32_t m_ProcessingCheckResults = 0;
52+
};
53+
54+
}

0 commit comments

Comments
 (0)