Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 4132f2f

Browse files
authored
feat: QoL improvements for server when running standalone (#1925)
* feat: QoL improvements for server when running standalone * fix: detach server child from cli console
1 parent d05e8e4 commit 4132f2f

File tree

2 files changed

+70
-19
lines changed

2 files changed

+70
-19
lines changed

engine/cli/commands/server_start_cmd.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ bool ServerStartCmd::Exec(const std::string& host, int port,
8080
mutable_cmds.push_back(L'\0');
8181
// Create child process
8282
if (!CreateProcess(
83-
NULL, // No module name (use command line)
83+
NULL, // No module name (use command line)
8484
mutable_cmds
85-
.data(), // Command line (replace with your actual executable)
86-
NULL, // Process handle not inheritable
87-
NULL, // Thread handle not inheritable
88-
FALSE, // Set handle inheritance
89-
0, // No creation flags
90-
NULL, // Use parent's environment block
91-
NULL, // Use parent's starting directory
92-
&si, // Pointer to STARTUPINFO structure
93-
&pi)) // Pointer to PROCESS_INFORMATION structure
85+
.data(), // Command line (replace with your actual executable)
86+
NULL, // Process handle not inheritable
87+
NULL, // Thread handle not inheritable
88+
FALSE, // Set handle inheritance
89+
CREATE_NO_WINDOW, // No new console
90+
NULL, // Use parent's environment block
91+
NULL, // Use parent's starting directory
92+
&si, // Pointer to STARTUPINFO structure
93+
&pi)) // Pointer to PROCESS_INFORMATION structure
9494
{
9595
std::cout << "Could not start server: " << GetLastError() << std::endl;
9696
return false;

engine/main.cc

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <drogon/HttpAppFramework.h>
22
#include <drogon/drogon.h>
3+
#include <trantor/utils/Logger.h>
34
#include <memory>
5+
#include <mutex>
46
#include "controllers/assistants.h"
57
#include "controllers/configs.h"
68
#include "controllers/engines.h"
@@ -53,16 +55,28 @@
5355
#error "Unsupported platform!"
5456
#endif
5557

58+
// Global var to signal drogon to shutdown
59+
volatile bool shutdown_signal;
60+
5661
void RunServer(std::optional<std::string> host, std::optional<int> port,
5762
bool ignore_cout) {
5863
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
59-
signal(SIGINT, SIG_IGN);
64+
auto signal_handler = +[](int sig) -> void {
65+
std::cout << "\rCaught interrupt signal, shutting down\n";
66+
shutdown_signal = true;
67+
};
68+
signal(SIGINT, signal_handler);
6069
#elif defined(_WIN32)
6170
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 << "\rCaught interrupt signal, shutting down\n";
73+
shutdown_signal = true;
74+
return TRUE;
75+
}
76+
return FALSE;
6377
};
6478
SetConsoleCtrlHandler(
65-
reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
79+
reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), TRUE);
6680
#endif
6781
auto config = file_manager_utils::GetCortexConfig();
6882
if (host.has_value() || port.has_value()) {
@@ -204,16 +218,23 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
204218
auto upload_path = std::filesystem::temp_directory_path() / "cortex-uploads";
205219
drogon::app().setUploadPath(upload_path.string());
206220

207-
LOG_INFO << "Server started, listening at: " << config.apiServerHost << ":"
208-
<< config.apiServerPort;
209-
LOG_INFO << "Please load your model";
210221
#ifndef _WIN32
211222
drogon::app().enableReusePort();
212223
#else
213224
drogon::app().enableDateHeader(false);
214225
#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+
217238
drogon::app().setThreadNum(drogon_thread_num);
218239
LOG_INFO << "Number of thread is:" << drogon::app().getThreadNum();
219240
drogon::app().disableSigtermHandling();
@@ -276,11 +297,37 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
276297
drogon::app().addListener(config.apiServerHost, 443, true);
277298
}
278299

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+
280311
if (hw_service->ShouldRestart()) {
281312
CTL_INF("Restart to update hardware configuration");
282313
hw_service->Restart(config.apiServerHost, std::stoi(config.apiServerPort));
283314
}
315+
drogon::app().quit();
316+
}
317+
318+
void print_help() {
319+
std::cout << "Usage: \ncortex-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);
284331
}
285332

286333
#if defined(_WIN32)
@@ -326,6 +373,8 @@ int main(int argc, char* argv[]) {
326373
std::wstring v = argv[i + 1];
327374
std::string log_level = cortex::wc::WstringToUtf8(v);
328375
logging_utils_helper::SetLogLevel(log_level, ignore_cout_log);
376+
} else if (command == L"--help" || command == L"-h") {
377+
print_help();
329378
}
330379
}
331380
#else
@@ -343,6 +392,8 @@ int main(int argc, char* argv[]) {
343392
} else if (strcmp(argv[i], "--loglevel") == 0) {
344393
std::string log_level = argv[i + 1];
345394
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();
346397
}
347398
}
348399
#endif

0 commit comments

Comments
 (0)