/** * Sets the status of the given job to {@link ClusterJob.Status#RUNNING} and add it to the queue to be run. * * @param job Job to start. * @param cluster Cluster the job is for. * @throws IOException */ public void startJob(ClusterJob job, Cluster cluster) throws IOException { // TODO: wrap in a transaction LOG.debug("Starting job {} for cluster {}", job.getJobId(), cluster.getId()); job.setJobStatus(ClusterJob.Status.RUNNING); // Note: writing job status as RUNNING, will allow other operations on the job // (like cancel, etc.) to happen in parallel. clusterStore.writeClusterJob(job); callbackQueues.add(cluster.getAccount().getTenantId(), new Element(gson.toJson(new CallbackData(CallbackData.Type.START, cluster, job)))); }
/** * Sets the status of the given job to {@link ClusterJob.Status#FAILED} and the status of the cluster to some given * status. * * @param job Job to fail. * @param cluster Cluster to set the status for. * @param status Status to set the cluster to. * @param message Error message. * @throws IOException * @throws IllegalAccessException */ public void failJobAndSetClusterStatus(ClusterJob job, Cluster cluster, Cluster.Status status, String message) throws IOException, IllegalAccessException { cluster.setStatus(status); clusterStore.writeCluster(cluster); job.setJobStatus(ClusterJob.Status.FAILED); if (message != null) { job.setStatusMessage(message); } clusterStore.writeClusterJob(job); serverStats.getFailedClusterStats().incrementStat(job.getClusterAction()); callbackQueues.add(cluster.getAccount().getTenantId(), new Element(gson.toJson(new CallbackData(CallbackData.Type.FAILURE, cluster, job)))); }
/** * Sets the status of the given job to {@link ClusterJob.Status#COMPLETE} and the status of the given cluster to * {@link co.cask.coopr.cluster.Cluster.Status#ACTIVE}. * * @param job Job to complete. * @param cluster Cluster the job was for. * @throws IOException */ public void completeJob(ClusterJob job, Cluster cluster) throws IOException, IllegalAccessException { job.setJobStatus(ClusterJob.Status.COMPLETE); clusterStore.writeClusterJob(job); LOG.debug("Job {} is complete", job.getJobId()); // Update cluster status if (job.getClusterAction() == ClusterAction.CLUSTER_DELETE) { cluster.setStatus(Cluster.Status.TERMINATED); } else { cluster.setStatus(Cluster.Status.ACTIVE); } clusterStore.writeCluster(cluster); serverStats.getSuccessfulClusterStats().incrementStat(job.getClusterAction()); if (job.getClusterAction() == ClusterAction.CLUSTER_DELETE) { wipeSensitiveFields(cluster); } callbackQueues.add(cluster.getAccount().getTenantId(), new Element(gson.toJson(new CallbackData(CallbackData.Type.SUCCESS, cluster, job)))); }
@Test public void testOnStartIsTrueWithBadURL() { HttpPostClusterCallback callback = new HttpPostClusterCallback(); conf = Configuration.create(); conf.set(Constants.HttpCallback.START_URL, "malformed-url"); callback.initialize(conf); ClusterJob job = new ClusterJob(new JobId(cluster.getId(), 1), ClusterAction.CLUSTER_CREATE); CallbackData data = new CallbackData(CallbackData.Type.START, cluster, job); CallbackContext context = new CallbackContext(clusterStoreService, userStore, cluster.getAccount()); Assert.assertTrue(callback.onStart(data, context)); }
@Test public void testTriggers() { HttpPostClusterCallback callback = new HttpPostClusterCallback(); String base = "http://" + host + ":" + port; conf = Configuration.create(); conf.set(Constants.HttpCallback.START_URL, base + "/start/endpoint"); conf.set(Constants.HttpCallback.START_TRIGGERS, ClusterAction.CLUSTER_CONFIGURE.name()); callback.initialize(conf); // should not get triggered ClusterJob job = new ClusterJob(new JobId(cluster.getId(), 1), ClusterAction.CLUSTER_CREATE); CallbackData data = new CallbackData(CallbackData.Type.START, cluster, job); CallbackContext context = new CallbackContext(clusterStoreService, userStore, cluster.getAccount()); callback.onStart(data, context); Assert.assertEquals(0, handler.getStartCount()); // should get triggered job = new ClusterJob(new JobId(cluster.getId(), 1), ClusterAction.CLUSTER_CONFIGURE); data = new CallbackData(CallbackData.Type.START, cluster, job); callback.onStart(data, context); Assert.assertEquals(1, handler.getStartCount()); }
@Test public void testCalls() { HttpPostClusterCallback callback = new HttpPostClusterCallback(); String base = "http://" + host + ":" + port; conf = Configuration.create(); conf.set(Constants.HttpCallback.START_URL, base + "/start/endpoint"); conf.set(Constants.HttpCallback.SUCCESS_URL, base + "/success/endpoint"); conf.set(Constants.HttpCallback.FAILURE_URL, base + "/failure/endpoint"); callback.initialize(conf); ClusterJob job = new ClusterJob(new JobId(cluster.getId(), 1), ClusterAction.CLUSTER_CREATE); CallbackData data = new CallbackData(CallbackData.Type.START, cluster, job); CallbackContext context = new CallbackContext(clusterStoreService, userStore, cluster.getAccount()); callback.onStart(data, context); callback.onSuccess(data, context); callback.onSuccess(data, context); callback.onFailure(data, context); Assert.assertEquals(handler.getStartCount(), 1); Assert.assertEquals(handler.getFailureCount(), 1); Assert.assertEquals(handler.getSuccessCount(), 2); }