当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 0

碧海青天/daemon
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 3.28 KB
一键复制 编辑 原始数据 按行查看 历史
碧海青天 提交于 2019-05-30 18:03 . no message
#include "daemon.hpp"
int main()
{
try
{
WORD wLastMs = 0;
Yaml::Node conf;
locale::global(locale(""));
system("title daemon - main");
EnableDebugPrivilege();
bool mainDaemon = true;
DWORD dwChildPid = 0;
BASEINFO baseInfo = { 0 };
list<PROCINFO> procInfoList = EnumProcInfo();
InitBaseInfo(baseInfo, procInfoList);
while (true)
{
SYSTEMTIME stLastWrite = GetLastWriteTime("conf.yml");
if (wLastMs == 0)
wLastMs = stLastWrite.wMilliseconds;
else if (wLastMs != stLastWrite.wMilliseconds) {
Log(L"conf.yml has been changed");
conf.Clear();
wLastMs = stLastWrite.wMilliseconds;
Log(L"empty configuration, need to reload conf.yml");
}
if (conf.IsNone())
{
Log(L"loading conf.yml");
Yaml::Parse(conf, "conf.yml");
Log(L"loading conf.yml completed");
}
bool become2child = baseInfo.wsName == baseInfo.wsPname && IsProcessAlive(baseInfo.dwPpid);
if (become2child && mainDaemon)
{
mainDaemon = false;
system("title daemon - child");
Log(L"main process exist, election become to child process");
}
else if (!become2child && !mainDaemon)
{
mainDaemon = true;
system("title daemon - main");
Log(L"main process not exist, election become to main process");
}
if (mainDaemon)
{
if (!dwChildPid || !IsProcessAlive(dwChildPid))
{
Log(L"child daemon not exist, try create child daemon");
dwChildPid = StartupProcess(baseInfo.wsName, conf["daemon"]["hide"].As<bool>() ? CREATE_NO_WINDOW : CREATE_NEW_CONSOLE);
if (!dwChildPid)
Log(L"create child daemon fail, something was wrong");
}
bool keepAlive = conf["keep-alive"].IsNone();
bool timingShutdown = conf["timing-shutdown"].IsNone();
bool intervalShutdown = conf["interval-shutdown"].IsNone();
bool windowMessage = conf["window-message"].IsNone();
if (!keepAlive || !timingShutdown || !intervalShutdown)
procInfoList = EnumProcInfo(timingShutdown || intervalShutdown);
if (!keepAlive)
KeepAlive(conf["keep-alive"], procInfoList);
if (!timingShutdown)
TimingShutdown(conf["timing-shutdown"], procInfoList);
if (!intervalShutdown)
IntervalShutdown(conf["interval-shutdown"], procInfoList);
if (!windowMessage)
WindowMessage(conf["window-message"]);
}
Sleep(conf["daemon"]["interval"].As<int>());
}
}
catch (runtime_error e)
{
Log(L"runtime error, " + ansi2unicode(string(e.what())) + L", abort");
}
catch (...)
{
Log(L"unknown error, abort");
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/gwsbhqt/daemon.git
git@gitee.com:gwsbhqt/daemon.git
gwsbhqt
daemon
daemon
master

搜索帮助