Skip to content

Commit

Permalink
[wip] some update
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Sep 12, 2024
1 parent df77be0 commit a7b2aa9
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 173 deletions.
13 changes: 4 additions & 9 deletions engine/commands/cortex_upd_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,11 @@ bool CortexUpdCmd::GetStableAndBeta(const std::string& v) {
.items = {DownloadItem{
.id = "cortex",
.fileName = file_name,
.type = DownloadType::Cortex,
.path = full_url,
}}}};

DownloadService download_service;
download_service.AddDownloadTask(
download_task,
[this](const std::string& absolute_path, bool unused) {
DownloadService().AddDownloadTask(
download_task, [](const DownloadTask finishedTask) {
// try to unzip the downloaded file
std::filesystem::path download_path{absolute_path};
CTL_INF("Downloaded engine path: " << download_path.string());
Expand Down Expand Up @@ -157,13 +154,11 @@ bool CortexUpdCmd::GetNightly(const std::string& v) {
.items = {DownloadItem{
.id = "cortex",
.fileName = kNightlyFileName,
.type = DownloadType::Cortex,
.path = release_path.str(),
}}};

DownloadService download_service;
download_service.AddDownloadTask(
download_task, [this](const std::string& absolute_path, bool unused) {
DownloadService().AddDownloadTask(
download_task, [](const DownloadTask finishedTask) {
// try to unzip the downloaded file
std::filesystem::path download_path{absolute_path};
CTL_INF("Downloaded engine path: " << download_path.string());
Expand Down
154 changes: 83 additions & 71 deletions engine/commands/engine_init_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
// clang-format on
#include "utils/cuda_toolkit_utils.h"
#include "utils/engine_matcher_utils.h"
#if defined(_WIN32) || defined(__linux__)
#include "utils/file_manager_utils.h"
#endif

namespace commands {

Expand Down Expand Up @@ -95,67 +93,84 @@ bool EngineInitCmd::Exec() const {
auto file_name = asset["name"].get<std::string>();
CTL_INF("URL: " << full_url);

auto downloadTask{DownloadTask{.id = engineName_,
.type = DownloadType::Engine,
.items = {DownloadItem{
.id = engineName_,
.fileName = file_name,
.type = DownloadType::Engine,
.path = full_url,
}}}};
std::filesystem::path engine_folder_path =
file_manager_utils::GetContainerFolderPath(
file_manager_utils::DownloadTypeToString(
DownloadType::Engine)) /
engineName_;

if (!std::filesystem::exists(engine_folder_path)) {
CTL_INF("Creating " << engine_folder_path.string());
std::filesystem::create_directory(engine_folder_path);
}

CTL_INF("Engine folder path: " << engine_folder_path.string()
<< "\n");

auto downloadTask{DownloadTask{
.id = engineName_,
.type = DownloadType::Engine,
.items = {DownloadItem{
.id = engineName_,
.fileName = file_name,
.path = full_url,
.localContainerPath = engine_folder_path.string(),
}}}};

DownloadService download_service;
download_service.AddDownloadTask(downloadTask, [this](
const std::string&
absolute_path,
bool unused) {
// try to unzip the downloaded file
std::filesystem::path downloadedEnginePath{absolute_path};
CTL_INF(
"Downloaded engine path: " << downloadedEnginePath.string());

std::filesystem::path extract_path =
downloadedEnginePath.parent_path().parent_path();

archive_utils::ExtractArchive(downloadedEnginePath.string(),
extract_path.string());
download_service.AddDownloadTask(
downloadTask, [](const DownloadTask finishedTask) {
// try to unzip the downloaded file
std::filesystem::path engine_zip_path =
std::filesystem::path(
finishedTask.items[0].localContainerPath) /
finishedTask.items[0].fileName;
CTL_INF("Engine zip path: " << engine_zip_path.string());

std::filesystem::path extract_path =
engine_zip_path.parent_path().parent_path();

archive_utils::ExtractArchive(engine_zip_path.string(),
extract_path.string());
#if defined(_WIN32) || defined(__linux__)
// FIXME: hacky try to copy the file. Remove this when we are able to set the library path
auto engine_path = extract_path / engineName_;
LOG_INFO << "Source path: " << engine_path.string();
auto executable_path =
file_manager_utils::GetExecutableFolderContainerPath();
for (const auto& entry :
std::filesystem::recursive_directory_iterator(engine_path)) {
if (entry.is_regular_file() &&
entry.path().extension() != ".gz") {
std::filesystem::path relative_path =
std::filesystem::relative(entry.path(), engine_path);
std::filesystem::path destFile =
executable_path / relative_path;

std::filesystem::create_directories(destFile.parent_path());
std::filesystem::copy_file(
entry.path(), destFile,
std::filesystem::copy_options::overwrite_existing);

std::cout << "Copied: " << entry.path().filename().string()
<< " to " << destFile.string() << std::endl;
}
}
std::cout << "DLL copying completed successfully." << std::endl;
// FIXME: hacky try to copy the file. Remove this when we are able to set the library path
auto engine_path = extract_path / engineName_;
LOG_INFO << "Source path: " << engine_path.string();
auto executable_path =
file_manager_utils::GetExecutableFolderContainerPath();
for (const auto& entry :
std::filesystem::recursive_directory_iterator(
engine_path)) {
if (entry.is_regular_file() &&
entry.path().extension() != ".gz") {
std::filesystem::path relative_path =
std::filesystem::relative(entry.path(), engine_path);
std::filesystem::path destFile =
executable_path / relative_path;

std::filesystem::create_directories(
destFile.parent_path());
std::filesystem::copy_file(
entry.path(), destFile,
std::filesystem::copy_options::overwrite_existing);

std::cout
<< "Copied: " << entry.path().filename().string()
<< " to " << destFile.string() << std::endl;
}
}
std::cout << "DLL copying completed successfully."
<< std::endl;
#endif

// remove the downloaded file
// TODO(any) Could not delete file on Windows because it is currently hold by httplib(?)
// Not sure about other platforms
try {
std::filesystem::remove(absolute_path);
} catch (const std::exception& e) {
CTL_WRN("Could not delete file: " << e.what());
}
CTL_INF("Finished!");
});
// remove the downloaded file
try {
std::filesystem::remove(engine_zip_path);
} catch (const std::exception& e) {
CTL_WRN("Could not delete file: " << e.what());
}
CTL_INF("Finished!");
});
if (system_info.os == "mac" || engineName_ == "cortex.onnx") {
// mac and onnx engine does not require cuda toolkit
return true;
Expand Down Expand Up @@ -195,28 +210,25 @@ bool EngineInitCmd::Exec() const {
return false;
}

std::ostringstream cuda_toolkit_path;
cuda_toolkit_path << jan_host << "/" << "dist/cuda-dependencies/"
<< cuda_driver_version << "/" << system_info.os
<< "/" << cuda_toolkit_file_name;
std::ostringstream cuda_toolkit_url;
cuda_toolkit_url << jan_host << "/" << "dist/cuda-dependencies/"
<< cuda_driver_version << "/" << system_info.os
<< "/" << cuda_toolkit_file_name;

LOG_DEBUG << "Cuda toolkit download url: " << jan_host
<< cuda_toolkit_path.str();
<< cuda_toolkit_url.str();

auto downloadCudaToolkitTask{DownloadTask{
.id = download_id,
.type = DownloadType::CudaToolkit,
.items = {DownloadItem{
.id = download_id,
.fileName = cuda_toolkit_file_name,
.type = DownloadType::CudaToolkit,
.path = cuda_toolkit_path.str(),
}},
.items = {DownloadItem{.id = download_id,
.fileName = cuda_toolkit_file_name,
.path = cuda_toolkit_url.str(),
.localContainerPath = "TODO"}},
}};

download_service.AddDownloadTask(
downloadCudaToolkitTask,
[](const std::string& absolute_path, bool unused) {
downloadCudaToolkitTask, [](const DownloadTask finishedTask) {
LOG_DEBUG << "Downloaded cuda path: " << absolute_path;
// try to unzip the downloaded file
std::filesystem::path downloaded_path{absolute_path};
Expand Down
1 change: 0 additions & 1 deletion engine/controllers/engines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ void Engines::InstallEngine(
.items = {DownloadItem{
.id = engine,
.fileName = name,
.type = DownloadType::Engine,
.path = full_url,
}}}};

Expand Down
82 changes: 22 additions & 60 deletions engine/services/download_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
#include <stdio.h>
#include <trantor/utils/Logger.h>
#include <filesystem>
#include <iostream>
#include <thread>

#include "download_service.h"
#include "exceptions/failed_curl_exception.h"
#include "exceptions/failed_init_curl_exception.h"
#include "exceptions/failed_open_file_exception.h"
#include "utils/file_manager_utils.h"
#include "utils/format_utils.h"
#include "utils/logging_utils.h"

void DownloadService::AddDownloadTask(const DownloadTask& task,
std::optional<DownloadItemCb> callback) {

void DownloadService::AddDownloadTask(
const DownloadTask& task,
std::optional<OnDownloadTaskSuccessfully> callback) {
CLI_LOG("Validating download items, please wait..");
// preprocess to check if all the item are valid
auto total_download_size{0};
Expand All @@ -29,14 +26,14 @@ void DownloadService::AddDownloadTask(const DownloadTask& task,
throw;
}
}
auto total_download_size_str{
format_utils::BytesToHumanReadable(total_download_size)};
CLI_LOG("Download items are valid. " << "Total: " << total_download_size_str
<< ". Start downloading..");

// all items are valid, start downloading
for (const auto& item : task.items) {
Download(task.id, item, std::nullopt, callback);
Download(task.id, item);
}

if (callback.has_value()) {
callback.value()(task);
}
}

Expand Down Expand Up @@ -66,77 +63,49 @@ uint64_t DownloadService::GetFileSize(const std::string& url) const {
}

void DownloadService::AddAsyncDownloadTask(
const DownloadTask& task, std::optional<DownloadItemCb> callback) {
tasks.push_back(task);
const DownloadTask& task,
std::optional<OnDownloadTaskSuccessfully> callback) {

for (const auto& item : task.items) {
std::thread([this, task, &callback, item]() {
this->Download(task.id, item, std::nullopt, callback);
this->Download(task.id, item);
}).detach();
}

// TODO: how to call the callback when all the download has finished?
}

size_t WriteCallback(void* ptr, size_t size, size_t nmemb, FILE* stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}

size_t ProgressCallback(void* ptr, curl_off_t totalToDownload,
curl_off_t nowDownloaded, curl_off_t totalUpload,
curl_off_t nowUploaded) {
if (totalToDownload > 0) {
double percentage = (nowDownloaded * 100.0) / totalToDownload;
std::cout << "Downloaded: " << percentage << "%\r" << std::flush;
}
return 0;
}

void DownloadService::Download(
const std::string& download_id, const DownloadItem& download_item,
std::optional<ProgressUpdateCallback> progress_update_callback,
std::optional<DownloadItemCb> download_success_callback) {

// TODO: check the local path inside download_item and create it
auto container_folder_path{file_manager_utils::GetContainerFolderPath(
file_manager_utils::DownloadTypeToString(download_item.type))};
CTL_INF("Container folder path: " << container_folder_path.string() << "\n");

auto item_folder_path{container_folder_path /
std::filesystem::path(download_id)};
CTL_INF("itemFolderPath: " << item_folder_path.string());
// TODO: should move this out of this function
if (!std::filesystem::exists(item_folder_path)) {
CTL_INF("Creating " << item_folder_path.string());
std::filesystem::create_directory(item_folder_path);
}
void DownloadService::Download(const std::string& download_id,
const DownloadItem& download_item) {
std::filesystem::path ofp =
std::filesystem::path(download_item.localContainerPath) /
download_item.fileName;

auto output_file_path{item_folder_path /
std::filesystem::path(download_item.fileName)};
CTL_INF("Absolute file output: " << output_file_path.string());
CTL_INF("Absolute file output: " << ofp.string().c_str());

CURL* curl;
FILE* file;
CURLcode res;

size_t (*writeCallbackPtr)(void*, size_t, size_t, FILE*) = &WriteCallback;
size_t (*progressCallbackPtr)(void*, curl_off_t, curl_off_t, curl_off_t,
curl_off_t) = &ProgressCallback;

curl = curl_easy_init();
if (!curl) {
throw FailedInitCurlException();
}

file = fopen(output_file_path.string().c_str(), "wb");
file = fopen(ofp.string().c_str(), "wb");
if (!file) {
auto err_msg = "Failed to open output file " + output_file_path.string();
auto err_msg{"Failed to open output file " + ofp.string()};
throw FailedOpenFileException(err_msg);
}

curl_easy_setopt(curl, CURLOPT_URL, download_item.path.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallbackPtr);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progressCallbackPtr);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

Expand All @@ -149,11 +118,4 @@ void DownloadService::Download(

fclose(file);
curl_easy_cleanup(curl);
if (download_success_callback.has_value()) {
// TODO: this should not be here
auto need_parse_gguf =
download_item.path.find("cortexso") == std::string::npos;
download_success_callback.value()(output_file_path.string(),
need_parse_gguf);
}
}
Loading

0 comments on commit a7b2aa9

Please sign in to comment.