feat: improve local node initalization
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
#include "spore/core/ClusterManager.h"
|
#include "spore/core/ClusterManager.h"
|
||||||
#include "spore/internal/Globals.h"
|
#include "spore/internal/Globals.h"
|
||||||
#include "spore/util/Logging.h"
|
#include "spore/util/Logging.h"
|
||||||
|
#include "spore/types/NodeInfo.h"
|
||||||
|
|
||||||
ClusterManager::ClusterManager(NodeContext& ctx, TaskManager& taskMgr) : ctx(ctx), taskManager(taskMgr) {
|
ClusterManager::ClusterManager(NodeContext& ctx, TaskManager& taskMgr) : ctx(ctx), taskManager(taskMgr) {
|
||||||
// Register callback for node/discovered event
|
// Register callback for node/discovered event - this fires when network is ready
|
||||||
ctx.on("node/discovered", [this](void* data) {
|
ctx.on("node/discovered", [this](void* data) {
|
||||||
NodeInfo* node = static_cast<NodeInfo*>(data);
|
NodeInfo* node = static_cast<NodeInfo*>(data);
|
||||||
this->addOrUpdateNode(node->hostname, node->ip);
|
this->addOrUpdateNode(node->hostname, node->ip);
|
||||||
@@ -367,6 +368,16 @@ void ClusterManager::addOrUpdateNode(const String& nodeHost, IPAddress nodeIP) {
|
|||||||
newNode.ip = nodeIP;
|
newNode.ip = nodeIP;
|
||||||
newNode.lastSeen = millis();
|
newNode.lastSeen = millis();
|
||||||
updateNodeStatus(newNode, newNode.lastSeen, ctx.config.node_inactive_threshold_ms, ctx.config.node_dead_threshold_ms);
|
updateNodeStatus(newNode, newNode.lastSeen, ctx.config.node_inactive_threshold_ms, ctx.config.node_dead_threshold_ms);
|
||||||
|
|
||||||
|
// Initialize static resources if this is the local node being added for the first time
|
||||||
|
if (nodeIP == ctx.localIP && nodeHost == ctx.hostname) {
|
||||||
|
newNode.resources.chipId = ESP.getChipId();
|
||||||
|
newNode.resources.sdkVersion = String(ESP.getSdkVersion());
|
||||||
|
newNode.resources.cpuFreqMHz = ESP.getCpuFreqMHz();
|
||||||
|
newNode.resources.flashChipSize = ESP.getFlashChipSize();
|
||||||
|
LOG_DEBUG("Cluster", "Initialized static resources for local node");
|
||||||
|
}
|
||||||
|
|
||||||
ctx.memberList->addMember(ipStr.c_str(), newNode);
|
ctx.memberList->addMember(ipStr.c_str(), newNode);
|
||||||
memberlistChanged = true;
|
memberlistChanged = true;
|
||||||
LOG_INFO("Cluster", "Added node: " + nodeHost + " @ " + newNode.ip.toString() + " | Status: " + statusToStr(newNode.status) + " | last update: 0");
|
LOG_INFO("Cluster", "Added node: " + nodeHost + " @ " + newNode.ip.toString() + " | Status: " + statusToStr(newNode.status) + " | last update: 0");
|
||||||
@@ -469,12 +480,9 @@ void ClusterManager::updateLocalNodeResources(NodeInfo& node) {
|
|||||||
node.status = NodeInfo::ACTIVE;
|
node.status = NodeInfo::ACTIVE;
|
||||||
node.uptime = millis();
|
node.uptime = millis();
|
||||||
|
|
||||||
|
// Update dynamic resources (always updated)
|
||||||
uint32_t freeHeap = ESP.getFreeHeap();
|
uint32_t freeHeap = ESP.getFreeHeap();
|
||||||
node.resources.freeHeap = freeHeap;
|
node.resources.freeHeap = freeHeap;
|
||||||
node.resources.chipId = ESP.getChipId();
|
|
||||||
node.resources.sdkVersion = String(ESP.getSdkVersion());
|
|
||||||
node.resources.cpuFreqMHz = ESP.getCpuFreqMHz();
|
|
||||||
node.resources.flashChipSize = ESP.getFlashChipSize();
|
|
||||||
|
|
||||||
// Log memory warnings if heap is getting low
|
// Log memory warnings if heap is getting low
|
||||||
if (freeHeap < ctx.config.low_memory_threshold_bytes) {
|
if (freeHeap < ctx.config.low_memory_threshold_bytes) {
|
||||||
|
|||||||
Reference in New Issue
Block a user