forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_update_cmd.cc
More file actions
61 lines (55 loc) · 1.8 KB
/
engine_update_cmd.cc
File metadata and controls
61 lines (55 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "engine_update_cmd.h"
#include <future>
#include "server_start_cmd.h"
#include "utils/cli_selection_utils.h"
#include "utils/curl_utils.h"
#include "utils/download_progress.h"
#include "utils/json_helper.h"
#include "utils/logging_utils.h"
#include "utils/system_info_utils.h"
#include "utils/url_parser.h"
namespace commands {
bool EngineUpdateCmd::Exec(const std::string& host, int port,
const std::string& engine) {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return false;
}
}
DownloadProgress dp;
dp.Connect(host, port);
// engine can be small, so need to start ws first
auto dp_res = std::async(std::launch::deferred, [&dp] {
bool need_cuda_download =
!system_info_utils::GetDriverAndCudaVersion().second.empty();
if (need_cuda_download) {
return dp.Handle({DownloadType::Engine, DownloadType::CudaToolkit});
} else {
return dp.Handle({DownloadType::Engine});
}
});
auto update_url = url_parser::Url{
/* .protocol = */ "http",
/* .host = */ host + ":" + std::to_string(port),
/* .pathParams = */ {"v1", "engines", engine, "update"},
/* .queries = */ {},
};
auto update_result = curl_utils::SimplePostJson(update_url.ToFullPath());
if (update_result.has_error()) {
try {
Json::Value json = json_helper::ParseJsonString(update_result.error());
std::cout << json["message"].asString() << std::endl;
} catch (const std::exception&) {
CTL_ERR(update_result.error());
}
return false;
}
if (!dp_res.get())
return false;
CLI_LOG("Engine " << engine << " updated successfully!")
return true;
}
}; // namespace commands