Skip to content

Commit

Permalink
fix: pull issue (#1381)
Browse files Browse the repository at this point in the history
  • Loading branch information
vansangpfiev authored Oct 1, 2024
1 parent c12a78f commit 9d39a50
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion engine/commands/engine_install_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void EngineInstallCmd::Exec(const std::string& engine,
auto result = engine_service_.InstallEngine(engine, version, src);
if (result.has_error()) {
CLI_LOG(result.error());
} else {
} else if(result && result.value()){
CLI_LOG("Engine " << engine << " installed successfully!");
}
}
Expand Down
20 changes: 11 additions & 9 deletions engine/services/download_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ cpp::result<void, std::string> DownloadService::VerifyDownloadTask(
return {};
}

cpp::result<void, std::string> DownloadService::AddDownloadTask(
cpp::result<bool, std::string> DownloadService::AddDownloadTask(
DownloadTask& task,
std::optional<OnDownloadTaskSuccessfully> callback) noexcept {
auto validating_result = VerifyDownloadTask(task);
Expand All @@ -65,12 +65,15 @@ cpp::result<void, std::string> DownloadService::AddDownloadTask(
// if any item from the task failed to download, the whole task will be
// considered failed
std::optional<std::string> dl_err_msg = std::nullopt;
bool has_task_done = false;
for (const auto& item : task.items) {
CLI_LOG("Start downloading: " + item.localPath.filename().string());
auto result = Download(task.id, item, true);
if (result.has_error()) {
dl_err_msg = result.error();
break;
} else if(result) {
has_task_done |= result.value();
}
}
if (dl_err_msg.has_value()) {
Expand All @@ -81,7 +84,7 @@ cpp::result<void, std::string> DownloadService::AddDownloadTask(
if (callback.has_value()) {
callback.value()(task);
}
return {};
return has_task_done;
}

cpp::result<uint64_t, std::string> DownloadService::GetFileSize(
Expand Down Expand Up @@ -109,7 +112,7 @@ cpp::result<uint64_t, std::string> DownloadService::GetFileSize(
return content_length;
}

cpp::result<void, std::string> DownloadService::AddAsyncDownloadTask(
cpp::result<bool, std::string> DownloadService::AddAsyncDownloadTask(
DownloadTask& task,
std::optional<OnDownloadTaskSuccessfully> callback) noexcept {
auto verifying_result = VerifyDownloadTask(task);
Expand Down Expand Up @@ -142,10 +145,10 @@ cpp::result<void, std::string> DownloadService::AddAsyncDownloadTask(
std::thread t(execute_download_async);
t.detach();

return {};
return true;
}

cpp::result<void, std::string> DownloadService::Download(
cpp::result<bool, std::string> DownloadService::Download(
const std::string& download_id, const DownloadItem& download_item,
bool allow_resume) noexcept {
CTL_INF("Absolute file output: " << download_item.localPath.string());
Expand Down Expand Up @@ -182,8 +185,7 @@ cpp::result<void, std::string> DownloadService::Download(
mode = "ab";
CLI_LOG("Resuming download..");
} else {
CLI_LOG("Start over..");
return cpp::fail("Cancelled Resume download!");
CLI_LOG("Start over..");
}
} else {
CLI_LOG(download_item.localPath.filename().string()
Expand All @@ -195,7 +197,7 @@ cpp::result<void, std::string> DownloadService::Download(
if (answer == "Y" || answer == "y" || answer.empty()) {
CLI_LOG("Re-downloading..");
} else {
return cpp::fail("Cancelled Re-download!");
return false;
}
}
}
Expand Down Expand Up @@ -232,7 +234,7 @@ cpp::result<void, std::string> DownloadService::Download(

fclose(file);
curl_easy_cleanup(curl);
return {};
return true;
}

curl_off_t DownloadService::GetLocalFileSize(
Expand Down
6 changes: 3 additions & 3 deletions engine/services/download_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ class DownloadService {
using OnDownloadTaskSuccessfully =
std::function<void(const DownloadTask& task)>;

cpp::result<void, std::string> AddDownloadTask(
cpp::result<bool, std::string> AddDownloadTask(
DownloadTask& task, std::optional<OnDownloadTaskSuccessfully> callback =
std::nullopt) noexcept;

cpp::result<void, std::string> AddAsyncDownloadTask(
cpp::result<bool, std::string> AddAsyncDownloadTask(
DownloadTask& task, std::optional<OnDownloadTaskSuccessfully> callback =
std::nullopt) noexcept;

Expand All @@ -77,7 +77,7 @@ class DownloadService {
cpp::result<void, std::string> VerifyDownloadTask(
DownloadTask& task) const noexcept;

cpp::result<void, std::string> Download(
cpp::result<bool, std::string> Download(
const std::string& download_id, const DownloadItem& download_item,
bool allow_resume) noexcept;

Expand Down
20 changes: 10 additions & 10 deletions engine/services/engine_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ std::vector<EngineInfo> EngineService::GetEngineInfoList() const {
return engines;
}

cpp::result<void, std::string> EngineService::InstallEngine(
cpp::result<bool, std::string> EngineService::InstallEngine(
const std::string& engine, const std::string& version,
const std::string& src) {

Expand All @@ -131,7 +131,7 @@ cpp::result<void, std::string> EngineService::InstallEngine(
}
}

cpp::result<void, std::string> EngineService::UnzipEngine(
cpp::result<bool, std::string> EngineService::UnzipEngine(
const std::string& engine, const std::string& version,
const std::string& path) {
bool found_cuda = false;
Expand Down Expand Up @@ -193,10 +193,10 @@ cpp::result<void, std::string> EngineService::UnzipEngine(
return DownloadCuda(engine);
}

return {};
return true;
}

cpp::result<void, std::string> EngineService::UninstallEngine(
cpp::result<bool, std::string> EngineService::UninstallEngine(
const std::string& engine) {
auto ecp = file_manager_utils::GetEnginesContainerPath();
auto engine_path = ecp / engine;
Expand All @@ -208,14 +208,14 @@ cpp::result<void, std::string> EngineService::UninstallEngine(
try {
std::filesystem::remove_all(engine_path);
CTL_INF("Engine " << engine << " uninstalled successfully!");
return {};
return true;
} catch (const std::exception& e) {
CTL_ERR("Failed to uninstall engine " << engine << ": " << e.what());
return cpp::fail("Failed to uninstall engine " + engine + ": " + e.what());
}
}

cpp::result<void, std::string> EngineService::DownloadEngine(
cpp::result<bool, std::string> EngineService::DownloadEngine(
const std::string& engine, const std::string& version) {
auto get_params = [&engine, &version]() -> std::vector<std::string> {
if (version == "latest") {
Expand Down Expand Up @@ -322,22 +322,22 @@ cpp::result<void, std::string> EngineService::DownloadEngine(
});
}
}
return {};
return true;
} else {
return cpp::fail("Failed to fetch engine release: " + engine);
}
}

cpp::result<void, std::string> EngineService::DownloadCuda(
cpp::result<bool, std::string> EngineService::DownloadCuda(
const std::string& engine) {
if (hw_inf_.sys_inf->os == "mac" || engine == "cortex.onnx") {
// mac and onnx engine does not require cuda toolkit
return {};
return true;
}

if (hw_inf_.cuda_driver_version.empty()) {
CTL_WRN("No cuda driver, continue with CPU");
return {};
return true;
}
// download cuda toolkit
const std::string jan_host = "catalog.jan.ai";
Expand Down
10 changes: 5 additions & 5 deletions engine/services/engine_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ class EngineService {

std::vector<EngineInfo> GetEngineInfoList() const;

cpp::result<void, std::string> InstallEngine(
cpp::result<bool, std::string> InstallEngine(
const std::string& engine, const std::string& version = "latest",
const std::string& src = "");

cpp::result<void, std::string> UninstallEngine(const std::string& engine);
cpp::result<bool, std::string> UninstallEngine(const std::string& engine);

private:
cpp::result<void, std::string> UnzipEngine(const std::string& engine,
cpp::result<bool, std::string> UnzipEngine(const std::string& engine,
const std::string& version,
const std::string& path);

cpp::result<void, std::string> DownloadEngine(
cpp::result<bool, std::string> DownloadEngine(
const std::string& engine, const std::string& version = "latest");

cpp::result<void, std::string> DownloadCuda(const std::string& engine);
cpp::result<bool, std::string> DownloadCuda(const std::string& engine);

std::string GetMatchedVariant(const std::string& engine,
const std::vector<std::string>& variants);
Expand Down
8 changes: 4 additions & 4 deletions engine/services/model_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ cpp::result<std::string, std::string> ModelService::HandleUrl(
if (result.has_error()) {
// CTL_ERR(result.error());
return cpp::fail(result.error());
} else {
} else if (result && result.value()) {
CLI_LOG("Model " << model_id << " downloaded successfully!")
}
return unique_model_id;
Expand Down Expand Up @@ -295,7 +295,7 @@ cpp::result<std::string, std::string> ModelService::DownloadModelFromCortexso(

if (result.has_error()) {
return cpp::fail(result.error());
} else {
} else if (result && result.value()) {
CLI_LOG("Model " << model_id << " downloaded successfully!")
}

Expand Down Expand Up @@ -415,7 +415,7 @@ cpp::result<bool, std::string> ModelService::StartModel(
auto res = cli.Post("/inferences/server/loadmodel", httplib::Headers(),
data_str.data(), data_str.size(), "application/json");
if (res) {
if (res->status == httplib::StatusCode::OK_200) {
if (res->status == httplib::StatusCode::OK_200) {
return true;
} else {
CTL_ERR("Model failed to load with status code: " << res->status);
Expand Down Expand Up @@ -459,7 +459,7 @@ cpp::result<bool, std::string> ModelService::StopModel(
auto res = cli.Post("/inferences/server/unloadmodel", httplib::Headers(),
data_str.data(), data_str.size(), "application/json");
if (res) {
if (res->status == httplib::StatusCode::OK_200) {
if (res->status == httplib::StatusCode::OK_200) {
return true;
} else {
CTL_ERR("Model failed to unload with status code: " << res->status);
Expand Down

0 comments on commit 9d39a50

Please sign in to comment.