From 507493cd2e62c26992912e0e294b014a709d68ca Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Wed, 8 Feb 2017 18:34:05 +0000 Subject: [PATCH] added SendNotification class to send notifications on failure git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-data-indexer-se-plugin@142412 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 7 + .../SocialDataIndexerPlugin.java | 21 +-- .../SocialDataIndexerPluginDeclaration.java | 5 +- .../utils/SendNotification.java | 131 ++++++++++++++++++ 4 files changed, 144 insertions(+), 20 deletions(-) create mode 100644 src/main/java/org/gcube/socialnetworking/socialdataindexer/utils/SendNotification.java diff --git a/pom.xml b/pom.xml index 333ef8f..56166ff 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ social-networking 2.2.0 18.0 + 4.1.2 2.0.2 @@ -119,6 +120,12 @@ 4.11 test + + org.apache.httpcomponents + httpclient + ${apache.http.version} + provided + diff --git a/src/main/java/org/gcube/socialnetworking/socialdataindexer/SocialDataIndexerPlugin.java b/src/main/java/org/gcube/socialnetworking/socialdataindexer/SocialDataIndexerPlugin.java index 39a1081..8c4d9a7 100644 --- a/src/main/java/org/gcube/socialnetworking/socialdataindexer/SocialDataIndexerPlugin.java +++ b/src/main/java/org/gcube/socialnetworking/socialdataindexer/SocialDataIndexerPlugin.java @@ -53,19 +53,11 @@ public class SocialDataIndexerPlugin extends Plugin hostsToContact; - - // private port number private List portNumbers; - // the elasticsearch client private TransportClient client; - - // connection to cassandra private DatabookStore store; private int count = 0; @@ -77,9 +69,10 @@ public class SocialDataIndexerPlugin extends Plugin inputs){ + public void launch(Map inputs) throws Exception{ try{ @@ -89,11 +82,6 @@ public class SocialDataIndexerPlugin extends Plugin pluginInputs; + private static Logger logger = LoggerFactory.getLogger(SendNotification.class); + private static final String NOTIFY_METHOD = "/2/notifications/notify-job-status"; + private static final String resource = "jersey-servlet"; + private static final String serviceName = "SocialNetworking"; + private static final String serviceClass = "Portal"; + + // user to contact on exception via social-networking ws + private final static String RECIPIENT_KEY = "recipient"; + + // service name + private final static String SERVICE_NAME = "Smart Executor"; + + public SendNotification(Map inputs) { + super(inputs); + this.pluginInputs = inputs; + } + + @SuppressWarnings("unchecked") + @Override + public void pluginStateEvolution(PluginStateEvolution pluginStateEvolution, + Exception exception) throws Exception { + + switch(pluginStateEvolution.getPluginState()){ + + case STOPPED: + case FAILED: + case DISCARDED: + + // check what happened + String recipient = pluginInputs.get(RECIPIENT_KEY); + String basePath = discoverEndPoint(); + logger.info("Recipient of the notification is " + recipient + ". Base path found for the notification service is " + basePath); + + if(basePath != null && recipient != null){ + basePath = basePath.contains("http:") ? basePath.replace("http", "https") : basePath; + HttpPost request = new HttpPost(basePath + NOTIFY_METHOD); + DefaultHttpClient httpClient = new DefaultHttpClient(); + JSONObject obj = new JSONObject(); + obj.put("job_id", pluginStateEvolution.getUuid()); + obj.put("recipient", recipient); + obj.put("job_name", pluginStateEvolution.getPluginDeclaration().getName()); + obj.put("service_name", SERVICE_NAME); + obj.put("status", "FAILED"); + obj.put("status_message", "original status reported by " + SERVICE_NAME + " was " + pluginStateEvolution.getPluginState() + + ". Exception is " + exception != null ? exception.getMessage() : null); + + logger.debug("Request json is going to be " + obj.toJSONString()); + try{ + + request.addHeader("gcube-token", SecurityTokenProvider.instance.get()); + StringEntity params = new StringEntity(obj.toJSONString()); + request.setEntity(params); + HttpResponse response = httpClient.execute(request); + logger.info(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase()); + + }catch (Exception ex) { + logger.error("Error while sending notification ", ex); + }finally{ + if(httpClient != null) + httpClient.getConnectionManager().shutdown(); + } + } + break; + default: logger.info("No notification is going to be sent, because the status of the plugin execution is " + pluginStateEvolution.getPluginState().name()); + } + } + + /** + * Discover the social networking service base path. + * @return base path of the service. + */ + private static String discoverEndPoint(){ + String context = ScopeProvider.instance.get(); + String basePath = null; + try{ + + SimpleQuery query = queryFor(GCoreEndpoint.class); + query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'",serviceClass)); + query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'"); + query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'",serviceName)); + query.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""+resource+"\"]/text()"); + + DiscoveryClient client = client(); + List endpoints = client.submit(query); + if (endpoints == null || endpoints.isEmpty()) + throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context); + + basePath = endpoints.get(0); + if(basePath==null) + throw new Exception("Endpoint:"+resource+", is null for serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context); + + logger.info("found entyname "+basePath+" for ckanResource: "+resource); + + }catch(Exception e){ + logger.error("Unable to retrieve such service endpoint information!", e); + } + + return basePath; + } + +}