From cf26dd7c52a2d2004bcde48f682bafe31b15e738 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Tue, 1 Oct 2024 17:30:27 +0700 Subject: [PATCH] fix: pull issue --- engine/commands/engine_install_cmd.cc | 2 +- engine/services/download_service.cc | 20 +++++++++++--------- engine/services/download_service.h | 6 +++--- engine/services/engine_service.cc | 20 ++++++++++---------- engine/services/engine_service.h | 10 +++++----- engine/services/model_service.cc | 8 ++++---- 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/engine/commands/engine_install_cmd.cc b/engine/commands/engine_install_cmd.cc index fdcac71ae..8cf7c1cc7 100644 --- a/engine/commands/engine_install_cmd.cc +++ b/engine/commands/engine_install_cmd.cc @@ -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!"); } } diff --git a/engine/services/download_service.cc b/engine/services/download_service.cc index d72384193..35444c238 100644 --- a/engine/services/download_service.cc +++ b/engine/services/download_service.cc @@ -53,7 +53,7 @@ cpp::result DownloadService::VerifyDownloadTask( return {}; } -cpp::result DownloadService::AddDownloadTask( +cpp::result DownloadService::AddDownloadTask( DownloadTask& task, std::optional callback) noexcept { auto validating_result = VerifyDownloadTask(task); @@ -65,12 +65,15 @@ cpp::result DownloadService::AddDownloadTask( // if any item from the task failed to download, the whole task will be // considered failed std::optional 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()) { @@ -81,7 +84,7 @@ cpp::result DownloadService::AddDownloadTask( if (callback.has_value()) { callback.value()(task); } - return {}; + return has_task_done; } cpp::result DownloadService::GetFileSize( @@ -109,7 +112,7 @@ cpp::result DownloadService::GetFileSize( return content_length; } -cpp::result DownloadService::AddAsyncDownloadTask( +cpp::result DownloadService::AddAsyncDownloadTask( DownloadTask& task, std::optional callback) noexcept { auto verifying_result = VerifyDownloadTask(task); @@ -142,10 +145,10 @@ cpp::result DownloadService::AddAsyncDownloadTask( std::thread t(execute_download_async); t.detach(); - return {}; + return true; } -cpp::result DownloadService::Download( +cpp::result DownloadService::Download( const std::string& download_id, const DownloadItem& download_item, bool allow_resume) noexcept { CTL_INF("Absolute file output: " << download_item.localPath.string()); @@ -182,8 +185,7 @@ cpp::result 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() @@ -195,7 +197,7 @@ cpp::result DownloadService::Download( if (answer == "Y" || answer == "y" || answer.empty()) { CLI_LOG("Re-downloading.."); } else { - return cpp::fail("Cancelled Re-download!"); + return false; } } } @@ -232,7 +234,7 @@ cpp::result DownloadService::Download( fclose(file); curl_easy_cleanup(curl); - return {}; + return true; } curl_off_t DownloadService::GetLocalFileSize( diff --git a/engine/services/download_service.h b/engine/services/download_service.h index f73258aad..b1c07fd7f 100644 --- a/engine/services/download_service.h +++ b/engine/services/download_service.h @@ -57,11 +57,11 @@ class DownloadService { using OnDownloadTaskSuccessfully = std::function; - cpp::result AddDownloadTask( + cpp::result AddDownloadTask( DownloadTask& task, std::optional callback = std::nullopt) noexcept; - cpp::result AddAsyncDownloadTask( + cpp::result AddAsyncDownloadTask( DownloadTask& task, std::optional callback = std::nullopt) noexcept; @@ -77,7 +77,7 @@ class DownloadService { cpp::result VerifyDownloadTask( DownloadTask& task) const noexcept; - cpp::result Download( + cpp::result Download( const std::string& download_id, const DownloadItem& download_item, bool allow_resume) noexcept; diff --git a/engine/services/engine_service.cc b/engine/services/engine_service.cc index d809e831e..54c201db9 100644 --- a/engine/services/engine_service.cc +++ b/engine/services/engine_service.cc @@ -116,7 +116,7 @@ std::vector EngineService::GetEngineInfoList() const { return engines; } -cpp::result EngineService::InstallEngine( +cpp::result EngineService::InstallEngine( const std::string& engine, const std::string& version, const std::string& src) { @@ -131,7 +131,7 @@ cpp::result EngineService::InstallEngine( } } -cpp::result EngineService::UnzipEngine( +cpp::result EngineService::UnzipEngine( const std::string& engine, const std::string& version, const std::string& path) { bool found_cuda = false; @@ -193,10 +193,10 @@ cpp::result EngineService::UnzipEngine( return DownloadCuda(engine); } - return {}; + return true; } -cpp::result EngineService::UninstallEngine( +cpp::result EngineService::UninstallEngine( const std::string& engine) { auto ecp = file_manager_utils::GetEnginesContainerPath(); auto engine_path = ecp / engine; @@ -208,14 +208,14 @@ cpp::result 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 EngineService::DownloadEngine( +cpp::result EngineService::DownloadEngine( const std::string& engine, const std::string& version) { auto get_params = [&engine, &version]() -> std::vector { if (version == "latest") { @@ -322,22 +322,22 @@ cpp::result EngineService::DownloadEngine( }); } } - return {}; + return true; } else { return cpp::fail("Failed to fetch engine release: " + engine); } } -cpp::result EngineService::DownloadCuda( +cpp::result 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"; diff --git a/engine/services/engine_service.h b/engine/services/engine_service.h index 8b6417019..102c3b121 100644 --- a/engine/services/engine_service.h +++ b/engine/services/engine_service.h @@ -38,21 +38,21 @@ class EngineService { std::vector GetEngineInfoList() const; - cpp::result InstallEngine( + cpp::result InstallEngine( const std::string& engine, const std::string& version = "latest", const std::string& src = ""); - cpp::result UninstallEngine(const std::string& engine); + cpp::result UninstallEngine(const std::string& engine); private: - cpp::result UnzipEngine(const std::string& engine, + cpp::result UnzipEngine(const std::string& engine, const std::string& version, const std::string& path); - cpp::result DownloadEngine( + cpp::result DownloadEngine( const std::string& engine, const std::string& version = "latest"); - cpp::result DownloadCuda(const std::string& engine); + cpp::result DownloadCuda(const std::string& engine); std::string GetMatchedVariant(const std::string& engine, const std::vector& variants); diff --git a/engine/services/model_service.cc b/engine/services/model_service.cc index 7c55d5adf..3c25dc680 100644 --- a/engine/services/model_service.cc +++ b/engine/services/model_service.cc @@ -242,7 +242,7 @@ cpp::result 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; @@ -295,7 +295,7 @@ cpp::result ModelService::DownloadModelFromCortexso( if (result.has_error()) { return cpp::fail(result.error()); - } else { + } else if (result && result.value()) { CLI_LOG("Model " << model_id << " downloaded successfully!") } @@ -415,7 +415,7 @@ cpp::result 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); @@ -459,7 +459,7 @@ cpp::result 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);