1
1
#include < drogon/HttpAppFramework.h>
2
2
#include < drogon/drogon.h>
3
+ #include < trantor/utils/Logger.h>
3
4
#include < memory>
5
+ #include < mutex>
4
6
#include " controllers/assistants.h"
5
7
#include " controllers/configs.h"
6
8
#include " controllers/engines.h"
53
55
#error "Unsupported platform!"
54
56
#endif
55
57
58
+ // Global var to signal drogon to shutdown
59
+ volatile bool shutdown_signal;
60
+
56
61
void RunServer (std::optional<std::string> host, std::optional<int > port,
57
62
bool ignore_cout) {
58
63
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
59
- signal (SIGINT, SIG_IGN);
64
+ auto signal_handler = +[](int sig) -> void {
65
+ std::cout << " \r Caught interrupt signal, shutting down\n " ;
66
+ shutdown_signal = true ;
67
+ };
68
+ signal (SIGINT, signal_handler);
60
69
#elif defined(_WIN32)
61
70
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
62
- return (ctrl_type == CTRL_C_EVENT) ? true : false ;
71
+ if (ctrl_type == CTRL_C_EVENT) {
72
+ std::cout << " \r Caught interrupt signal, shutting down\n " ;
73
+ shutdown_signal = true ;
74
+ return TRUE ;
75
+ }
76
+ return FALSE ;
63
77
};
64
78
SetConsoleCtrlHandler (
65
- reinterpret_cast <PHANDLER_ROUTINE>(console_ctrl_handler), true );
79
+ reinterpret_cast <PHANDLER_ROUTINE>(console_ctrl_handler), TRUE );
66
80
#endif
67
81
auto config = file_manager_utils::GetCortexConfig ();
68
82
if (host.has_value () || port.has_value ()) {
@@ -204,16 +218,23 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
204
218
auto upload_path = std::filesystem::temp_directory_path () / " cortex-uploads" ;
205
219
drogon::app ().setUploadPath (upload_path.string ());
206
220
207
- LOG_INFO << " Server started, listening at: " << config.apiServerHost << " :"
208
- << config.apiServerPort ;
209
- LOG_INFO << " Please load your model" ;
210
221
#ifndef _WIN32
211
222
drogon::app ().enableReusePort ();
212
223
#else
213
224
drogon::app ().enableDateHeader (false );
214
225
#endif
215
- drogon::app ().addListener (config.apiServerHost ,
216
- std::stoi (config.apiServerPort ));
226
+ try {
227
+ drogon::app ().addListener (config.apiServerHost ,
228
+ std::stoi (config.apiServerPort ));
229
+ } catch (const std::exception& e) {
230
+ LOG_ERROR << " Failed to start server: " << e.what ();
231
+ return ;
232
+ }
233
+
234
+ LOG_INFO << " Server started, listening at: " << config.apiServerHost << " :"
235
+ << config.apiServerPort ;
236
+ LOG_INFO << " Please load your model" ;
237
+
217
238
drogon::app ().setThreadNum (drogon_thread_num);
218
239
LOG_INFO << " Number of thread is:" << drogon::app ().getThreadNum ();
219
240
drogon::app ().disableSigtermHandling ();
@@ -276,11 +297,37 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
276
297
drogon::app ().addListener (config.apiServerHost , 443 , true );
277
298
}
278
299
279
- drogon::app ().run ();
300
+ // Fires up the server in another thread and set the shutdown signal if it somehow dies
301
+ std::thread ([] {
302
+ drogon::app ().run ();
303
+ shutdown_signal = true ;
304
+ }).detach ();
305
+
306
+ // Now this thread can monitor the shutdown signal
307
+ while (!shutdown_signal) {
308
+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
309
+ }
310
+
280
311
if (hw_service->ShouldRestart ()) {
281
312
CTL_INF (" Restart to update hardware configuration" );
282
313
hw_service->Restart (config.apiServerHost , std::stoi (config.apiServerPort ));
283
314
}
315
+ drogon::app ().quit ();
316
+ }
317
+
318
+ void print_help () {
319
+ std::cout << " Usage: \n cortex-server [options]\n\n " ;
320
+ std::cout << " Options:\n " ;
321
+ std::cout << " --config_file_path Path to the config file (default: "
322
+ " ~/.cortexrc)\n " ;
323
+ std::cout << " --data_folder_path Path to the data folder (default: "
324
+ " ~/cortexcpp)\n " ;
325
+ std::cout << " --host Host name (default: 127.0.0.1)\n " ;
326
+ std::cout << " --port Port number (default: 39281)\n " ;
327
+ std::cout << " --ignore_cout Ignore cout output\n " ;
328
+ std::cout << " --loglevel Set log level\n " ;
329
+
330
+ exit (0 );
284
331
}
285
332
286
333
#if defined(_WIN32)
@@ -326,6 +373,8 @@ int main(int argc, char* argv[]) {
326
373
std::wstring v = argv[i + 1 ];
327
374
std::string log_level = cortex::wc::WstringToUtf8 (v);
328
375
logging_utils_helper::SetLogLevel (log_level, ignore_cout_log);
376
+ } else if (command == L" --help" || command == L" -h" ) {
377
+ print_help ();
329
378
}
330
379
}
331
380
#else
@@ -343,6 +392,8 @@ int main(int argc, char* argv[]) {
343
392
} else if (strcmp (argv[i], " --loglevel" ) == 0 ) {
344
393
std::string log_level = argv[i + 1 ];
345
394
logging_utils_helper::SetLogLevel (log_level, ignore_cout_log);
395
+ } else if (strcmp (argv[i], " --help" ) == 0 || strcmp (argv[i], " -h" ) == 0 ) {
396
+ print_help ();
346
397
}
347
398
}
348
399
#endif
0 commit comments