Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent avoids delays shutdown during scanning #384

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
2 changes: 1 addition & 1 deletion src/modules/inventory/include/inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Inventory {
void ScanPorts();
void ScanProcesses();
void Scan();
void SyncLoop(std::unique_lock<std::mutex>& lock);
void SyncLoop();
void ShowConfig();
cJSON * Dump() const;
static void LogErrorInventory(const std::string& log);
Expand Down
53 changes: 34 additions & 19 deletions src/modules/inventory/src/inventoryImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ void Inventory::UpdateChanges(const std::string& table,
NotifyChange(result, data, table);
}
};

std::unique_lock<std::mutex> lock{m_mutex};
DBSyncTxn txn
{
m_spDBSync->handle(),
Expand All @@ -373,12 +375,16 @@ void Inventory::TryCatchTask(const std::function<void()>& task) const
{
if (!m_stopping)
{
task(); // Ejecuta la tarea
task();
}
else
{
LogTrace("No Scanning during stopping");
}
}
catch (const std::exception& ex)
{
LogErrorInventory(std::string{ex.what()});
LogError("{}",std::string{ex.what()});
}
}

Expand Down Expand Up @@ -422,23 +428,25 @@ void Inventory::Init(const std::shared_ptr<ISysInfo>& spInfo,
m_spInfo = spInfo;
m_reportDiffFunction = reportDiffFunction;

std::unique_lock<std::mutex> lock{m_mutex};
m_stopping = false;
m_spDBSync = std::make_unique<DBSync>(HostType::AGENT,
DbEngineType::SQLITE3,
dbPath,
GetCreateStatement(),
DbManagement::PERSISTENT);
m_spNormalizer = std::make_unique<InvNormalizer>(normalizerConfigPath, normalizerType);
SyncLoop(lock);
{
std::unique_lock<std::mutex> lock{m_mutex};
m_stopping = false;
m_spDBSync = std::make_unique<DBSync>(HostType::AGENT,
DbEngineType::SQLITE3,
dbPath,
GetCreateStatement(),
DbManagement::PERSISTENT);
m_spNormalizer = std::make_unique<InvNormalizer>(normalizerConfigPath, normalizerType);
}

SyncLoop();
}

void Inventory::Destroy()
{
std::unique_lock<std::mutex> lock{m_mutex};
m_stopping = true;
m_cv.notify_all();
lock.unlock();
}


Expand Down Expand Up @@ -671,7 +679,6 @@ nlohmann::json Inventory::GetNetworkData()
networkTableData["network_item_id"] = GetItemId(networkTableData, NETWORK_ITEM_ID_FIELDS);

ret[NETWORKS_TABLE].push_back(networkTableData);

}
}

Expand Down Expand Up @@ -733,6 +740,8 @@ void Inventory::ScanPackages()
NotifyChange(result, data, PACKAGES_TABLE);
}
};

std::unique_lock<std::mutex> lock{m_mutex};
DBSyncTxn txn
{
m_spDBSync->handle(),
Expand Down Expand Up @@ -862,6 +871,7 @@ void Inventory::ScanProcesses()
NotifyChange(result, data, PROCESSES_TABLE);
}
};
std::unique_lock<std::mutex> lock{m_mutex};
DBSyncTxn txn
{
m_spDBSync->handle(),
Expand Down Expand Up @@ -902,19 +912,24 @@ void Inventory::Scan()
LogInfo("Evaluation finished.");
}

void Inventory::SyncLoop(std::unique_lock<std::mutex>& lock)
void Inventory::SyncLoop()
{
if (m_scanOnStart)
LogInfo("Module started.");

if (m_scanOnStart && !m_stopping)
{
Scan();
}

while (!m_cv.wait_for(lock, std::chrono::milliseconds{m_intervalValue}, [&]()
{
return m_stopping;
}))
while (!m_stopping)
{
{
std::unique_lock<std::mutex> lock{m_mutex};
m_cv.wait_for(lock,
std::chrono::milliseconds{m_intervalValue}, [&]() { return m_stopping; } );
}
Scan();
}
std::unique_lock<std::mutex> lock{m_mutex};
m_spDBSync.reset(nullptr);
}
Loading