Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::interfaces::health_check_builder Class Reference

Fluent builder for creating health checks. More...

#include <health_check_builder.h>

Collaboration diagram for kcenon::common::interfaces::health_check_builder:
Collaboration graph

Public Types

using check_fn_type = std::function<health_check_result()>
 

Public Member Functions

 health_check_builder ()=default
 
health_check_buildername (std::string value)
 Set the health check name.
 
health_check_buildertype (health_check_type value)
 Set the health check type.
 
health_check_builderwith_check (check_fn_type fn)
 Set the check function.
 
health_check_buildercritical (bool value)
 Set whether this check is critical.
 
health_check_buildertimeout (std::chrono::milliseconds value)
 Set the check timeout.
 
Result< std::shared_ptr< health_check > > build () const
 Build the health check.
 
std::shared_ptr< health_checkbuild_unsafe () const
 Build the health check without validation.
 
health_check_builderreset ()
 Reset the builder to initial state.
 

Private Attributes

std::string name_
 
health_check_type type_ = health_check_type::custom
 
check_fn_type check_fn_
 
bool critical_ = true
 
std::chrono::milliseconds timeout_ {5000}
 

Detailed Description

Fluent builder for creating health checks.

This class provides a builder pattern for constructing health checks with a fluent API. It allows setting various properties before creating the final health check object.

Example usage:

auto check = health_check_builder()
.name("database")
.timeout(std::chrono::seconds{10})
.critical(true)
.with_check([]() {
result.message = "Database OK";
return result;
})
.build();
Result< std::shared_ptr< health_check > > build() const
Build the health check.

Definition at line 48 of file health_check_builder.h.

Member Typedef Documentation

◆ check_fn_type

Constructor & Destructor Documentation

◆ health_check_builder()

kcenon::common::interfaces::health_check_builder::health_check_builder ( )
default

Member Function Documentation

◆ build()

Result< std::shared_ptr< health_check > > kcenon::common::interfaces::health_check_builder::build ( ) const
inlinenodiscard

Build the health check.

Returns
Result containing the health check or error

Validates that all required fields are set before building.

Definition at line 110 of file health_check_builder.h.

110 {
111 if (name_.empty()) {
112 return {error_info{1, "Health check name is required", "health_check_builder"}};
113 }
114
115 if (!check_fn_) {
116 return {error_info{2, "Check function is required", "health_check_builder"}};
117 }
118
119 std::shared_ptr<health_check> check = std::make_shared<lambda_health_check>(
121 return ok(std::move(check));
122 }
VoidResult ok()
Create a successful void result.
Definition utilities.h:71

References check_fn_, critical_, name_, kcenon::common::ok(), timeout_, and type_.

Here is the call graph for this function:

◆ build_unsafe()

std::shared_ptr< health_check > kcenon::common::interfaces::health_check_builder::build_unsafe ( ) const
inlinenodiscard

Build the health check without validation.

Returns
Shared pointer to the health check (may be invalid)

Use this method only when you're sure all required fields are set. For safer construction, use build() instead.

Definition at line 131 of file health_check_builder.h.

131 {
132 return std::make_shared<lambda_health_check>(
134 }

References check_fn_, critical_, name_, timeout_, and type_.

◆ critical()

health_check_builder & kcenon::common::interfaces::health_check_builder::critical ( bool value)
inline

Set whether this check is critical.

Parameters
valuetrue if critical
Returns
Reference to this builder

Definition at line 89 of file health_check_builder.h.

89 {
90 critical_ = value;
91 return *this;
92 }

References critical_.

◆ name()

health_check_builder & kcenon::common::interfaces::health_check_builder::name ( std::string value)
inline

Set the health check name.

Parameters
valueHealth check name
Returns
Reference to this builder

Definition at line 59 of file health_check_builder.h.

59 {
60 name_ = std::move(value);
61 return *this;
62 }

References name_.

◆ reset()

health_check_builder & kcenon::common::interfaces::health_check_builder::reset ( )
inline

Reset the builder to initial state.

Returns
Reference to this builder

Definition at line 140 of file health_check_builder.h.

140 {
141 name_.clear();
143 check_fn_ = nullptr;
144 critical_ = true;
145 timeout_ = std::chrono::milliseconds{5000};
146 return *this;
147 }

References check_fn_, critical_, kcenon::common::interfaces::custom, name_, timeout_, and type_.

◆ timeout()

health_check_builder & kcenon::common::interfaces::health_check_builder::timeout ( std::chrono::milliseconds value)
inline

Set the check timeout.

Parameters
valueTimeout duration
Returns
Reference to this builder

Definition at line 99 of file health_check_builder.h.

99 {
100 timeout_ = value;
101 return *this;
102 }

References timeout_.

◆ type()

health_check_builder & kcenon::common::interfaces::health_check_builder::type ( health_check_type value)
inline

Set the health check type.

Parameters
valueHealth check type
Returns
Reference to this builder

Definition at line 69 of file health_check_builder.h.

69 {
70 type_ = value;
71 return *this;
72 }

References type_.

◆ with_check()

health_check_builder & kcenon::common::interfaces::health_check_builder::with_check ( check_fn_type fn)
inline

Set the check function.

Parameters
fnFunction to execute for health check
Returns
Reference to this builder

Definition at line 79 of file health_check_builder.h.

79 {
80 check_fn_ = std::move(fn);
81 return *this;
82 }

References check_fn_.

Member Data Documentation

◆ check_fn_

check_fn_type kcenon::common::interfaces::health_check_builder::check_fn_
private

Definition at line 152 of file health_check_builder.h.

Referenced by build(), build_unsafe(), reset(), and with_check().

◆ critical_

bool kcenon::common::interfaces::health_check_builder::critical_ = true
private

Definition at line 153 of file health_check_builder.h.

Referenced by build(), build_unsafe(), critical(), and reset().

◆ name_

std::string kcenon::common::interfaces::health_check_builder::name_
private

Definition at line 150 of file health_check_builder.h.

Referenced by build(), build_unsafe(), name(), and reset().

◆ timeout_

std::chrono::milliseconds kcenon::common::interfaces::health_check_builder::timeout_ {5000}
private

Definition at line 154 of file health_check_builder.h.

154{5000};

Referenced by build(), build_unsafe(), reset(), and timeout().

◆ type_

health_check_type kcenon::common::interfaces::health_check_builder::type_ = health_check_type::custom
private

Definition at line 151 of file health_check_builder.h.

Referenced by build(), build_unsafe(), reset(), and type().


The documentation for this class was generated from the following file: