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

Commit 916e29f

Browse files
authored
Merge pull request #1678 from janhq/j/add-load-unload-cli
feat: add load/unload engine cli
2 parents 3d02299 + 4dbca80 commit 916e29f

File tree

11 files changed

+235
-123
lines changed

11 files changed

+235
-123
lines changed

engine/cli/command_line_parser.cc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include "commands/engine_get_cmd.h"
1010
#include "commands/engine_install_cmd.h"
1111
#include "commands/engine_list_cmd.h"
12+
#include "commands/engine_load_cmd.h"
1213
#include "commands/engine_uninstall_cmd.h"
14+
#include "commands/engine_unload_cmd.h"
1315
#include "commands/engine_update_cmd.h"
1416
#include "commands/engine_use_cmd.h"
1517
#include "commands/hardware_activate_cmd.h"
@@ -474,6 +476,41 @@ void CommandLineParser::SetupEngineCommands() {
474476
EngineUse(engine_use_cmd, engine_name);
475477
}
476478

479+
auto engine_load_cmd = engines_cmd->add_subcommand("load", "Load engine");
480+
engine_load_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
481+
" engines load [engine_name]");
482+
engine_load_cmd->callback([this, engine_load_cmd] {
483+
if (std::exchange(executed_, true))
484+
return;
485+
if (engine_load_cmd->get_subcommands().empty()) {
486+
CLI_LOG("[engine_name] is required\n");
487+
CLI_LOG(engine_load_cmd->help());
488+
}
489+
});
490+
engine_load_cmd->group(kSubcommands);
491+
for (auto& engine : engine_service_.kSupportEngines) {
492+
std::string engine_name{engine};
493+
EngineLoad(engine_load_cmd, engine_name);
494+
}
495+
496+
auto engine_unload_cmd =
497+
engines_cmd->add_subcommand("unload", "Unload engine");
498+
engine_unload_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
499+
" engines unload [engine_name]");
500+
engine_unload_cmd->callback([this, engine_unload_cmd] {
501+
if (std::exchange(executed_, true))
502+
return;
503+
if (engine_unload_cmd->get_subcommands().empty()) {
504+
CLI_LOG("[engine_name] is required\n");
505+
CLI_LOG(engine_unload_cmd->help());
506+
}
507+
});
508+
engine_unload_cmd->group(kSubcommands);
509+
for (auto& engine : engine_service_.kSupportEngines) {
510+
std::string engine_name{engine};
511+
EngineUnload(engine_unload_cmd, engine_name);
512+
}
513+
477514
EngineGet(engines_cmd);
478515
}
479516

@@ -691,6 +728,44 @@ void CommandLineParser::EngineUpdate(CLI::App* parent,
691728
});
692729
}
693730

