this.onSuccessUrl = conf.get(Constants.HttpCallback.SUCCESS_URL); this.onFailureUrl = conf.get(Constants.HttpCallback.FAILURE_URL); this.startTriggerActions = parseActionsString(conf.get(Constants.HttpCallback.START_TRIGGERS, Constants.HttpCallback.DEFAULT_START_TRIGGERS)); this.successTriggerActions = parseActionsString(conf.get(Constants.HttpCallback.SUCCESS_TRIGGERS, Constants.HttpCallback.DEFAULT_SUCCESS_TRIGGERS)); this.failureTriggerActions = parseActionsString(conf.get(Constants.HttpCallback.FAILURE_TRIGGERS, Constants.HttpCallback.DEFAULT_FAILURE_TRIGGERS)); if (onStartUrl != null) {
public boolean onStart(CallbackData data, CallbackContext context) { ClusterAction jobAction = data.getJob().getClusterAction(); if (startTriggerActions.contains(jobAction)) { if (onStartUrl != null) { LOG.debug("sending request to {} before performing {} on cluster {}", onStartUrl, jobAction, data.getCluster().getId()); sendPost(onStartUrl, data, context); } } return true; }
@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); }
@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()); }
public void onSuccess(CallbackData data, CallbackContext context) { ClusterAction jobAction = data.getJob().getClusterAction(); if (successTriggerActions.contains(data.getJob().getClusterAction())) { if (onSuccessUrl != null) { LOG.debug("{} completed successfully on cluster {}, sending request to {}", jobAction, data.getCluster().getId(), onSuccessUrl); sendPost(onSuccessUrl, data, context); } } }
@Override public void onFailure(CallbackData data, CallbackContext context) { ClusterAction jobAction = data.getJob().getClusterAction(); if (failureTriggerActions.contains(data.getJob().getClusterAction())) { if (onFailureUrl != null) { LOG.debug("{} failed on cluster {}, sending request to {}", jobAction, data.getCluster().getId(), onFailureUrl); sendPost(onFailureUrl, data, context); } } }