Skip to content

Commit

Permalink
feat: Created a task to push a command to the agent queue to test the…
Browse files Browse the repository at this point in the history
… command manager
  • Loading branch information
lchico committed Aug 19, 2024
1 parent ddd4c8a commit 3f9c2b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/agent/command_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ set(VCPKG_MANIFEST_DIR ${CMAKE_SOURCE_DIR}/../../)

project(CommandManager)

find_package(Boost REQUIRED COMPONENTS asio beast)

add_library(CommandManager src/command_manager.cpp)
target_include_directories(CommandManager PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(CommandManager PUBLIC Boost::asio PRIVATE Boost::beast )

if(BUILD_TESTS)
enable_testing()
Expand Down
39 changes: 27 additions & 12 deletions src/agent/src/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ void Agent::Run()
// {"status", "Pending"}};
//

const nlohmann::json dataContent = {{"command", "upgradeModule"}, {"status", "Pending"}};
const Message messageToSend {MessageType::COMMAND, dataContent, "CommandManager"};
m_agentQueue.push(messageToSend);

m_taskManager.EnqueueTask(m_communicator.WaitForTokenExpirationAndAuthenticate());

m_taskManager.EnqueueTask(m_communicator.GetCommandsFromManager(
Expand All @@ -46,6 +42,25 @@ void Agent::Run()
[this]() { return getMessagesFromQueue(m_messageQueue, STATELESS); },
[this]([[maybe_unused]] const std::string& response) { popMessagesFromQueue(m_messageQueue, STATELESS); }));


// Push message to Agent Queue every 100 seconds.
m_taskManager.EnqueueTask([this]() -> boost::asio::awaitable<void> {

while (true) {
// Push the message
const nlohmann::json dataContent = {{"command", "upgradeModule"}, {"status", "Pending"}};
std::cout << "--------------------------------" << std::endl;
std::cout << "Push command to the agent queue:" << std::endl;
std::cout << dataContent << std::endl;
std::cout << "--------------------------------" << std::endl;

const Message messageToSend {MessageType::COMMAND, dataContent, "CommandManager"};
m_agentQueue.push(messageToSend);
std::this_thread::sleep_for(std::chrono::seconds(100));
}
});


m_taskManager.EnqueueTask(m_commandManager.ProcessCommandsFromQueue<Message>(
[this]() -> std::optional<Message>
{
Expand Down Expand Up @@ -75,14 +90,14 @@ void Agent::Run()
}

// change status and push again
// if (jdataData.at("status") == "Pending")
//{
// std::cout << "Message status is Pending. Updating status and pushing back." << std::endl;
// jdataData["status"] = "InProcess";
// Message newMessage(MessageType::COMMAND, jdataData, "CommandManager");
// // m_agentQueue.push(newMessage);
// // return m;
// }
if (jdataData.at("status") == "Pending")
{
std::cout << "Message status is Pending. Updating status and pushing back." << std::endl;
jdataData["status"] = "InProcess";
Message newMessage(MessageType::COMMAND, jdataData, "CommandManager");
// m_agentQueue.push(newMessage);
// return m;
}

return m;
},
Expand Down

0 comments on commit 3f9c2b8

Please sign in to comment.