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

Commit 0ebada2

Browse files
fix: cache total bytes for download event (#1598)
* fix: cache total bytes for download event * fix: log --------- Co-authored-by: vansangpfiev <sang@jan.ai>
1 parent 26f3d6f commit 0ebada2

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

engine/cli/commands/engine_install_cmd.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "engine_install_cmd.h"
2+
#include <future>
23
#include "server_start_cmd.h"
34
#include "utils/download_progress.h"
45
#include "utils/engine_constants.h"
@@ -31,9 +32,16 @@ bool EngineInstallCmd::Exec(const std::string& engine,
3132
}
3233
}
3334

35+
DownloadProgress dp;
36+
dp.Connect(host_, port_);
37+
// engine can be small, so need to start ws first
38+
auto dp_res = std::async(std::launch::deferred,
39+
[&dp, &engine] { return dp.Handle(engine); });
40+
CLI_LOG("Validating download items, please wait..")
41+
3442
httplib::Client cli(host_ + ":" + std::to_string(port_));
3543
Json::Value json_data;
36-
json_data["version"] = version.empty() ? "latest" : version;
44+
json_data["version"] = version.empty() ? "latest" : version;
3745
auto data_str = json_data.toStyledString();
3846
cli.set_read_timeout(std::chrono::seconds(60));
3947
auto res = cli.Post("/v1/engines/install/" + engine, httplib::Headers(),
@@ -43,18 +51,19 @@ bool EngineInstallCmd::Exec(const std::string& engine,
4351
if (res->status != httplib::StatusCode::OK_200) {
4452
auto root = json_helper::ParseJsonString(res->body);
4553
CLI_LOG(root["message"].asString());
54+
dp.ForceStop();
4655
return false;
56+
} else {
57+
CLI_LOG("Start downloading..");
4758
}
4859
} else {
4960
auto err = res.error();
5061
CTL_ERR("HTTP error: " << httplib::to_string(err));
62+
dp.ForceStop();
5163
return false;
5264
}
5365

54-
CLI_LOG("Start downloading ...")
55-
DownloadProgress dp;
56-
dp.Connect(host_, port_);
57-
if (!dp.Handle(engine))
66+
if (!dp_res.get())
5867
return false;
5968

6069
bool check_cuda_download = !system_info_utils::GetCudaVersion().empty();

engine/cli/commands/model_pull_cmd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ std::optional<std::string> ModelPullCmd::Exec(const std::string& host, int port,
122122
return std::nullopt;
123123
}
124124

125-
CLI_LOG("Start downloading ...")
125+
CLI_LOG("Start downloading..")
126126
DownloadProgress dp;
127127
bool force_stop = false;
128128

engine/cli/utils/download_progress.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ bool DownloadProgress::Connect(const std::string& host, int port) {
2323

2424
bool DownloadProgress::Handle(const std::string& id) {
2525
assert(!!ws_);
26+
uint64_t total = std::numeric_limits<uint64_t>::max();
2627
status_ = DownloadStatus::DownloadStarted;
2728
std::unique_ptr<indicators::DynamicProgress<indicators::ProgressBar>> bars;
2829

2930
std::vector<std::unique_ptr<indicators::ProgressBar>> items;
3031
indicators::show_console_cursor(false);
31-
auto handle_message = [this, &bars, &items, id](const std::string& message) {
32+
auto handle_message = [this, &bars, &items, &total,
33+
id](const std::string& message) {
3234
CTL_INF(message);
3335

3436
auto pad_string = [](const std::string& str,
@@ -70,7 +72,10 @@ bool DownloadProgress::Handle(const std::string& id) {
7072
for (int i = 0; i < ev.download_task_.items.size(); i++) {
7173
auto& it = ev.download_task_.items[i];
7274
uint64_t downloaded = it.downloadedBytes.value_or(0);
73-
uint64_t total = it.bytes.value_or(std::numeric_limits<uint64_t>::max());
75+
if (total == 0 || total == std::numeric_limits<uint64_t>::max()) {
76+
total = it.bytes.value_or(std::numeric_limits<uint64_t>::max());
77+
CTL_INF("Updated - total: " << total);
78+
}
7479
if (ev.type_ == DownloadStatus::DownloadUpdated) {
7580
(*bars)[i].set_option(indicators::option::PrefixText{
7681
pad_string(it.id) +

0 commit comments

Comments
 (0)