-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
144 lines (123 loc) · 3.62 KB
/
Copy pathmain.cpp
File metadata and controls
144 lines (123 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <csignal>
#include <sys/time.h>
#include "cmdline.h"
#include "config.hpp"
#include "log.hpp"
#include "taskTable.hpp"
#include "httpThread.hpp"
#include "util.hpp"
#ifndef INSTALL_PREFIX
#define INSTALL_PREFIX "/usr/local"
#endif
Config* config = nullptr;
TaskTable* taskTable = nullptr;
HttpThread* httpThread = nullptr;
static cmdline::parser arg;
static bool isRunning = true;
static bool isReload = false;
int main(int argc, char *argv[])
{
// 解析参数
arg.add<std::string>("config", 'c', "Config File", false, std::string(INSTALL_PREFIX) + "/etc/timer.ini");
arg.parse_check(argc, argv);
// 初始化配置类
config = new Config(arg.get<std::string>("config"));
// 获取PID文件中的数据
std::string pidFileData = util::fileGetContents(config->getPidFile());
if (!pidFileData.empty() && kill(std::stoi(pidFileData), 0) == 0) {
std::cerr << "Running!" << std::endl;
return EXIT_FAILURE;
}
// 初始化日志模块
Log::initiate(config->getLogDir(), config->getLogLevel());
if (!util::filePutContents(config->getPidFile(), std::to_string(getpid()))) {
Log::error("Pid file write fail! [{}]", config->getPidFile());
return EXIT_FAILURE;
}
try
{
taskTable = new TaskTable(config->getDbPath());
}
catch(const std::exception& e)
{
Log::error("Task Table initiate fail: {}", e.what());
return EXIT_FAILURE;
}
// 初始化HTTP线程
httpThread = new HttpThread(config, taskTable);
httpThread->start();
// 监听定时器信号
signal(SIGALRM, [](int signal) {
// 如果正在重载,本次不处理
if (isReload) return;
tm tm = util::getLocaltime();
// 循环任务表
for(Task task : taskTable->list()) {
// 如果不是循环执行,或者未启用
if (!task.loop || !task.enable) continue;
if (task.cronRange.checkRunTime(tm.tm_sec, tm.tm_min, tm.tm_hour, tm.tm_mday, tm.tm_mon + 1, tm.tm_wday)) {
// 运行任务
taskTable->run(task, config);
}
}
});
// 设置定时器参数
struct itimerval timer;
timer.it_value.tv_sec = 1; // 初始定时器间隔为1秒
timer.it_value.tv_usec = 0;
timer.it_interval.tv_sec = 1; // 定时器间隔为1秒
timer.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &timer, nullptr);
// 监听退出信号
signal(SIGTERM, [](int signal) {
// 退出HTTP线程
httpThread->close();
// 清除定时器
setitimer(ITIMER_REAL, nullptr, nullptr);
isRunning = false;
});
// 监听重载信号
signal(SIGHUP, [](int signal) {
// 标记为正在重载
isReload = true;
Log::info("Reload!");
// 退出HTTP线程
httpThread->close();
// 清除任务表
delete taskTable;
// 清除配置类
delete config;
// 初始化配置类
config = new Config(arg.get<std::string>("config"));
// 初始化日志模块
Log::initiate(config->getLogDir(), config->getLogLevel());
if (!util::filePutContents(config->getPidFile(), std::to_string(getpid()))) {
Log::error("Pid file write fail! [{}]", config->getPidFile());
exit(EXIT_FAILURE);
}
// 初始化任务表
try
{
taskTable = new TaskTable(config->getDbPath());
}
catch(const std::exception& e)
{
Log::error("Task Table initiate fail: {}", e.what());
exit(EXIT_FAILURE);
}
// 初始化HTTP线程
httpThread = new HttpThread(config, taskTable);
httpThread->start();
isReload = false;
});
while (isRunning)
{
pause();
}
std::filesystem::remove(config->getPidFile());
delete httpThread;
delete taskTable;
delete config;
return EXIT_SUCCESS;
}