-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpfaces-job-manager.cpp
165 lines (153 loc) · 4.11 KB
/
pfaces-job-manager.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
#include "pfaces-agent-utils.h"
#include "pfaces-job-manager.h"
pFacesJob::pFacesJob(size_t _id, const std::string& _launch_cmd) {
control_pack = std::make_shared<non_blocking_thread_pack>();
control_pack->launch_cmd = _launch_cmd;
id = _id;
status = PFACES_JOB_STATUS::idle;
}
std::string pFacesJob::getLaunchCommand() {
return control_pack->launch_cmd;
}
size_t pFacesJob::getId() {
return id;
}
std::string pFacesJob::statusStr() {
switch (status)
{
case PFACES_JOB_STATUS::idle:
return "idle";
break;
case PFACES_JOB_STATUS::started:
return "started";
break;
case PFACES_JOB_STATUS::finished:
return "finished";
break;
case PFACES_JOB_STATUS::killed:
return "killed";
break;
default:
return "error";
break;
}
}
std::string pFacesJob::outputStr() {
std::lock_guard<std::mutex> lock(control_pack->thread_mutex);
return control_pack->output;
}
void pFacesJob::refresh() {
std::lock_guard<std::mutex> lock(control_pack->thread_mutex);
if (control_pack->done_notifier)
status = PFACES_JOB_STATUS::finished;
}
void pFacesJob::run() {
if (status == PFACES_JOB_STATUS::started ||
status == PFACES_JOB_STATUS::finished ||
status == PFACES_JOB_STATUS::killed)
return;
job_thread = std::thread(
pfacesAgentUtils::exec_non_blocking_thread_ready,
control_pack
);
status = PFACES_JOB_STATUS::started;
}
void pFacesJob::kill() {
if (
status == PFACES_JOB_STATUS::finished ||
status == PFACES_JOB_STATUS::killed)
return;
if (status == PFACES_JOB_STATUS::idle){
status = PFACES_JOB_STATUS::killed;
return;
}
{
std::lock_guard<std::mutex> lock(control_pack->thread_mutex);
control_pack->kill_signal = true;
}
job_thread.join();
status = PFACES_JOB_STATUS::killed;
}
size_t pfacesJobManager::launchJob(const std::string& _launch_cmd) {
std::lock_guard<std::mutex> lock(work_coordinator);
size_t new_id = pfacesJobs.size() + 1000;
pfacesJobs.push_back(std::make_shared<pFacesJob>(new_id, _launch_cmd));
size_t ret;
try {
pfacesJobs[pfacesJobs.size()-1]->run();
ret = new_id;
}
catch (...) {
ret = 0;
}
return ret;
}
void pfacesJobManager::killJob(size_t id) {
std::lock_guard<std::mutex> lock(work_coordinator);
// dont delete the job from the list
// otherwise, you need to changine the IDing mechanism
for (size_t i = 0; i < pfacesJobs.size(); i++){
if (pfacesJobs[i]->getId() == id) {
pfacesJobs[i]->refresh();
pfacesJobs[i]->kill();
break;
}
}
}
std::string pfacesJobManager::getJobOutput(size_t id) {
std::lock_guard<std::mutex> lock(work_coordinator);
std::string ret;
for (size_t i = 0; i < pfacesJobs.size(); i++) {
if (pfacesJobs[i]->getId() == id) {
pfacesJobs[i]->refresh();
ret = pfacesJobs[i]->outputStr();
break;
}
}
if (ret.empty())
return "job-not-found";
else
return ret;
}
std::string pfacesJobManager::getJobStatus(size_t id) {
std::lock_guard<std::mutex> lock(work_coordinator);
std::string ret;
for (size_t i = 0; i < pfacesJobs.size(); i++) {
if (pfacesJobs[i]->getId() == id) {
pfacesJobs[i]->refresh();
ret = pfacesJobs[i]->statusStr();
break;
}
}
if (ret.empty())
return "job-not-found";
else
return ret;
}
std::string pfacesJobManager::getJobsTableJSON(
std::string id_col_name,
std::string id_cmd_name,
std::string status_col_name,
std::string details_col_name) {
std::lock_guard<std::mutex> lock(work_coordinator);
size_t num_jobs = pfacesJobs.size();
std::vector<std::string> table_head_keys = {
id_col_name,
id_cmd_name,
status_col_name,
details_col_name
};
std::vector<std::vector<std::string>> info_list;
for (size_t i = 0; i < num_jobs; i++){
pfacesJobs[i]->refresh();
std::vector<std::string> info;
info.push_back(std::to_string(pfacesJobs[i]->getId()));
info.push_back(pfacesJobs[i]->getLaunchCommand());
info.push_back(pfacesJobs[i]->statusStr());
info.push_back(pfacesJobs[i]->outputStr());
info_list.push_back(info);
}
return (num_jobs > 0) ?
pfacesAgentUtils::makeJSONTable(table_head_keys, info_list) :
"no-jobs";
}