Răsfoiți Sursa

!2 fix last entity startup problem

chenliangyu 7 luni în urmă
părinte
comite
5a571471c9
2 a modificat fișierele cu 40 adăugiri și 2 ștergeri
  1. 39 1
      Framework/spbase/StartUpBase.cpp
  2. 1 1
      Framework/spbase/sp_mod.c

+ 39 - 1
Framework/spbase/StartUpBase.cpp

@@ -7,6 +7,8 @@
 #include <map>
 #include "log_define.h"
 #include <SpBase.h>
+#include <chrono>
+#include <thread>
 
 class StartupLog {
 public:
@@ -55,6 +57,17 @@ private:
     StartupLogManager();
     ~StartupLogManager();
     std::map<int, StartupLog> logs_;
+    std::chrono::steady_clock::time_point m_lastUpdateTime;
+
+    void recordStartTime() {
+        m_lastUpdateTime = std::chrono::steady_clock::now();
+    }
+
+    bool isTimeExceeded() {
+        auto current_time = std::chrono::steady_clock::now();
+        auto duration = std::chrono::duration_cast<std::chrono::minutes>(current_time - m_lastUpdateTime);
+        return duration.count() >= 2;
+    }
 };
 
 std::string GetCurrentTimeStr();
@@ -150,11 +163,35 @@ std::string StartupLog::toJSON() const {
     return jsonReq;
 }
 
-StartupLogManager::StartupLogManager() {}
+StartupLogManager::StartupLogManager()
+{
+    static std::thread checkTimeThread;
+    recordStartTime();
+    auto checkStartupEnd = [this] {
+        while (true)
+        {
+            std::this_thread::sleep_for(std::chrono::seconds(5));
+            if (isTimeExceeded())
+            {
+                for (auto current : logs_)
+                {
+                    std::string resultMsg = current.second.toJSON();
+                    DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)(resultMsg.c_str());
+                }
+                logs_.clear();
+                break;
+            }
+        }
+    };
+    if (!checkTimeThread.joinable()) {
+        checkTimeThread = std::thread(checkStartupEnd);
+    }
+}
 
 StartupLogManager::~StartupLogManager() {}
 
 void StartupLogManager::addLog(const std::string& module_entity, int idx) {
+    recordStartTime();
     if (logs_.find(idx) == logs_.end()) {
         logs_[idx] = StartupLog(module_entity, idx); // 直接拷贝 log 对象
     } // 如果已存在,则忽略
@@ -176,6 +213,7 @@ bool StartupLogManager::checkLogExist(int idx)
 }
 
 void StartupLogManager::addStep(int idx, const StartupLog::Step& step) {
+    recordStartTime();
     if (checkLogExist(idx))
     {
         StartupLog &current = getLog(idx);

+ 1 - 1
Framework/spbase/sp_mod.c

@@ -1613,7 +1613,7 @@ sp_mod_t *sp_mod_mgr_find_module_by_name(sp_mod_mgr_t *mgr, const char *mod_name
 
 sp_mod_t *sp_mod_mgr_find_module_by_idx(sp_mod_mgr_t *mgr, int mod_idx)
 {
-	if (mod_idx == -1 || mod_idx >= mgr->arr_mod->nelts)
+	if (mod_idx == -1 || mod_idx > mgr->arr_mod->nelts)
 		return NULL;
 	
 	return ARRAY_IDX(mgr->arr_mod, mod_idx, sp_mod_t*);