-
Notifications
You must be signed in to change notification settings - Fork 1
/
TaskScheduler.cpp
188 lines (160 loc) · 4.39 KB
/
TaskScheduler.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Created by Anaid Gakhokidze on 2017-05-08.
//
#include <thread>
#include <vector>
#include <sstream>
#include "TaskScheduler.h"
#include "Log.h"
TaskScheduler::TaskScheduler(std::string databaseName, int mode)
: m_mode(mode)
, m_tasks()
, m_db(databaseName)
, m_metricsQ(0) // Initial size is 0, but the Q is variable in size
, m_exit(false)
, m_log()
, m_updatedAggregateMetrics(false)
, m_tasksMetrics()
{
}
bool TaskScheduler::initialize()
{
if (!m_db.initialize()) return false;
for (auto i = m_tasks.begin(); i != m_tasks.end(); i++)
{
std::string taskName = (*i)->getName();
if (!m_db.createTable(taskName))
{
return false;
}
}
if (!m_db.createAggregateMetricTable())
{
return false;
}
return true;
}
void TaskScheduler::addTask(Task *task)
{
m_tasks.push_back(task);
}
void TaskScheduler::cancelTask(Task *task)
{
m_tasks.remove(task);
}
void TaskScheduler::changeTaskOrder(Task *task, int order)
{
m_tasks.remove(task);
std::list<Task*>::iterator itr = m_tasks.begin();
std::advance(itr, order);
m_tasks.insert(itr, task);
}
void TaskScheduler::threadTask(Task * task)
{
m_log.logMessage("Beginning the thread for task %s\n", task->getName().c_str());
while (!m_exit)
{
std::unordered_map<std::string, double> metrics = task->operator()();
m_log.logMessage("%s()\n", task->getName().c_str());
Metric * metric = new Metric(task->getName(), metrics);
m_metricsQ.push(metric);
std::this_thread::sleep_for(std::chrono::seconds(task->getInterval()));
}
}
void TaskScheduler::insertMetric(Metric & metric)
{
std::string taskName = metric.getTaskName();
std::unordered_map<std::string, double> rawMetrics = metric.getRawMetrics();
bool needToAlterTable = m_tasksMetrics.find(taskName) == m_tasksMetrics.end();
if (needToAlterTable)
{
for (auto i : rawMetrics)
{
m_db.addColumn(taskName, i.first);
}
m_tasksMetrics.insert({taskName, rawMetrics});
}
m_db.insertRecord(taskName, rawMetrics);
}
void TaskScheduler::databaseThreadTask()
{
Metric metric;
while (!m_exit)
{
while (m_metricsQ.pop(metric))
{
insertMetric(metric);
}
}
while (m_metricsQ.pop(metric))
{
insertMetric(metric);
}
m_log.logMessage("Finished inserting all of the outstanding metrics into the database\n");
updateAggregateMetrics();
}
void TaskScheduler::userInputThreadTask()
{
std::cout << "Task scheduler is running now.\n";
std::string userCommand;
do {
std::cout << "Enter 'exit' or Ctrl-C and press enter to quit task scheduler: ";
std::cin >> userCommand;
} while (userCommand != "exit" && !m_exit);
std::cout << "Exiting task scheduler\n";
m_exit = true;
}
void TaskScheduler::start()
{
m_log.logMessage("Starting task scheduler\n");
std::thread dbThread(&TaskScheduler::databaseThreadTask, this);
std::thread userInputThread;
if (m_mode == 0)
{
userInputThread = std::thread(&TaskScheduler::userInputThreadTask, this);
}
std::vector<std::thread> threads;
for (auto i = m_tasks.begin(); i != m_tasks.end(); i++)
{
threads.push_back(std::thread(&TaskScheduler::threadTask, this, *i));
}
for (auto i = threads.begin(); i != threads.end(); i++)
{
(*i).join();
}
dbThread.join();
if (m_mode == 0)
{
userInputThread.join();
}
}
void TaskScheduler::updateAggregateMetrics()
{
if (m_updatedAggregateMetrics) return;
for (auto i : m_tasksMetrics)
{
std::string taskName = i.first;
for (auto j : i.second)
{
m_db.updateAggregateMetric("avg", taskName, j.first);
m_db.updateAggregateMetric("max", taskName, j.first);
m_db.updateAggregateMetric("min", taskName, j.first);
}
}
m_updatedAggregateMetrics = true;
m_log.logMessage("Finished updating all of the aggregate values for metrics.\n");
}
void TaskScheduler::deinitialize()
{
if (!m_updatedAggregateMetrics)
{
updateAggregateMetrics();
}
m_db.deinitialize();
m_log.logMessage("Task scheduler de-initialized successfully.\n");
}
void TaskScheduler::stop()
{
m_exit = true;
m_log.logMessage("Stop command has been initiated\n");
}