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

Commit 3f9680c

Browse files
fix: Isolate stdout from the API server and CLI (#1526)
Co-authored-by: vansangpfiev <sang@jan.ai>
1 parent 5562284 commit 3f9680c

File tree

5 files changed

+87
-50
lines changed

5 files changed

+87
-50
lines changed

engine/controllers/engines.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void Engines::InstallEngine(
2121
}
2222

2323
auto version{"latest"};
24-
auto result = engine_service_->InstallEngine(engine, version);
24+
auto result = engine_service_->InstallEngineAsync(engine, version);
2525
if (result.has_error()) {
2626
Json::Value res;
2727
res["message"] = result.error();
@@ -30,7 +30,7 @@ void Engines::InstallEngine(
3030
callback(resp);
3131
} else {
3232
Json::Value res;
33-
res["message"] = "Engine " + engine + " installed successfully!";
33+
res["message"] = "Engine " + engine + " starts installing!";
3434
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
3535
resp->setStatusCode(k200OK);
3636
callback(resp);

engine/main.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ int main(int argc, char* argv[]) {
126126
return 1;
127127
}
128128

129+
// avoid printing logs to terminal
130+
is_server = true;
131+
129132
std::optional<int> server_port;
130133
for (int i = 0; i < argc; i++) {
131134
if (strcmp(argv[i], "--config_file_path") == 0) {

engine/services/engine_service.cc

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ cpp::result<bool, std::string> EngineService::InstallEngine(
136136
}
137137
}
138138

139+
cpp::result<bool, std::string> EngineService::InstallEngineAsync(
140+
const std::string& engine, const std::string& version,
141+
const std::string& src) {
142+
// Although this function is called async, only download tasks are performed async
143+
// TODO(sang) better handler for unzip and download scenarios
144+
auto ne = NormalizeEngine(engine);
145+
if (!src.empty()) {
146+
return UnzipEngine(ne, version, src);
147+
} else {
148+
auto result = DownloadEngine(ne, version, true /*async*/);
149+
if (result.has_error()) {
150+
return result;
151+
}
152+
return DownloadCuda(ne, true /*async*/);
153+
}
154+
}
155+
139156
cpp::result<bool, std::string> EngineService::UnzipEngine(
140157
const std::string& engine, const std::string& version,
141158
const std::string& path) {
@@ -222,7 +239,7 @@ cpp::result<bool, std::string> EngineService::UninstallEngine(
222239
}
223240

224241
cpp::result<bool, std::string> EngineService::DownloadEngine(
225-
const std::string& engine, const std::string& version) {
242+
const std::string& engine, const std::string& version, bool async) {
226243

227244
// Check if GITHUB_TOKEN env exist
228245
const char* github_token = std::getenv("GITHUB_TOKEN");
@@ -320,27 +337,34 @@ cpp::result<bool, std::string> EngineService::DownloadEngine(
320337
.localPath = local_path,
321338
}}}};
322339

323-
return download_service_->AddDownloadTask(
324-
downloadTask, [](const DownloadTask& finishedTask) {
325-
// try to unzip the downloaded file
326-
CTL_INF("Engine zip path: "
327-
<< finishedTask.items[0].localPath.string());
328-
329-
std::filesystem::path extract_path =
330-
finishedTask.items[0].localPath.parent_path().parent_path();
331-
332-
archive_utils::ExtractArchive(
333-
finishedTask.items[0].localPath.string(),
334-
extract_path.string());
335-
336-
// remove the downloaded file
337-
try {
338-
std::filesystem::remove(finishedTask.items[0].localPath);
339-
} catch (const std::exception& e) {
340-
CTL_WRN("Could not delete file: " << e.what());
341-
}
342-
CTL_INF("Finished!");
343-
});
340+
auto on_finished = [](const DownloadTask& finishedTask) {
341+
// try to unzip the downloaded file
342+
CTL_INF(
343+
"Engine zip path: " << finishedTask.items[0].localPath.string());
344+
345+
std::filesystem::path extract_path =
346+
finishedTask.items[0].localPath.parent_path().parent_path();
347+
348+
archive_utils::ExtractArchive(
349+
finishedTask.items[0].localPath.string(), extract_path.string());
350+
351+
// remove the downloaded file
352+
try {
353+
std::filesystem::remove(finishedTask.items[0].localPath);
354+
} catch (const std::exception& e) {
355+
CTL_WRN("Could not delete file: " << e.what());
356+
}
357+
CTL_INF("Finished!");
358+
};
359+
if (async) {
360+
auto res = download_service_->AddTask(downloadTask, on_finished);
361+
if (res.has_error()) {
362+
return cpp::fail(res.error());
363+
}
364+
return true;
365+
} else {
366+
return download_service_->AddDownloadTask(downloadTask, on_finished);
367+
}
344368
}
345369
}
346370
return true;
@@ -350,7 +374,7 @@ cpp::result<bool, std::string> EngineService::DownloadEngine(
350374
}
351375

352376
cpp::result<bool, std::string> EngineService::DownloadCuda(
353-
const std::string& engine) {
377+
const std::string& engine, bool async) {
354378
if (hw_inf_.sys_inf->os == "mac" || engine == kOnnxRepo ||
355379
engine == kOnnxEngine) {
356380
// mac and onnx engine does not require cuda toolkit
@@ -403,19 +427,27 @@ cpp::result<bool, std::string> EngineService::DownloadCuda(
403427
.localPath = cuda_toolkit_local_path}},
404428
}};
405429