731+
void CommandLineParser::EngineUnload(CLI::App* parent,
732+
const std::string& engine_name) {
733+
auto sub_cmd = parent->add_subcommand(engine_name, "");
734+
sub_cmd->usage("Usage:\n" + commands::GetCortexBinary() + " engines unload " +
735+
engine_name);
736+
sub_cmd->group(kEngineGroup);
737+
738+
sub_cmd->callback([this, engine_name] {
739+
if (std::exchange(executed_, true))
740+
return;
741+
auto result = commands::EngineUnloadCmd().Exec(
742+
cml_data_.config.apiServerHost,
743+
std::stoi(cml_data_.config.apiServerPort), engine_name);
744+
if (result.has_error()) {
745+
CTL_ERR(result.error());
746+
}
747+
});
748+
}
749+
750+
void CommandLineParser::EngineLoad(CLI::App* parent,
751+
const std::string& engine_name) {
752+
auto sub_cmd = parent->add_subcommand(engine_name, "");
753+
sub_cmd->usage("Usage:\n" + commands::GetCortexBinary() + " engines load " +
754+
engine_name);
755+
sub_cmd->group(kEngineGroup);
756+
757+
sub_cmd->callback([this, engine_name] {
758+
if (std::exchange(executed_, true))
759+
return;
760+
auto result = commands::EngineLoadCmd().Exec(
761+
cml_data_.config.apiServerHost,
762+
std::stoi(cml_data_.config.apiServerPort), engine_name);
763+
if (result.has_error()) {
764+
CTL_ERR(result.error());
765+
}
766+
});
767+
}
768+
694769
void CommandLineParser::EngineUse(CLI::App* parent,
695770
const std::string& engine_name) {
696771
auto engine_use_cmd = parent->add_subcommand(engine_name, "");

engine/cli/command_line_parser.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
#include <memory>
44
#include <unordered_map>
55
#include "CLI/CLI.hpp"
6+
#include "commands/hardware_list_cmd.h"
67
#include "services/engine_service.h"
78
#include "services/model_service.h"
89
#include "utils/config_yaml_utils.h"
9-
#include "commands/hardware_list_cmd.h"
10-
#include "common/hardware_config.h"
1110

1211
class CommandLineParser {
1312
public:
@@ -40,6 +39,10 @@ class CommandLineParser {
4039

4140
void EngineUse(CLI::App* parent, const std::string& engine_name);
4241

42+
void EngineLoad(CLI::App* parent, const std::string& engine_name);
43+
44+
void EngineUnload(CLI::App* parent, const std::string& engine_name);
45+
4346
void ModelUpdate(CLI::App* parent);
4447

4548
CLI::App app_;
@@ -66,7 +69,6 @@ class CommandLineParser {
6669

6770
bool show_menu = false;
6871

69-
7072
int port;
7173
config_yaml_utils::CortexConfig config;
7274
std::unordered_map<std::string, std::string> model_update_options;

engine/cli/commands/config_upd_cmd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void commands::ConfigUpdCmd::Exec(
6363
}
6464

6565
auto update_cnf_result =
66-
curl_utils::SimplePatch(url.ToFullPath(), json.toStyledString());
66+
curl_utils::SimplePatchJson(url.ToFullPath(), json.toStyledString());
6767
if (update_cnf_result.has_error()) {
6868
CLI_LOG_ERROR(update_cnf_result.error());
6969
return;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "engine_load_cmd.h"
2+
#include "server_start_cmd.h"
3+
#include "utils/cli_selection_utils.h"
4+
#include "utils/curl_utils.h"
5+
#include "utils/logging_utils.h"
6+
#include "utils/url_parser.h"
7+
8+
namespace commands {
9+
cpp::result<void, std::string> EngineLoadCmd::Exec(const std::string& host,
10+
int port,
11+
const std::string& engine) {
12+
// Start server if server is not started yet
13+
if (!commands::IsServerAlive(host, port)) {
14+
CLI_LOG("Starting server ...");
15+
commands::ServerStartCmd ssc;
16+
if (!ssc.Exec(host, port)) {
17+
return cpp::fail("Failed to start server");
18+
}
19+
}
20+
21+
auto load_engine_url = url_parser::Url{
22+
.protocol = "http",
23+
.host = host + ":" + std::to_string(port),
24+
.pathParams = {"v1", "engines", engine, "load"},
25+
};
26+
auto load_engine_result =
27+
curl_utils::SimplePostJson(load_engine_url.ToFullPath());
28+
if (load_engine_result.has_error()) {
29+
CTL_ERR(load_engine_result.error());
30+
return cpp::fail("Failed to load engine: " + load_engine_result.error());
31+
}
32+
33+
CLI_LOG("Engine loaded successfully");
34+
return {};
35+
}
36+
}; // namespace commands
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include "utils/result.hpp"
5+
6+
namespace commands {
7+
8+
class EngineLoadCmd {
9+
public:
10+
cpp::result<void, std::string> Exec(const std::string& host, int port,
11+
const std::string& engine);
12+
};
13+
} // namespace commands

engine/cli/commands/engine_uninstall_cmd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void EngineUninstallCmd::Exec(const std::string& host, int port,
2121
.host = host + ":" + std::to_string(port),
2222
.pathParams = {"v1", "engines", engine}};
2323

24-
auto result = curl_utils::SimpleDelete(url.ToFullPath());
24+
auto result = curl_utils::SimpleDeleteJson(url.ToFullPath());
2525
if (result.has_error()) {
2626
CTL_ERR(result.error());
2727
return;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "engine_unload_cmd.h"
2+
#include "server_start_cmd.h"
3+
#include "utils/cli_selection_utils.h"
4+
#include "utils/curl_utils.h"
5+
#include "utils/logging_utils.h"
6+
#include "utils/url_parser.h"
7+
8+
namespace commands {
9+
cpp::result<void, std::string> EngineUnloadCmd::Exec(
10+
const std::string& host, int port, const std::string& engine) {
11+
// Start server if server is not started yet
12+
if (!commands::IsServerAlive(host, port)) {
13+
CLI_LOG("Starting server ...");
14+
commands::ServerStartCmd ssc;
15+
if (!ssc.Exec(host, port)) {
16+
return cpp::fail("Failed to start server");
17+
}
18+
}
19+
20+
auto load_engine_url = url_parser::Url{
21+
.protocol = "http",
22+
.host = host + ":" + std::to_string(port),
23+
.pathParams = {"v1", "engines", engine, "load"},
24+
};
25+
auto load_engine_result =
26+
curl_utils::SimpleDeleteJson(load_engine_url.ToFullPath());
27+
if (load_engine_result.has_error()) {
28+
CTL_ERR(load_engine_result.error());
29+
return cpp::fail("Failed to unload engine: " + load_engine_result.error());
30+
}
31+
32+
CLI_LOG("Engine unloaded successfully");
33+
return {};
34+
}
35+
}; // namespace commands
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include "utils/result.hpp"
5+
6+
namespace commands {
7+
8+
class EngineUnloadCmd {
9+
public:
10+
cpp::result<void, std::string> Exec(const std::string& host, int port,
11+
const std::string& engine);
12+
};
13+
} // namespace commands

engine/common/engine_servicei.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
2+
3+
#include <json/value.h>
24
#include <string>
35
#include <vector>
4-
#include "json/json.h"
56
#include "utils/result.hpp"
67

78
// TODO: namh think of the other name
@@ -37,12 +38,12 @@ struct EngineVariantResponse {
3738
class EngineServiceI {
3839
public:
3940
virtual ~EngineServiceI() {}
40-
41+
4142
virtual cpp::result<DefaultEngineVariant, std::string>
4243
SetDefaultEngineVariant(const std::string& engine, const std::string& version,
4344
const std::string& variant) = 0;
4445

45-
virtual cpp::result<DefaultEngineVariant, std::string>
46+
virtual cpp::result<DefaultEngineVariant, std::string>
4647
GetDefaultEngineVariant(const std::string& engine) = 0;
4748

4849
virtual cpp::result<std::vector<EngineVariantResponse>, std::string>
@@ -53,5 +54,4 @@ virtual cpp::result<DefaultEngineVariant, std::string>
5354

5455
virtual cpp::result<void, std::string> UnloadEngine(
5556
const std::string& engine_name) = 0;
56-
57-
};
57+
};

engine/controllers/engines.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ void Engines::SetDefaultEngineVariant(
229229
resp->setStatusCode(k400BadRequest);
230230
callback(resp);
231231
} else {
232-
auto resp =
233-
cortex_utils::CreateCortexHttpJsonResponse(result.value().ToJson());
232+
Json::Value res;
233+
res["message"] = "Engine " + result.value().variant + " " +
234+
result.value().version + " set as default";
235+
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
234236
resp->setStatusCode(k200OK);
235237
callback(resp);
236238
}

0 commit comments

Comments
 (0)