Skip to content

Commit b10846b

Browse files
committed
feat: add heartbeat feature
Detect heartbeat status every 5 seconds. If no heartbeat is received within 5 seconds, the program will exit automatically.
1 parent 127b9bf commit b10846b

5 files changed

Lines changed: 55 additions & 2 deletions

File tree

OdbDesignServer/Controllers/HealthCheckController.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Odb::App::Server
2222
register_route_handler("/healthz/live", std::bind(&HealthCheckController::health_check_live, this, std::placeholders::_1));
2323
register_route_handler("/healthz/ready", std::bind(&HealthCheckController::health_check_ready, this, std::placeholders::_1));
2424
register_route_handler("/healthz/started", std::bind(&HealthCheckController::health_check_started, this, std::placeholders::_1));
25+
register_route_handler("/healthz/heartbeat", std::bind(&HealthCheckController::health_check_heartbeat, this, std::placeholders::_1));
2526
}
2627

2728
crow::response HealthCheckController::health_check_live(const crow::request& req)
@@ -38,4 +39,11 @@ namespace Odb::App::Server
3839
{
3940
return crow::response(crow::status::OK, "txt", "healthy: started");
4041
}
42+
43+
crow::response HealthCheckController::health_check_heartbeat(const crow::request& req)
44+
{
45+
// update last heartbeat
46+
OdbDesignServerApp::inst_->updateLastHeartbeat();
47+
return crow::response(crow::status::OK, "txt", "heartbeat received");
48+
}
4149
}

OdbDesignServer/Controllers/HealthCheckController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Odb::App::Server
1818
crow::response health_check_live(const crow::request& req);
1919
crow::response health_check_ready(const crow::request& req);
2020
crow::response health_check_started(const crow::request& req);
21+
crow::response health_check_heartbeat(const crow::request& req);
2122

2223
};
2324
}

OdbDesignServer/OdbDesignServerApp.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <thread>
2+
#include <chrono>
3+
#include <iostream>
14
#include "OdbDesignServerApp.h"
25
#include "Controllers/HelloWorldController.h"
36
#include "Controllers/FileUploadController.h"
@@ -11,9 +14,13 @@ using namespace Odb::Lib::App;
1114

1215
namespace Odb::App::Server
1316
{
17+
OdbDesignServerApp* OdbDesignServerApp::inst_ = nullptr;
1418
OdbDesignServerApp::OdbDesignServerApp(int argc, char* argv[])
1519
: OdbServerAppBase(argc, argv)
16-
{
20+
{
21+
inst_ = this;
22+
// set last heartbeat time to now
23+
lastHeartbeat_.store(std::chrono::steady_clock::now(), std::memory_order_relaxed);
1724
}
1825

1926
//OdbDesignServerApp::~OdbDesignServerApp()
@@ -44,6 +51,24 @@ namespace Odb::App::Server
4451
m_vecControllers.push_back(std::make_shared<DesignsController>(*this));
4552
}
4653

54+
void monitorHeartbeat()
55+
{
56+
auto lastTime = OdbDesignServerApp::inst_->lastHeartbeat_.load(std::memory_order_relaxed);
57+
while (true)
58+
{
59+
std::this_thread::sleep_for(std::chrono::seconds(1));
60+
auto now = std::chrono::steady_clock::now();
61+
auto diff = now - lastTime;
62+
// check heartbeat
63+
if (diff > std::chrono::seconds(5))
64+
{
65+
std::cerr << "Heartbeat timeout, exiting..." << std::endl;
66+
exit(0);
67+
}
68+
lastTime = OdbDesignServerApp::inst_->lastHeartbeat_.load(std::memory_order_relaxed);
69+
}
70+
}
71+
4772
bool OdbDesignServerApp::preServerRun()
4873
{
4974
// CORS
@@ -70,6 +95,10 @@ namespace Odb::App::Server
7095
auto basicRequestAuth = std::make_unique<BasicRequestAuthentication>(BasicRequestAuthentication(disableAuth));
7196
request_auth(std::move(basicRequestAuth));
7297

98+
// start heart beat monitor
99+
std::thread heartbeatMonitor(monitorHeartbeat);
100+
heartbeatMonitor.detach(); // run in background
101+
73102
return true;
74103
}
75104
}

OdbDesignServer/OdbDesignServerApp.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ namespace Odb::App::Server
1414

1515
//Utils::ExitCode Run() override;
1616

17-
protected:
17+
static OdbDesignServerApp* inst_;
18+
// store last heartbeat time
19+
std::atomic<std::chrono::steady_clock::time_point> lastHeartbeat_;
20+
void updateLastHeartbeat()
21+
{
22+
lastHeartbeat_.store(std::chrono::steady_clock::now(), std::memory_order_relaxed);
23+
}
24+
25+
protected:
1826
void add_controllers() override;
1927

2028

swagger/odbdesign-server-0.9-swagger.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,13 @@ paths:
606606
"200":
607607
description: ""
608608
/healthz/ready:
609+
get:
610+
tags: ["health check"]
611+
parameters: []
612+
responses:
613+
"200":
614+
description: ""
615+
/healthz/heartbeat:
609616
get:
610617
tags: ["health check"]
611618
parameters: []

0 commit comments

Comments
 (0)