summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames E. Blair <jeblair@redhat.com>2019-02-11 07:47:26 -0800
committerJames E. Blair <jeblair@redhat.com>2019-02-11 07:47:26 -0800
commit12e829169865c17fd24c21a188339a9943ad8bdc (patch)
tree103966b93467b148214d23c5f4918c463150bfca
parent5a36c4a3ebbc99d102d396906aae4e3caaec9ad2 (diff)
Clean up comments
-rw-r--r--zuul-preview/main.cc46
1 files changed, 25 insertions, 21 deletions
diff --git a/zuul-preview/main.cc b/zuul-preview/main.cc
index 42f479d..22ce31b 100644
--- a/zuul-preview/main.cc
+++ b/zuul-preview/main.cc
@@ -17,6 +17,12 @@
17 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */ 18 */
19 19
20/*
21 * This program reads one line at a time on standard input, expecting
22 * a specially formatted hostname from Apache's RewriteMap and uses
23 * that to look up a build URL which it emits on standard output.
24 */
25
20#include <config.h> 26#include <config.h>
21#include <pthread.h> 27#include <pthread.h>
22#include <boost/optional.hpp> 28#include <boost/optional.hpp>
@@ -36,6 +42,7 @@ vector<string> split(const string &in)
36 return parts; 42 return parts;
37} 43}
38 44
45// An LRU cache of hostname->URL mappings.
39class Cache { 46class Cache {
40 // A queue of hostname, URL pairs. The head of the queue is always 47 // A queue of hostname, URL pairs. The head of the queue is always
41 // the most recently accessed entry, the tail is the least. 48 // the most recently accessed entry, the tail is the least.
@@ -64,7 +71,6 @@ public:
64 auto val = *(location->second); 71 auto val = *(location->second);
65 queue.erase(location->second); 72 queue.erase(location->second);
66 queue.push_front(val); 73 queue.push_front(val);
67 //cout << "get push " << val.second << endl;
68 return val.second; 74 return val.second;
69 } 75 }
70 76
@@ -78,12 +84,10 @@ public:
78 84
79 if (queue.size() == size) { 85 if (queue.size() == size) {
80 auto last = queue.back(); 86 auto last = queue.back();
81 //cout << "put pop " << last.second << endl;
82 queue.pop_back(); 87 queue.pop_back();
83 map.erase(last.first); 88 map.erase(last.first);
84 } 89 }
85 90
86 //cout << "put push " << value << endl;
87 queue.push_front(make_pair(key, value)); 91 queue.push_front(make_pair(key, value));
88 map[key] = queue.begin(); 92 map[key] = queue.begin();
89 } 93 }
@@ -96,52 +100,52 @@ int main(int, char**)
96 100
97 string hostname; 101 string hostname;
98 Cache cache{1024}; 102 Cache cache{1024};
103
104 // For each request apache receieves, it sends us the HTTP host name
105 // on standard input. We use that to look up the build URL and emit
106 // it on standard output. Apache will send us one request at a time
107 // (protected by an internal mutex) and expect exactly one line of
108 // output for each.
109 // Expected input:
110 // site.926bb0aaddad4bc3853269451e115dcb.openstack.preview.opendev.org
99 while (getline(cin, hostname)) { 111 while (getline(cin, hostname)) {
100 // Expected hostname:
101 // site.75031cad206c4014ad7a3387091d15ab.openstack.preview.opendev.org
102 // Apache will drop "preview.opendev.org", so our expected input will be:
103 // site.75031cad206c4014ad7a3387091d15ab.openstack
104 // site.7c16d914db5a4c4b91cd9a31d119dd48.openstack
105 // site.688b70499b9a41a08f498ed6e932960c.openstack
106 // site.dbefc23dcc594577a8bfa4db4f9b0a8f.openstack
107 112
113 // If we have the value in the cache, return it.
108 if (auto val = cache.get(hostname)) { 114 if (auto val = cache.get(hostname)) {
109 cout << val.value() << endl; 115 cout << val.value() << endl;
110 continue; 116 continue;
111 } 117 }
112 118
119 // We use the first three parts of the hostname to look up the
120 // build url.
113 auto parts = split(hostname); 121 auto parts = split(hostname);
114 if (parts.size() < 3) { 122 if (parts.size() < 3) {
115 cout << "not enough args" << endl; 123 cout << "Not enough args" << endl;
116 continue; 124 continue;
117 } 125 }
118 auto artifact = parts[0]; 126 auto artifact = parts[0];
119 auto buildid = parts[1]; 127 auto buildid = parts[1];
120 auto tenant = parts[2]; 128 auto tenant = parts[2];
121 /*
122 cout << artifact << endl
123 << buildid << endl
124 << tenant << endl;
125 */
126 129
127 // 75031cad206c4014ad7a3387091d15ab
128 try { 130 try {
131 // Use the Zuul API to look up the artifact URL.
129 auto uri = web::uri_builder("/api/tenant/" + tenant + "/build"); 132 auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
130 uri.append_path(buildid); 133 uri.append_path(buildid);
131 auto response = client.request( 134 auto response = client.request(
132 web::http::methods::GET, uri.to_string()).get(); 135 web::http::methods::GET, uri.to_string()).get();
133 // body is a web::json::value 136 // body is a web::json::value
134 // cout << response.status_code() << endl;
135 auto body = response.extract_json().get(); 137 auto body = response.extract_json().get();
136 //cout << body.serialize() << endl;
137 138
138 // TODO: use artifact 139 // TODO: use artifact instead of log_url
139 // body["log_url"].as_string() returns a const std::string& 140 // body["log_url"].as_string() returns a const std::string&
140 cout << body["log_url"].as_string() << endl; 141 cout << body["log_url"].as_string() << endl;
141 142
142 cache.put(hostname, body["log_url"].as_string()); 143 cache.put(hostname, body["log_url"].as_string());
143 } catch (...) { 144 } catch (...) {
144 cout << "error" << endl; 145 // If anything goes wrong, we still need to return only a single
146 // string to apache, and recover for the next request, so we
147 // have a general exception handler here.
148 cout << "Error" << endl;
145 } 149 }
146 } 150 }
147} 151}