/** * Take a task from the queue to execute. Post body must contain a workerId key, which must also be passed back * when finishing a task. Tasks are returned as a json object with taskId, jobId, clusterId, taskName and nodeId * as key value pairs, and with a config key whose value is a json object with all the configuration settings. * * @param request The request to take a task. * @param responder Responder to send the response. */ @POST @Path("/take") public void handleTakeTask(HttpRequest request, HttpResponder responder) { TakeTaskRequest takeRequest = HttpHelper.decodeRequestBody(request, responder, TakeTaskRequest.class, gson); try { String taskJson = taskQueueService.takeNextClusterTask(takeRequest); if (taskJson == null) { responder.sendStatus(HttpResponseStatus.NO_CONTENT); return; } responder.sendString(HttpResponseStatus.OK, taskJson); } catch (IOException e) { LOG.error("Exception while taking task.", e); responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error taking task."); } catch (MissingEntityException e) { responder.sendError(HttpResponseStatus.FORBIDDEN, "Provisioner " + takeRequest.getProvisionerId() + " is not registered."); } }