-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmqueue.cpp
73 lines (65 loc) · 2.14 KB
/
rmqueue.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
#include "utils.h"
int main(int argc, char *args[]) {
// int target_id = stoi(args[1]);
set<int> target_ids = {};
for(int i = 1; i < argc; i++){
target_ids.insert(stoi(args[i]));
}
string QUEUEPATH = PROJECTPATH + (PROJECTPATH.back() == '/' ? "" : "/") + "process_queue.que";
ifstream ifile(QUEUEPATH);
string line;
vector<Script> scripts;
vector<int> ids;
// TODO: Implement the functionality to remove a running script from the queue.
// If the script is running, change the status to "terminating".
// If the script is not running, remove the script from the queue.
// Finished
if(ifile.is_open()){
while(getline(ifile, line)){
Script tmp(line);
if(target_ids.find(tmp.id) != target_ids.end()){
target_ids.erase(tmp.id);
ids.push_back(tmp.id);
if(tmp.status == "running"){
tmp.status = "terminating";
}else{
tmp.status = "0";
string tmp_script_path = PROJECTPATH + (PROJECTPATH.back() == '/' ? "" : "/") + "tmp/tmp_script_" + to_string(tmp.id) + "-" + tmp.submit_time + ".sh";
if(access(tmp_script_path.c_str(), F_OK) == 0){
remove(tmp_script_path.c_str());
}
}
}
if(tmp.status != "0"){
scripts.push_back(tmp);
}
}
}else{
cout << "Failed to open file." << endl;
return 1;
}
if(target_ids.size() > 0){
cout << "Cannot find script(s) with id ";
for(auto id : target_ids){
cout << id << " ";
}
cout << "in queue." << endl;
return 0;
}
ofstream ofile(QUEUEPATH);
if(ofile.is_open()){
for(auto script : scripts){
ofile << script << endl;
}
ofile.close();
}else{
cout << "Failed to open file." << endl;
return 1;
}
cout << "Removed script(s) with id ";
for(auto id : ids){
cout << id << " ";
}
cout << "from queue." << endl;
return 0;
}