From e3371dd516f8d0e033081615be70aa3994614851 Mon Sep 17 00:00:00 2001 From: Santiago Vendramini Date: Thu, 5 Dec 2024 10:10:17 +0100 Subject: [PATCH] feat: Added functions to set the result for IN_PROGRESS commands The necessary functions were added to mark as not executed the in-progress commands that remained after a restart. Additionally, the structure of the command result in a stateful event was updated. --- .../include/command_handler.hpp | 18 ++++++++++++++ src/agent/src/agent.cpp | 24 +++++++++++++++++++ src/agent/src/command_handler_utils.cpp | 12 ++++++---- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/agent/command_handler/include/command_handler.hpp b/src/agent/command_handler/include/command_handler.hpp index f5fe488773..e0629049e1 100644 --- a/src/agent/command_handler/include/command_handler.hpp +++ b/src/agent/command_handler/include/command_handler.hpp @@ -72,6 +72,24 @@ namespace command_handler } } + void CleanUpInProgressCommands( + std::function>&)> callback) + { + auto cmds = m_commandStore.GetCommandByStatus(module_command::Status::IN_PROGRESS); + + if (cmds != std::nullopt) + { + for (auto& cmd : *cmds) + { + cmd.ExecutionResult.ErrorCode = module_command::Status::UNKNOWN; + cmd.ExecutionResult.Message = "Agent stopped during execution"; + m_commandStore.UpdateCommand(cmd); + } + } + + callback(cmds); + } + /// @brief Stops the command handler void Stop(); diff --git a/src/agent/src/agent.cpp b/src/agent/src/agent.cpp index a526ba1072..1856821c7d 100644 --- a/src/agent/src/agent.cpp +++ b/src/agent/src/agent.cpp @@ -42,6 +42,30 @@ Agent::Agent(const std::string& configFilePath, std::unique_ptr m_taskManager.Start( m_configurationParser.GetConfig("agent", "thread_count").value_or(config::DEFAULT_THREAD_COUNT)); + + m_commandHandler.CleanUpInProgressCommands( + [this](const auto& inProgressCommands) + { + if (inProgressCommands != std::nullopt) + { + for (auto& cmd : *inProgressCommands) + { + auto metadata = nlohmann::json::object(); + metadata["module"] = "command"; + metadata["id"] = cmd.Id; + metadata["operation"] = "update"; + + nlohmann::json commandJson; + commandJson["command"]["result"]["code"] = cmd.ExecutionResult.ErrorCode; + commandJson["command"]["result"]["message"] = cmd.ExecutionResult.Message; + + const Message message { + MessageType::STATEFUL, {commandJson}, metadata["module"], "", metadata.dump()}; + // controlar resultado del push + m_messageQueue->push(message); + } + } + }); } Agent::~Agent() diff --git a/src/agent/src/command_handler_utils.cpp b/src/agent/src/command_handler_utils.cpp index 623e33164f..937022ec52 100644 --- a/src/agent/src/command_handler_utils.cpp +++ b/src/agent/src/command_handler_utils.cpp @@ -83,12 +83,16 @@ DispatchCommand(module_command::CommandEntry commandEntry, co_await (TimerTask(timer, result, commandCompleted) || ExecuteCommandTask(executeFunction, commandEntry, result, commandCompleted, timer)); + auto metadata = nlohmann::json::object(); + metadata["module"] = "command"; + metadata["id"] = commandEntry.Id; + metadata["operation"] = "update"; + nlohmann::json resultJson; - resultJson["error"] = result->ErrorCode; - resultJson["message"] = result->Message; - resultJson["id"] = commandEntry.Id; + resultJson["command"]["result"]["code"] = result->ErrorCode; + resultJson["command"]["result"]["message"] = result->Message; - Message message {MessageType::STATEFUL, {resultJson}, "CommandHandler"}; + Message message {MessageType::STATEFUL, {resultJson}, metadata["module"], "", metadata.dump()}; messageQueue->push(message); co_return *result;