|
20 | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
21 | 21 | import com.fasterxml.jackson.databind.ObjectWriter; |
22 | 22 | import io.grpc.MethodDescriptor; |
| 23 | +import io.grpc.stub.ServerCallStreamObserver; |
23 | 24 | import io.grpc.stub.StreamObserver; |
24 | 25 | import org.apache.commons.lang3.time.StopWatch; |
25 | 26 | import org.opencb.commons.datastore.core.Query; |
@@ -183,39 +184,50 @@ public void run(MethodDescriptor<Request, ?> method, Request request, StreamObse |
183 | 184 |
|
184 | 185 | Exception e = null; |
185 | 186 | int numResults = -1; |
| 187 | + boolean cancelled = false; |
186 | 188 | try { |
187 | 189 | numResults = requestRunner.run(query, queryOptions); |
| 190 | + cancelled = isCancelled(streamObserver); |
| 191 | + if (cancelled) { |
| 192 | + logger.warn("Request cancelled by client"); |
| 193 | + } |
188 | 194 | streamObserver.onCompleted(); |
189 | 195 | } catch (Exception ex) { |
190 | 196 | e = ex; |
191 | 197 | logger.error("Catch error: " + e.getMessage(), e); |
192 | 198 | streamObserver.onError(ex); |
193 | 199 | } finally { |
194 | 200 | stopWatch.stop(); |
195 | | - logEnd(e, stopWatch, numResults, requestDescription); |
| 201 | + logEnd(e, cancelled, stopWatch, numResults, requestDescription); |
196 | 202 | } |
197 | 203 |
|
198 | 204 | } |
199 | 205 |
|
200 | | - private void logEnd(Exception e, StopWatch stopWatch, int numResults, String requestDescription) { |
| 206 | + protected boolean isCancelled(StreamObserver<?> streamObserver) { |
| 207 | + return streamObserver instanceof ServerCallStreamObserver |
| 208 | + && ((ServerCallStreamObserver<?>) streamObserver).isCancelled(); |
| 209 | + } |
| 210 | + |
| 211 | + private void logEnd(Exception e, boolean cancelled, StopWatch stopWatch, int numResults, String requestDescription) { |
201 | 212 | StringBuilder sb = new StringBuilder(); |
202 | | - boolean ok; |
203 | | - if (e == null) { |
204 | | - sb.append("OK"); |
205 | | - ok = true; |
206 | | - } else { |
| 213 | + if (e != null) { |
207 | 214 | sb.append("ERROR"); |
208 | | - ok = false; |
| 215 | + } else if (cancelled) { |
| 216 | + sb.append("CANCELLED"); |
| 217 | + } else { |
| 218 | + sb.append("OK"); |
209 | 219 | } |
210 | 220 | sb.append(", ").append(stopWatch.getTime(TimeUnit.MILLISECONDS)).append("ms"); |
211 | 221 | if (numResults >= 0) { |
212 | 222 | sb.append(", ").append(numResults).append(" results"); |
213 | 223 | } |
214 | 224 | sb.append(", ").append(requestDescription); |
215 | | - if (ok) { |
216 | | - logger.info(sb.toString()); |
217 | | - } else { |
| 225 | + if (e != null) { |
218 | 226 | logger.error(sb.toString()); |
| 227 | + } else if (cancelled) { |
| 228 | + logger.warn(sb.toString()); |
| 229 | + } else { |
| 230 | + logger.info(sb.toString()); |
219 | 231 | } |
220 | 232 | } |
221 | 233 | } |
0 commit comments