1+ #include " server_start_cmd.h"
2+ #include " commands/cortex_upd_cmd.h"
3+ #include " httplib.h"
4+ #include " trantor/utils/Logger.h"
5+ #include " utils/cortex_utils.h"
6+ #include " utils/file_manager_utils.h"
7+ #include " utils/logging_utils.h"
8+
9+ namespace commands {
10+
11+ namespace {
12+ bool TryConnectToServer (const std::string& host, int port) {
13+ constexpr const auto kMaxRetry = 3u ;
14+ auto count = 0u ;
15+ // Check if server is started
16+ while (true ) {
17+ if (IsServerAlive (host, port))
18+ break ;
19+ // Wait for server up
20+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
21+ if (count++ == kMaxRetry ) {
22+ std::cerr << " Could not start server" << std::endl;
23+ return false ;
24+ }
25+ }
26+ return true ;
27+ }
28+ } // namespace
29+
30+ ServerStartCmd::ServerStartCmd () {}
31+
32+ bool ServerStartCmd::Exec (const std::string& host, int port) {
33+ #if defined(_WIN32) || defined(_WIN64)
34+ // Windows-specific code to create a new process
35+ STARTUPINFO si;
36+ PROCESS_INFORMATION pi;
37+
38+ ZeroMemory (&si, sizeof (si));
39+ si.cb = sizeof (si);
40+ ZeroMemory (&pi, sizeof (pi));
41+ auto exe = commands::GetCortexBinary ();
42+ std::string cmds =
43+ cortex_utils::GetCurrentPath () + " /" + exe + " --start-server" ;
44+ // Create child process
45+ if (!CreateProcess (
46+ NULL , // No module name (use command line)
47+ const_cast <char *>(
48+ cmds.c_str ()), // Command line (replace with your actual executable)
49+ NULL , // Process handle not inheritable
50+ NULL , // Thread handle not inheritable
51+ FALSE , // Set handle inheritance to FALSE
52+ 0 , // No creation flags
53+ NULL , // Use parent's environment block
54+ NULL , // Use parent's starting directory
55+ &si, // Pointer to STARTUPINFO structure
56+ &pi)) // Pointer to PROCESS_INFORMATION structure
57+ {
58+ std::cout << " Could not start server: " << GetLastError () << std::endl;
59+ return false ;
60+ } else {
61+ if (!TryConnectToServer (host, port)) {
62+ return false ;
63+ }
64+ std::cout << " Server started" << std::endl;
65+ }
66+
67+ #else
68+ // Unix-like system-specific code to fork a child process
69+ pid_t pid = fork ();
70+
71+ if (pid < 0 ) {
72+ // Fork failed
73+ std::cerr << " Could not start server: " << std::endl;
74+ return false ;
75+ } else if (pid == 0 ) {
76+ // No need to configure LD_LIBRARY_PATH for macOS
77+ #if !defined(__APPLE__) || !defined(__MACH__)
78+ const char * name = " LD_LIBRARY_PATH" ;
79+ auto data = getenv (name);
80+ std::string v;
81+ if (auto g = getenv (name); g) {
82+ v += g;
83+ }
84+ CTL_INF (" LD_LIBRARY_PATH: " << v);
85+ auto data_path = file_manager_utils::GetCortexDataPath ();
86+ auto llamacpp_path = data_path / " engines" / " cortex.llamacpp/" ;
87+ auto trt_path = data_path / " engines" / " cortex.tensorrt-llm/" ;
88+ auto new_v = trt_path.string () + " :" + llamacpp_path.string () + " :" + v;
89+ setenv (name, new_v.c_str (), true );
90+ CTL_INF (" LD_LIBRARY_PATH: " << getenv (name));
91+ #endif
92+ auto exe = commands::GetCortexBinary ();
93+ std::string p = cortex_utils::GetCurrentPath () + " /" + exe;
94+ execl (p.c_str (), exe.c_str (), " --start-server" , (char *)0 );
95+ } else {
96+ // Parent process
97+ if (!TryConnectToServer (host, port)) {
98+ return false ;
99+ }
100+ std::cout << " Server started" << std::endl;
101+ }
102+ #endif
103+ return true ;
104+ }
105+
106+ }; // namespace commands
0 commit comments