@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); RemoteWorkResponse.Builder workResponse = RemoteWorkResponse.newBuilder(); // TODO(alpha): Choose a better temp directory name. Path tempRoot = workPath.getRelative("build-" + UUID.randomUUID().toString()); FileSystemUtils.createDirectoryAndParents(tempRoot); boolean deleteTree = true; try { String workJson = CharStreams.toString(request.getReader()); RemoteWorkRequest.Builder workRequest = RemoteWorkRequest.newBuilder(); JsonFormat.parser().merge(workJson, workRequest); RemoteWorkRequest work = workRequest.build(); final MemcacheActionCache actionCache = new MemcacheActionCache(tempRoot, remoteOptions, cache); final MemcacheWorkExecutor workExecutor = new MemcacheWorkExecutor(actionCache, executorService, tempRoot); if (options.debug) { System.out.println( "[INFO] Work received has " + work.getInputFilesCount() + " inputs files and " + work.getOutputFilesCount() + " output files."); } RemoteWorkExecutor.Response executorResponse = workExecutor.submit(work).get(); if (options.debug) { if (!executorResponse.success()) { deleteTree = false; System.out.println("[WARNING] Work failed."); System.out.println(workJson); } else { System.out.println("[INFO] Work completed."); } } workResponse .setSuccess(executorResponse.success()) .setOut(executorResponse.getOut()) .setErr(executorResponse.getErr()); } catch (Exception e) { workResponse.setSuccess(false).setOut("").setErr("").setException(e.toString()); } finally { if (deleteTree) { FileSystemUtils.deleteTree(tempRoot); } else { System.out.println("[WARNING] Preserving work directory " + tempRoot.toString() + "."); } response.setStatus(HttpServletResponse.SC_OK); response.getWriter().print(JsonFormat.printer().print(workResponse.build())); } }
@Override public void executeSynchronously( RemoteWorkRequest request, StreamObserver<RemoteWorkResponse> responseObserver) { Path tempRoot = workPath.getRelative("build-" + UUID.randomUUID().toString()); try { FileSystemUtils.createDirectoryAndParents(tempRoot); final ConcurrentMapActionCache actionCache = new ConcurrentMapActionCache(tempRoot, cache); final MemcacheWorkExecutor workExecutor = MemcacheWorkExecutor.createLocalWorkExecutor(actionCache, tempRoot); if (LOG_FINER) { LOG.fine( "Work received has " + request.getInputFilesCount() + " input files and " + request.getOutputFilesCount() + " output files."); } RemoteWorkResponse response = workExecutor.executeLocally(request); responseObserver.onNext(response); if (options.debug) { if (!response.getSuccess()) { LOG.warning("Work failed. Request: " + request.toString() + "."); } else if (LOG_FINER) { LOG.fine("Work completed."); } } if (!options.debug || response.getSuccess()) { FileSystemUtils.deleteTree(tempRoot); } else { LOG.warning("Preserving work directory " + tempRoot.toString() + "."); } } catch (IOException | InterruptedException e) { RemoteWorkResponse.Builder response = RemoteWorkResponse.newBuilder(); response.setSuccess(false).setOut("").setErr("").setException(e.toString()); responseObserver.onNext(response.build()); if (e instanceof InterruptedException) { Thread.currentThread().interrupt(); } } finally { responseObserver.onCompleted(); } }