Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
health_check_builder.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
13#pragma once
14
15#include <chrono>
16#include <functional>
17#include <memory>
18#include <string>
19
20#include "health_check.h"
21
23
49public:
50 using check_fn_type = std::function<health_check_result()>;
51
53
59 health_check_builder& name(std::string value) {
60 name_ = std::move(value);
61 return *this;
62 }
63
70 type_ = value;
71 return *this;
72 }
73
80 check_fn_ = std::move(fn);
81 return *this;
82 }
83
90 critical_ = value;
91 return *this;
92 }
93
99 health_check_builder& timeout(std::chrono::milliseconds value) {
100 timeout_ = value;
101 return *this;
102 }
103
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 }
123
131 [[nodiscard]] std::shared_ptr<health_check> build_unsafe() const {
132 return std::make_shared<lambda_health_check>(
134 }
135
141 name_.clear();
143 check_fn_ = nullptr;
144 critical_ = true;
145 timeout_ = std::chrono::milliseconds{5000};
146 return *this;
147 }
148
149private:
150 std::string name_;
153 bool critical_ = true;
154 std::chrono::milliseconds timeout_{5000};
155};
156
157} // namespace kcenon::common::interfaces
Result type for error handling with member function support.
Definition core.cppm:165
Fluent builder for creating health checks.
health_check_builder & with_check(check_fn_type fn)
Set the check function.
std::function< health_check_result()> check_fn_type
Result< std::shared_ptr< health_check > > build() const
Build the health check.
std::shared_ptr< health_check > build_unsafe() const
Build the health check without validation.
health_check_builder & name(std::string value)
Set the health check name.
health_check_builder & type(health_check_type value)
Set the health check type.
health_check_builder & timeout(std::chrono::milliseconds value)
Set the check timeout.
health_check_builder & reset()
Reset the builder to initial state.
health_check_builder & critical(bool value)
Set whether this check is critical.
Base classes and types for health checking functionality.
health_check_type
Types of health checks supported by the system.
VoidResult ok()
Create a successful void result.
Definition utilities.h:71
Standard error information used by Result<T>.
Definition core.cppm:106