mirror of
https://github.com/EvernodeXRPL/sashimono.git
synced 2026-04-29 15:38:00 +00:00
Hp monitor loop to monitor container status and re spawn if crashed. (#8)
This commit is contained in:
committed by
GitHub
parent
d36992ede9
commit
433a4a1e0a
@@ -33,8 +33,12 @@ namespace sqlite
|
||||
|
||||
constexpr const char *UPDATE_STATUS_IN_HP = "UPDATE instances SET status = ? WHERE name = ?";
|
||||
|
||||
constexpr const char *UPDATE_CURRENT_STATUS_IN_HP = "UPDATE instances SET current_status = ? WHERE name = ?";
|
||||
|
||||
constexpr const char *IS_CONTAINER_EXISTS = "SELECT * FROM instances WHERE name = ?";
|
||||
|
||||
constexpr const char *GET_RUNNING_INSTANCE_NAMES = "SELECT name FROM instances WHERE status = ?";
|
||||
|
||||
constexpr const char *IS_TABLE_EXISTS = "SELECT * FROM sqlite_master WHERE type='table' AND name = ?";
|
||||
|
||||
/**
|
||||
@@ -281,6 +285,7 @@ namespace sqlite
|
||||
table_column_info("owner_pubkey", COLUMN_DATA_TYPE::TEXT),
|
||||
table_column_info("time", COLUMN_DATA_TYPE::INT),
|
||||
table_column_info("status", COLUMN_DATA_TYPE::TEXT),
|
||||
table_column_info("current_status", COLUMN_DATA_TYPE::TEXT),
|
||||
table_column_info("name", COLUMN_DATA_TYPE::TEXT, true),
|
||||
table_column_info("ip", COLUMN_DATA_TYPE::TEXT),
|
||||
table_column_info("peer_port", COLUMN_DATA_TYPE::INT),
|
||||
@@ -377,6 +382,28 @@ namespace sqlite
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the current status of the given container to the new value.
|
||||
* @param db Database connection.
|
||||
* @param container_name Name of the container whose status should be updated.
|
||||
* @param current_status The new status of the container.
|
||||
* @return 0 on success and -1 on error.
|
||||
*/
|
||||
int update_current_status_in_container(sqlite3 *db, std::string_view container_name, std::string_view current_status)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(db, UPDATE_CURRENT_STATUS_IN_HP, -1, &stmt, 0) == SQLITE_OK && stmt != NULL &&
|
||||
sqlite3_bind_text(stmt, 1, current_status.data(), current_status.length(), SQLITE_STATIC) == SQLITE_OK &&
|
||||
sqlite3_bind_text(stmt, 2, container_name.data(), container_name.length(), SQLITE_STATIC) == SQLITE_OK &&
|
||||
sqlite3_step(stmt) == SQLITE_DONE)
|
||||
{
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
LOG_ERROR << "Error updating container current status for " << container_name;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the max peer and user ports assigned for instances excluding destroyed instances.
|
||||
* @param db Database connection.
|
||||
@@ -431,4 +458,30 @@ namespace sqlite
|
||||
// Finalize and distroys the statement.
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the given vector with names of running hp instances.
|
||||
* @param db Database connection.
|
||||
* @param running_instance_names Vector to hold name of instances from database.
|
||||
*/
|
||||
void get_running_instance_names(sqlite3 *db, std::vector<std::string> &running_instance_names)
|
||||
{
|
||||
running_instance_names.clear();
|
||||
|
||||
sqlite3_stmt *stmt;
|
||||
std::string_view running_status(hp::CONTAINER_STATES[hp::STATES::RUNNING]);
|
||||
|
||||
if (sqlite3_prepare_v2(db, GET_RUNNING_INSTANCE_NAMES, -1, &stmt, 0) == SQLITE_OK && stmt != NULL &&
|
||||
sqlite3_bind_text(stmt, 1, running_status.data(), running_status.length(), SQLITE_STATIC) == SQLITE_OK)
|
||||
{
|
||||
while (stmt != NULL && sqlite3_step(stmt) == SQLITE_ROW)
|
||||
{
|
||||
const std::string name(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)));
|
||||
running_instance_names.push_back(name);
|
||||
}
|
||||
}
|
||||
|
||||
// Finalize and distroys the statement.
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user