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
1215namespace 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}
0 commit comments