406-
return download_service_->AddDownloadTask(
407-
downloadCudaToolkitTask, [&](const DownloadTask& finishedTask) {
408-
auto engine_path =
409-
file_manager_utils::GetEnginesContainerPath() / engine;
410-
archive_utils::ExtractArchive(finishedTask.items[0].localPath.string(),
411-
engine_path.string());
412-
413-
try {
414-
std::filesystem::remove(finishedTask.items[0].localPath);
415-
} catch (std::exception& e) {
416-
CTL_ERR("Error removing downloaded file: " << e.what());
417-
}
418-
});
430+
auto on_finished = [engine](const DownloadTask& finishedTask) {
431+
auto engine_path = file_manager_utils::GetEnginesContainerPath() / engine;
432+
archive_utils::ExtractArchive(finishedTask.items[0].localPath.string(),
433+
engine_path.string());
434+
435+
try {
436+
std::filesystem::remove(finishedTask.items[0].localPath);
437+
} catch (std::exception& e) {
438+
CTL_ERR("Error removing downloaded file: " << e.what());
439+
}
440+
};
441+
if (async) {
442+
auto res = download_service_->AddTask(downloadCudaToolkitTask, on_finished);
443+
if (res.has_error()) {
444+
return cpp::fail(res.error());
445+
}
446+
return true;
447+
} else {
448+
return download_service_->AddDownloadTask(downloadCudaToolkitTask,
449+
on_finished);
450+
}
419451
}
420452

421453
std::string EngineService::GetMatchedVariant(

engine/services/engine_service.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class EngineService {
4747
const std::string& engine, const std::string& version = "latest",
4848
const std::string& src = "");
4949

50+
cpp::result<bool, std::string> InstallEngineAsync(
51+
const std::string& engine, const std::string& version = "latest",
52+
const std::string& src = "");
53+
5054
cpp::result<bool, std::string> UninstallEngine(const std::string& engine);
5155

5256
private:
@@ -55,9 +59,9 @@ class EngineService {
5559
const std::string& path);
5660

5761
cpp::result<bool, std::string> DownloadEngine(
58-
const std::string& engine, const std::string& version = "latest");
62+
const std::string& engine, const std::string& version = "latest", bool async = false);
5963

60-
cpp::result<bool, std::string> DownloadCuda(const std::string& engine);
64+
cpp::result<bool, std::string> DownloadCuda(const std::string& engine, bool async = false);
6165

6266
std::string GetMatchedVariant(const std::string& engine,
6367
const std::vector<std::string>& variants);

engine/utils/logging_utils.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,26 @@
55
// if not verbose only log result to console
66
inline bool log_verbose = false;
77

8+
// CLI and Server are sharing logging utils, only print to message if executable is CLI
9+
inline bool is_server = false;
10+
811
// Only use trantor log
9-
#define CTL_DBG(msg) \
10-
LOG_DEBUG << msg;
11-
12+
#define CTL_DBG(msg) LOG_DEBUG << msg;
1213

13-
#define CTL_INF(msg) \
14-
LOG_INFO << msg;
14+
#define CTL_INF(msg) LOG_INFO << msg;
1515

16-
#define CTL_WRN(msg) \
17-
LOG_WARN << msg;
16+
#define CTL_WRN(msg) LOG_WARN << msg;
1817

19-
// Use std::cout if not verbose, use trantor log if verbose
2018
#define CTL_ERR(msg) LOG_ERROR << msg;
2119

2220
#define CLI_LOG(msg) \
23-
if (log_verbose) { \
21+
if (log_verbose || is_server) { \
2422
LOG_INFO << msg; \
2523
} else { \
2624
std::cout << msg << std::endl; \
2725
}
2826
#define CLI_LOG_ERROR(msg) \
29-
if (log_verbose) { \
27+
if (log_verbose || is_server) { \
3028
LOG_INFO << msg; \
3129
} else { \
3230
LOG_ERROR << msg; \

0 commit comments

Comments
 (0)