@Override public TakeTaskRequest deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObj = json.getAsJsonObject(); String workerId = context.deserialize(jsonObj.get("workerId"), String.class); String provisionerId = context.deserialize(jsonObj.get("provisionerId"), String.class); String tenantId = context.deserialize(jsonObj.get("tenantId"), String.class); return new TakeTaskRequest(workerId, provisionerId, tenantId); } }
String tenantId = takeRequest.getTenantId(); String provisionerId = takeRequest.getProvisionerId(); String workerId = takeRequest.getWorkerId(); String consumerId = provisionerId + "." + workerId;
/** * 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."); } }
@Test public void testTakeTaskForDeadProvisionerErrors() throws Exception { TakeTaskRequest takeRequest = new TakeTaskRequest("workerX", "nonexistant-provider", "tenantY"); assertResponseStatus(doPostInternalAPI("/tasks/take", gson.toJson(takeRequest)), HttpResponseStatus.FORBIDDEN); }
private TakeTaskRequest getRequest() throws IOException { String tenantId = USER1_ACCOUNT.getTenantId(); ClusterTask clusterTask = new ClusterTask( ProvisionerAction.CREATE, TaskId.fromString("1-1-1"), "node_id", "service", ClusterAction.CLUSTER_CREATE, "test", USER1_ACCOUNT); clusterStore.writeClusterTask(clusterTask); ClusterJob clusterJob = new ClusterJob(JobId.fromString("1-1"), ClusterAction.CLUSTER_CREATE); clusterStore.writeClusterJob(clusterJob); TaskConfig taskConfig = new TaskConfig( NodeProperties.builder().build(), Entities.ProviderExample.JOYENT, ImmutableMap.<String, NodeProperties>of(), new TaskServiceAction("svcA", new ServiceAction("shell", ImmutableMap.<String, String>of())), new JsonObject(), new JsonObject() ); SchedulableTask schedulableTask= new SchedulableTask(clusterTask, taskConfig); provisionerQueues.add(tenantId, new Element(clusterTask.getTaskId(), gson.toJson(schedulableTask))); return new TakeTaskRequest("worker1", PROVISIONER_ID, TENANT_ID); }
@Test public void testTakeTask() throws Exception { String tenantId = USER1_ACCOUNT.getTenantId(); ClusterTask clusterTask = new ClusterTask( ProvisionerAction.CREATE, TaskId.fromString("1-1-1"), "node_id", "service", ClusterAction.CLUSTER_CREATE, "test", USER1_ACCOUNT); clusterStore.writeClusterTask(clusterTask); ClusterJob clusterJob = new ClusterJob(JobId.fromString("1-1"), ClusterAction.CLUSTER_CREATE); clusterStore.writeClusterJob(clusterJob); TaskConfig taskConfig = new TaskConfig( NodeProperties.builder().build(), Entities.ProviderExample.JOYENT, ImmutableMap.<String, NodeProperties>of(), new TaskServiceAction("svcA", new ServiceAction("shell", ImmutableMap.<String, String>of())), new JsonObject(), new JsonObject() ); SchedulableTask schedulableTask= new SchedulableTask(clusterTask, taskConfig); provisionerQueues.add(tenantId, new Element(clusterTask.getTaskId(), gson.toJson(schedulableTask))); TakeTaskRequest takeRequest = new TakeTaskRequest("worker1", PROVISIONER_ID, TENANT_ID); HttpResponse response = doPostInternalAPI("/tasks/take", gson.toJson(takeRequest)); assertResponseStatus(response, HttpResponseStatus.OK); JsonObject responseJson = getResponseJson(response); Assert.assertEquals(clusterTask.getTaskId(), responseJson.get("taskId").getAsString()); }
ClusterAction.CLUSTER_CREATE, 3, 0); TakeTaskRequest takeRequest = new TakeTaskRequest("workerX", PROVISIONER_ID, tenantId);
provisionerQueues.add(tenantId, new Element(clusterTask.getTaskId(), gson.toJson(schedulableTask))); TakeTaskRequest takeRequest = new TakeTaskRequest("worker1", PROVISIONER_ID, tenantId); SchedulableTask task = TestHelper.takeTask(getBaseUrlInternalAPI(), takeRequest); Assert.assertEquals(clusterTask.getTaskId(), task.getTaskId());
private void testCallbacks(boolean failJob) throws Exception { ClusterScheduler clusterScheduler = injector.getInstance(ClusterScheduler.class); String tenantId = cluster.getAccount().getTenantId(); clusterQueues.add(tenantId, new Element(cluster.getId(), ClusterAction.CLUSTER_CREATE.name())); clusterScheduler.run(); CallbackScheduler callbackScheduler = injector.getInstance(CallbackScheduler.class); // should be no job in the queue until the start callback runs Assert.assertEquals(0, jobQueues.size(tenantId)); waitForCallback(callbackScheduler); Assert.assertEquals(CallbackData.Type.START, mockClusterCallback.getReceivedCallbacks().get(0).getType()); JobScheduler jobScheduler = injector.getInstance(JobScheduler.class); jobScheduler.run(); // take tasks until there are no more TakeTaskRequest takeRequest = new TakeTaskRequest("consumer1", PROVISIONER_ID, tenantId); SchedulableTask task = TestHelper.takeTask(getInternalServerUrl(), takeRequest); while (task != null) { FinishTaskRequest finishRequest = new FinishTaskRequest("consumer1", PROVISIONER_ID, tenantId, task.getTaskId(), null, null, failJob ? 1 : 0, null, null, null); TestHelper.finishTask(getInternalServerUrl(), finishRequest); jobScheduler.run(); jobScheduler.run(); task = TestHelper.takeTask(getInternalServerUrl(), takeRequest); } jobScheduler.run(); waitForCallback(callbackScheduler); // at this point, the failure callback should have run Assert.assertEquals(failJob ? CallbackData.Type.FAILURE : CallbackData.Type.SUCCESS, mockClusterCallback.getReceivedCallbacks().get(1).getType()); }
SchedulableTask task = TestHelper.takeTask(getBaseUrlInternalAPI(), new TakeTaskRequest("workerX", PROVISIONER_ID, tenantId)); Assert.assertNull(task); jobScheduler.run();
ClusterAction.CLUSTER_CREATE, 3, 0); SchedulableTask task = TestHelper.takeTask(getBaseUrlInternalAPI(), new TakeTaskRequest("workerX", PROVISIONER_ID, tenantId));
jobScheduler.run(); TakeTaskRequest takeRequest = new TakeTaskRequest("consumer1", PROVISIONER_ID, tenantId); SchedulableTask task = TestHelper.takeTask(getInternalServerUrl(), takeRequest);
provisionerQueues.add(tenantId, new Element(clusterTask.getTaskId(), gson.toJson(schedulableTask))); TakeTaskRequest takeRequest = new TakeTaskRequest("worker1", PROVISIONER_ID, tenantId); SchedulableTask task = TestHelper.takeTask(getBaseUrlInternalAPI(), takeRequest); Assert.assertEquals(clusterTask.getTaskId(), task.getTaskId());
TestHelper.takeTask(getBaseUrlInternalAPI(), new TakeTaskRequest("workerX", PROVISIONER_ID, tenantId));
ClusterAction.CLUSTER_DELETE, 2, 0); TakeTaskRequest takeRequest = new TakeTaskRequest("workerX", PROVISIONER_ID, tenantId);
TakeTaskRequest takeRequest = new TakeTaskRequest("consumer1", PROVISIONER_ID, tenantId); SchedulableTask task = TestHelper.takeTask(getInternalServerUrl(), takeRequest);
ClusterAction.CLUSTER_CREATE, 6, 0); TakeTaskRequest takeRequest = new TakeTaskRequest("workerX", PROVISIONER_ID, tenantId);
ClusterAction.CLUSTER_CREATE, 6, 0); TakeTaskRequest takeRequest = new TakeTaskRequest("workerX", PROVISIONER_ID, tenantId);
callbackScheduler.run(); TakeTaskRequest takeRequest = new TakeTaskRequest("workerX", PROVISIONER_ID, tenantId); assertStatusWithUser1(clusterId, Cluster.Status.PENDING, ClusterJob.Status.RUNNING, ClusterAction.CLUSTER_CREATE, 3, 0);