125{
126 std::cout << "=== Health Check Example ===\n\n";
127
128
129 std::cout << "1. Health check types:\n";
130 std::cout <<
" liveness -> \"" <<
to_string(health_check_type::liveness) <<
"\"\n";
131 std::cout <<
" readiness -> \"" <<
to_string(health_check_type::readiness) <<
"\"\n";
132 std::cout <<
" dependency -> \"" <<
to_string(health_check_type::dependency) <<
"\"\n";
133
135 if (parsed.is_ok())
136 {
137 std::cout <<
" \"readiness\" -> " <<
to_string(parsed.value()) <<
"\n";
138 }
139
140
141 std::cout << "\n2. Running health checks:\n";
142 std::vector<std::unique_ptr<health_check>> checks;
143 checks.push_back(std::make_unique<app_liveness_check>());
144 checks.push_back(std::make_unique<cache_readiness_check>(true));
145 checks.push_back(std::make_unique<database_dependency_check>(true));
146
147 bool all_healthy = true;
148 for (const auto& hc : checks)
149 {
150 auto result = hc->check();
151 std::cout <<
" [" <<
to_string(hc->get_type()) <<
"] " << hc->get_name() <<
": "
153 << " (critical=" << (hc->is_critical() ? "yes" : "no")
154 << ", timeout=" << hc->get_timeout().count() << "ms)\n";
155
156 if (result.status == health_status::unhealthy && hc->is_critical())
157 {
158 all_healthy = false;
159 }
160 }
161
162 std::cout << "\n Overall: " << (all_healthy ? "HEALTHY" : "UNHEALTHY") << "\n";
163
164
165 std::cout << "\n3. Simulating database failure:\n";
167 auto result = db_down.check();
168 std::cout <<
" " << db_down.get_name() <<
": " <<
status_string(result.status) <<
" - "
169 << result.message << "\n";
170
171 std::cout << "\nDone.\n";
172 return 0;
173}
std::string status_string(health_status status)
Result< health_check_type > health_check_type_from_string(const std::string &str)
Convert string to health check type.
std::string to_string(log_level level)
Convert log level to string.