Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions engine/commands/chat_cmd.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#include "chat_cmd.h"
#include "httplib.h"

#include "database/models.h"
#include "model_status_cmd.h"
#include "server_start_cmd.h"
#include "trantor/utils/Logger.h"
#include "utils/logging_utils.h"
#include "run_cmd.h"

namespace commands {
void ChatCmd::Exec(const std::string& host, int port,
const std::string& model_handle) {
RunCmd rc(host, port, model_handle);
const std::string& model_handle,
std::shared_ptr<DownloadService> download_service) {
RunCmd rc(host, port, model_handle, download_service);
rc.Exec(true /*chat_flag*/);
}
}; // namespace commands
}; // namespace commands
7 changes: 5 additions & 2 deletions engine/commands/chat_cmd.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#include <string>
#include "services/download_service.h"

namespace commands {
class ChatCmd {
public:
void Exec(const std::string& host, int port, const std::string& model_handle);
void Exec(const std::string& host, int port, const std::string& model_handle,
std::shared_ptr<DownloadService> download_service);
};
} // namespace commands
} // namespace commands
4 changes: 2 additions & 2 deletions engine/commands/engine_get_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace commands {
class EngineGetCmd {
public:
explicit EngineGetCmd()
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};
explicit EngineGetCmd(std::shared_ptr<DownloadService> download_service)
: engine_service_{EngineService(download_service)} {};

void Exec(const std::string& engineName) const;

Expand Down
4 changes: 2 additions & 2 deletions engine/commands/engine_install_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace commands {

class EngineInstallCmd {
public:
explicit EngineInstallCmd()
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};
explicit EngineInstallCmd(std::shared_ptr<DownloadService> download_service)
: engine_service_{EngineService(download_service)} {};

void Exec(const std::string& engine, const std::string& version = "latest",
const std::string& src = "");
Expand Down
4 changes: 2 additions & 2 deletions engine/commands/engine_list_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace commands {
class EngineListCmd {
public:
explicit EngineListCmd()
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};
explicit EngineListCmd(std::shared_ptr<DownloadService> download_service)
: engine_service_{EngineService(download_service)} {};

bool Exec();

Expand Down
4 changes: 2 additions & 2 deletions engine/commands/engine_uninstall_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace commands {
class EngineUninstallCmd {
public:
explicit EngineUninstallCmd()
: engine_service_{EngineService(std::make_shared<DownloadService>())} {};
explicit EngineUninstallCmd(std::shared_ptr<DownloadService> download_service)
: engine_service_{EngineService(download_service)} {};

void Exec(const std::string& engine);

Expand Down
4 changes: 2 additions & 2 deletions engine/commands/model_del_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace commands {

class ModelDelCmd {
public:
explicit ModelDelCmd()
: model_service_{ModelService(std::make_shared<DownloadService>())} {};
explicit ModelDelCmd(std::shared_ptr<DownloadService> download_service)
: model_service_{ModelService(download_service)} {};

void Exec(const std::string& model_handle);

Expand Down
4 changes: 2 additions & 2 deletions engine/commands/model_pull_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace commands {

class ModelPullCmd {
public:
explicit ModelPullCmd()
: model_service_{ModelService(std::make_shared<DownloadService>())} {};
explicit ModelPullCmd(std::shared_ptr<DownloadService> download_service)
: model_service_{ModelService(download_service)} {};
void Exec(const std::string& input);

private:
Expand Down
7 changes: 4 additions & 3 deletions engine/commands/run_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
namespace commands {
class RunCmd {
public:
explicit RunCmd(std::string host, int port, std::string model_handle)
explicit RunCmd(std::string host, int port, std::string model_handle,
std::shared_ptr<DownloadService> download_service)
: host_{std::move(host)},
port_{port},
model_handle_{std::move(model_handle)},
engine_service_{EngineService(std::make_shared<DownloadService>())},
model_service_{ModelService(std::make_shared<DownloadService>())} {};
engine_service_{EngineService(download_service)},
model_service_{ModelService(download_service)} {};

void Exec(bool chat_flag);

Expand Down
18 changes: 9 additions & 9 deletions engine/controllers/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void CommandLineParser::SetupCommonCommands() {
return;
}
try {
commands::ModelPullCmd().Exec(cml_data_.model_id);
commands::ModelPullCmd(download_service_).Exec(cml_data_.model_id);
} catch (const std::exception& e) {
CLI_LOG(e.what());
}
Expand All @@ -150,7 +150,7 @@ void CommandLineParser::SetupCommonCommands() {
}
commands::RunCmd rc(cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort),
cml_data_.model_id);
cml_data_.model_id, download_service_);
rc.Exec(cml_data_.chat_flag);
});

Expand All @@ -175,7 +175,7 @@ void CommandLineParser::SetupCommonCommands() {
if (cml_data_.msg.empty()) {
commands::ChatCmd().Exec(cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort),
cml_data_.model_id);
cml_data_.model_id, download_service_);
} else {
commands::ChatCompletionCmd(model_service_)
.Exec(cml_data_.config.apiServerHost,
Expand Down Expand Up @@ -285,7 +285,7 @@ void CommandLineParser::SetupModelCommands() {
CLI_LOG(model_del_cmd->help());
return;
};
commands::ModelDelCmd().Exec(cml_data_.model_id);
commands::ModelDelCmd(download_service_).Exec(cml_data_.model_id);
});

std::string model_alias;
Expand Down Expand Up @@ -358,8 +358,7 @@ void CommandLineParser::SetupEngineCommands() {
list_engines_cmd->callback([this]() {
if (std::exchange(executed_, true))
return;
commands::EngineListCmd command;
command.Exec();
commands::EngineListCmd(download_service_).Exec();
});

auto install_cmd = engines_cmd->add_subcommand("install", "Install engine");
Expand Down Expand Up @@ -478,7 +477,8 @@ void CommandLineParser::EngineInstall(CLI::App* parent,
if (std::exchange(executed_, true))
return;
try {
commands::EngineInstallCmd().Exec(engine_name, version, src);
commands::EngineInstallCmd(download_service_)
.Exec(engine_name, version, src);
} catch (const std::exception& e) {
CTL_ERR(e.what());
}
Expand All @@ -496,7 +496,7 @@ void CommandLineParser::EngineUninstall(CLI::App* parent,
if (std::exchange(executed_, true))
return;
try {
commands::EngineUninstallCmd().Exec(engine_name);
commands::EngineUninstallCmd(download_service_).Exec(engine_name);
} catch (const std::exception& e) {
CTL_ERR(e.what());
}
Expand Down Expand Up @@ -528,7 +528,7 @@ void CommandLineParser::EngineGet(CLI::App* parent) {
engine_get_cmd->callback([this, engine_name] {
if (std::exchange(executed_, true))
return;
commands::EngineGetCmd().Exec(engine_name);
commands::EngineGetCmd(download_service_).Exec(engine_name);
});
}
}
Expand Down
1 change: 1 addition & 0 deletions engine/controllers/command_line_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "services/engine_service.h"
#include "services/model_service.h"
#include "utils/config_yaml_utils.h"

class CommandLineParser {
public:
CommandLineParser();
Expand Down
8 changes: 0 additions & 8 deletions engine/controllers/process_manager.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "process_manager.h"
#include "utils/cortex_utils.h"
#include "utils/logging_utils.h"

#include <trantor/utils/Logger.h>
#include <cstdlib>
Expand All @@ -9,13 +8,6 @@ void ProcessManager::destroy(
const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback) {

auto destroy_result = download_service_->Destroy();
if (destroy_result.has_error()) {
CTL_ERR("Failed to destroy download service: " + destroy_result.error());
} else {
CTL_INF("Download service stopped!");
}

app().quit();
Json::Value ret;
ret["message"] = "Program is exitting, goodbye!";
Expand Down
7 changes: 0 additions & 7 deletions engine/controllers/process_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <drogon/HttpController.h>
#include <drogon/HttpTypes.h>
#include "services/download_service.h"

using namespace drogon;

Expand All @@ -12,12 +11,6 @@ class ProcessManager : public drogon::HttpController<ProcessManager, false> {
METHOD_ADD(ProcessManager::destroy, "/destroy", Delete);
METHOD_LIST_END

explicit ProcessManager(std::shared_ptr<DownloadService> download_service)
: download_service_{download_service} {}

void destroy(const HttpRequestPtr& req,
std::function<void(const HttpResponsePtr&)>&& callback);

private:
std::shared_ptr<DownloadService> download_service_;
};
6 changes: 3 additions & 3 deletions engine/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void RunServer() {
std::filesystem::create_directories(
std::filesystem::path(config.logFolderPath) /
std::filesystem::path(cortex_utils::logs_folder));
trantor::FileLogger asyncFileLogger;
static trantor::FileLogger asyncFileLogger;
asyncFileLogger.setFileName(
(std::filesystem::path(config.logFolderPath) /
std::filesystem::path(cortex_utils::logs_base_name))
Expand Down Expand Up @@ -93,7 +93,7 @@ void RunServer() {
auto engine_ctl = std::make_shared<Engines>(engine_service);
auto model_ctl = std::make_shared<Models>(model_service);
auto event_ctl = std::make_shared<Events>(event_queue_ptr);
auto pm_ctl = std::make_shared<ProcessManager>(download_service);
auto pm_ctl = std::make_shared<ProcessManager>();

drogon::app().registerController(engine_ctl);
drogon::app().registerController(model_ctl);
Expand Down Expand Up @@ -126,7 +126,7 @@ int main(int argc, char* argv[]) {
if (strcmp(argv[i], "--config_file_path") == 0) {
file_manager_utils::cortex_config_file_path = argv[i + 1];

} else if(strcmp(argv[i], "--data_folder_path") == 0) {
} else if (strcmp(argv[i], "--data_folder_path") == 0) {
file_manager_utils::cortex_data_folder_path = argv[i + 1];
}
}
Expand Down
19 changes: 7 additions & 12 deletions engine/services/download_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,12 @@ void DownloadService::ProcessTask(DownloadTask& task) {
}
} while (still_running);

ProcessCompletedTransfers();
if (stop_flag_) {
CTL_INF("Download service is stopping..");
return;
}

ProcessCompletedTransfers();
for (auto handle : task_handles) {
curl_multi_remove_handle(multi_handle_, handle);
curl_easy_cleanup(handle);
Expand All @@ -304,14 +308,12 @@ void DownloadService::ProcessTask(DownloadTask& task) {

// if terminate by API calling and not from process stopping, we emit
// DownloadStopped event
if (is_terminated && !stop_flag_) {
if (is_terminated) {
event_queue_->enqueue(
EventType::DownloadEvent,
DownloadEvent{.type_ = DownloadEventType::DownloadStopped,
.download_task_ = task});
}

if (!is_terminated) {
} else {
RemoveTaskFromStopList(task.id);
CTL_INF("Executing callback..");
ExecuteCallback(task);
Expand Down Expand Up @@ -402,10 +404,3 @@ void DownloadService::ExecuteCallback(const DownloadTask& task) {
callbacks_.erase(it);
}
}

cpp::result<void, std::string> DownloadService::Destroy() {
CTL_INF("Destroying download service..");
stop_flag_ = true;
queue_condition_.notify_one();
return {};
}
5 changes: 3 additions & 2 deletions engine/services/download_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class DownloadService {
}
}

DownloadService(const DownloadService&) = delete;
DownloadService& operator=(const DownloadService&) = delete;

/**
* Adding new download task to the queue. Asynchronously. This function should
* be used by HTTP API.
Expand All @@ -74,8 +77,6 @@ class DownloadService {

cpp::result<void, std::string> StopTask(const std::string& task_id);

cpp::result<void, std::string> Destroy();

private:
struct DownloadingData {
std::string item_id;
Expand Down
Loading