Skip to content

Commit

Permalink
fix bugs in webui
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroAir committed Apr 18, 2024
1 parent e9a52d4 commit 2780bde
Show file tree
Hide file tree
Showing 21 changed files with 1,535 additions and 1,322 deletions.
61 changes: 5 additions & 56 deletions src/atom/server/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Date: 2023-11-11
Description: Daemon process implementation for Linux and Windows. But there is
still some problems on Windows, espacially the console.
still some problems on Windows, especially the console.
**************************************************/

Expand Down Expand Up @@ -42,7 +42,7 @@ std::string DaemonGuard::ToString() const {
ss << "[DaemonGuard parentId=" << m_parentId << " mainId=" << m_mainId
<< " parentStartTime=" << Utils::timeStampToString(m_parentStartTime)
<< " mainStartTime=" << Utils::timeStampToString(m_mainStartTime)
<< " restartCount=" << m_restartCount << "]";
<< " restartCount=" << m_restartCount.load() << "]";
return ss.str();
}

Expand Down Expand Up @@ -82,7 +82,7 @@ int DaemonGuard::RealDaemon(int argc, char **argv,
CloseHandle(DaemonGuard.hThread);

// 等待一段时间后重新启动子进程
m_restartCount += 1;
m_restartCount++;
Sleep(g_daemonRestartInterval * 1000);
}
#else
Expand Down Expand Up @@ -123,7 +123,7 @@ int DaemonGuard::RealDaemon(int argc, char **argv,
}

// 等待一段时间后重新启动子进程
m_restartCount += 1;
m_restartCount++;
sleep(g_daemonRestartInterval);
}
}
Expand Down Expand Up @@ -205,55 +205,4 @@ bool CheckPidFile() {
return true;
#endif
}
} // namespace Atom::Async

/*
// 定义 MainCb 函数
int MainCb(int argc, char **argv)
{
// 实际任务的代码逻辑
for (int i = 0; i < 10; ++i)
{
std::cout << "hello world" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
// 主函数
int main(int argc, char **argv)
{
// 初始化日志库、配置文件等
// ...
// 检查 PID 文件是否存在,并检查文件中的 PID 是否有效
if (CheckPidFile())
{
// LOG_F(ERROR, "process already running with pid file {}",
g_pidFilePath); exit(-1);
}
// 写入 PID 文件
WritePidFile();
// 注册退出信号处理函数
signal(SIGTERM, SignalHandler);
signal(SIGINT, SignalHandler);
// 创建 DaemonGuard 对象并启动进程,如果需要创建守护进程,则先创建守护进程
DaemonGuard DaemonGuard;
DaemonGuard.StartDaemon(argc, argv, std::bind(MainCb, argc, argv),
g_isDaemon); DaemonGuard.RealDaemon(argc, argv, std::bind(MainCb, argc, argv));
if (g_isDaemon)
{
// LOG_F(INFO, "daemon process start pid={} argv={}", getpid(), argv)
}
std::cout << DaemonGuard.ToString() << std::endl;
// 删除 PID 文件并退出程序
remove(g_pidFilePath.c_str());
return 0;
}
*/
} // namespace Atom::Async
5 changes: 4 additions & 1 deletion src/atom/server/daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ Description: Daemon process implementation
#ifndef ATOM_SERVER_DAEMON_HPP
#define ATOM_SERVER_DAEMON_HPP

#include <atomic>
#include <cstring>
#include <ctime>
#include <functional>
#include <mutex>
#include <string>
#include <thread>

#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -93,7 +96,7 @@ class DaemonGuard {
#endif
time_t m_parentStartTime = 0; /**< The start time of the parent process. */
time_t m_mainStartTime = 0; /**< The start time of the child process. */
int m_restartCount = 0; /**< The number of restarts. */
std::atomic<int> m_restartCount{0}; /**< The number of restarts. */
};

/**
Expand Down
39 changes: 31 additions & 8 deletions src/atom/server/global_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,40 @@ GlobalSharedPtrManager &GlobalSharedPtrManager::getInstance() {
}

void GlobalSharedPtrManager::removeSharedPtr(const std::string &key) {
std::unique_lock<std::shared_mutex> lock(mtx);
std::unique_lock lock(mtx);
sharedPtrMap.erase(key);
}

void GlobalSharedPtrManager::removeExpiredWeakPtrs() {
std::unique_lock lock(mtx);
auto it = sharedPtrMap.begin();
while (it != sharedPtrMap.end()) {
try {
if (std::any_cast<std::weak_ptr<void>>(it->second).expired()) {
it = sharedPtrMap.erase(it);
} else {
++it;
}
} catch (const std::bad_any_cast &) {
++it;
}
}
}

void GlobalSharedPtrManager::clearAll() {
std::unique_lock lock(mtx);
sharedPtrMap.clear();
}

size_t GlobalSharedPtrManager::size() const {
std::shared_lock lock(mtx);
return sharedPtrMap.size();
}

void GlobalSharedPtrManager::printSharedPtrMap() const {
std::shared_lock<std::shared_mutex> lock(mtx);
std::cout << "Shared pointer map:" << std::endl;
for (const auto &it : sharedPtrMap) {
std::ostringstream oss;
oss << it.second.type().name();
std::cout << "- Key: " << it.first << ", Type: " << oss.str()
<< std::endl;
std::shared_lock lock(mtx);
std::cout << "GlobalSharedPtrManager:\n";
for (const auto &pair : sharedPtrMap) {
std::cout << " " << pair.first << "\n";
}
}
Loading

0 comments on commit 2780bde

Please sign in to comment.