Browse Source

Clean up comments

master
James E. Blair 3 months ago
parent
commit
12e8291698
1 changed files with 25 additions and 21 deletions
  1. 25
    21
      zuul-preview/main.cc

+ 25
- 21
zuul-preview/main.cc View File

@@ -17,6 +17,12 @@
17 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 26
 #include <config.h>
21 27
 #include <pthread.h>
22 28
 #include <boost/optional.hpp>
@@ -36,6 +42,7 @@ vector<string> split(const string &in)
36 42
   return parts;
37 43
 }
38 44
 
45
+// An LRU cache of hostname->URL mappings.
39 46
 class Cache {
40 47
   // A queue of hostname, URL pairs.  The head of the queue is always
41 48
   // the most recently accessed entry, the tail is the least.
@@ -64,7 +71,6 @@ public:
64 71
     auto val = *(location->second);
65 72
     queue.erase(location->second);
66 73
     queue.push_front(val);
67
-    //cout << "get push " << val.second << endl;
68 74
     return val.second;
69 75
   }
70 76
 
@@ -78,12 +84,10 @@ public:
78 84
 
79 85
     if (queue.size() == size) {
80 86
       auto last = queue.back();
81
-      //cout << "put pop " << last.second << endl;
82 87
       queue.pop_back();
83 88
       map.erase(last.first);
84 89
     }
85 90
 
86
-    //cout << "put push " << value << endl;
87 91
     queue.push_front(make_pair(key, value));
88 92
     map[key] = queue.begin();
89 93
   }
@@ -96,52 +100,52 @@ int main(int, char**)
96 100
 
97 101
   string hostname;
98 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 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 114
     if (auto val = cache.get(hostname)) {
109 115
       cout << val.value() << endl;
110 116
       continue;
111 117
     }
112 118
 
119
+    // We use the first three parts of the hostname to look up the
120
+    // build url.
113 121
     auto parts = split(hostname);
114 122
     if (parts.size() < 3) {
115
-      cout << "not enough args" << endl;
123
+      cout << "Not enough args" << endl;
116 124
       continue;
117 125
     }
118 126
     auto artifact = parts[0];
119 127
     auto buildid = parts[1];
120 128
     auto tenant = parts[2];
121
-    /*
122
-    cout << artifact << endl
123
-         << buildid << endl
124
-         << tenant << endl;
125
-    */
126 129
 
127
-     // 75031cad206c4014ad7a3387091d15ab
128 130
     try {
131
+      // Use the Zuul API to look up the artifact URL.
129 132
       auto uri = web::uri_builder("/api/tenant/" + tenant + "/build");
130 133
       uri.append_path(buildid);
131 134
       auto response = client.request(
132 135
         web::http::methods::GET, uri.to_string()).get();
133 136
       // body is a web::json::value
134
-      // cout << response.status_code() << endl;
135 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 140
       // body["log_url"].as_string() returns a const std::string&
140 141
       cout << body["log_url"].as_string() << endl;
141 142
 
142 143
       cache.put(hostname, body["log_url"].as_string());
143 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
 }

Loading…
Cancel
Save