Deb Roclinn commited on
Commit
4305cb6
1 Parent(s): 561ef2b

Removed duplicate checks and made IDs timestamp-aware

Browse files
Files changed (1) hide show
  1. server.cpp +14 -34
server.cpp CHANGED
@@ -13,6 +13,7 @@
13
  #include <ranges>
14
  #include "threadpool.h"
15
  #include <sstream>
 
16
  #define CROW_MAIN
17
 
18
  using std::string;
@@ -26,7 +27,13 @@ namespace rv = std::ranges::views;
26
 
27
  constexpr string to_st(const auto& i){ std::stringstream ss; ss << i; return ss.str(); }
28
 
29
- static inline auto uid(const std::string& s){ std::hash<string> h; return to_st(h(s)); }
 
 
 
 
 
 
30
 
31
  static inline string exec(const char* cmd) {
32
  std::array<char, 128> buffer;
@@ -86,17 +93,6 @@ int main(){
86
  return exec(cmd.c_str());
87
  };
88
 
89
- auto check_queue = [=](const string& name) -> int {
90
- if(!commissions->empty()){
91
- const auto v = q_to_v(*commissions);
92
- if(auto pos = std::find_if( v.begin(), v.end()
93
- , [=](const guy& g){ auto& [n,d] = g; return n == name; });
94
- pos != v.end())
95
- return int(std::distance(v.begin(), pos));
96
- }
97
- return -1;
98
- };
99
-
100
  auto poppe = [=](){
101
  lock_guard<mutex> qlock(*queue_mutex);
102
  commissions->pop();
@@ -130,34 +126,18 @@ int main(){
130
  } else {
131
  CROW_LOG_INFO << prompt << " commissioned";
132
  auto id = uid(prompt);
133
- if(auto r = check_queue(id); r < 0){
134
- enqueue({id, prompt});
135
- pool->enqueue(training_loop);
136
- CROW_LOG_INFO << "Launched training loop";
137
- return "Scheduled training for " + id;
138
- } else
139
- return id + " is currently " + (r ? string("in line") : string("training"));
140
  }
141
  });
142
 
143
- CROW_ROUTE(app, "/check/<string>")([=](crow::response& res, const string& name){
144
- CROW_LOG_INFO << name << " check'd";
145
- if(fs::exists(fs::path(name + ".zip"))){
146
- res.write("O I know that guy");
147
- res.set_static_file_info(name + ".zip");
148
- } else if(auto r = check_queue(name); r < 0)
149
- res.write("Doesn't look like much of anything to me");
150
- else
151
- res.write(name + " is currently "
152
- + (r ? string("in line") : string("training")));
153
- res.end();
154
- });
155
-
156
  CROW_ROUTE(app, "/list")([&](){
157
  std::vector<string> fin = splitOn(exec("ls *zip"), "\n")
158
- , q = reify(q_to_v(*commissions) | rv::transform([](const guy& i){ return i[0]; }));
 
159
  crow::json::wvalue ret;
160
- ret["finished"] = fin;
161
  ret["pending"] = q;
162
  return ret;
163
  });
 
13
  #include <ranges>
14
  #include "threadpool.h"
15
  #include <sstream>
16
+ #include <chrono>
17
  #define CROW_MAIN
18
 
19
  using std::string;
 
27
 
28
  constexpr string to_st(const auto& i){ std::stringstream ss; ss << i; return ss.str(); }
29
 
30
+ static inline auto uid(const std::string& s){
31
+ std::stringstream ss;
32
+ std::hash<string> h;
33
+ const auto t0 = std::chrono::system_clock::now();
34
+ ss << s << '|' << std::chrono::duration_cast<std::chrono::nanoseconds>(t0.time_since_epoch()).count();
35
+ return to_st(h(ss.str()));
36
+ }
37
 
38
  static inline string exec(const char* cmd) {
39
  std::array<char, 128> buffer;
 
93
  return exec(cmd.c_str());
94
  };
95
 
 
 
 
 
 
 
 
 
 
 
 
96
  auto poppe = [=](){
97
  lock_guard<mutex> qlock(*queue_mutex);
98
  commissions->pop();
 
126
  } else {
127
  CROW_LOG_INFO << prompt << " commissioned";
128
  auto id = uid(prompt);
129
+ enqueue({id, prompt});
130
+ pool->enqueue(training_loop);
131
+ CROW_LOG_INFO << "Launched training loop";
132
+ return "Scheduled training for " + id;
 
 
 
133
  }
134
  });
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  CROW_ROUTE(app, "/list")([&](){
137
  std::vector<string> fin = splitOn(exec("ls *zip"), "\n")
138
+ , q = reify( q_to_v(*commissions)
139
+ | rv::transform([](const guy& i){ return i[0] + ": '" + i[1] + "'"; }));
140
  crow::json::wvalue ret;
 
141
  ret["pending"] = q;
142
  return ret;
143
  });