From f6fc591e8d74c2903ed13e41c01b6ac06d5940f0 Mon Sep 17 00:00:00 2001 From: jalmodovar Date: Mon, 27 Feb 2017 12:28:13 +0100 Subject: [PATCH] polling capabilities. --- build.sbt | 2 +- .../scala/services/ExecutableService.scala | 37 +++++++++++-------- src/main/scala/services/ServiceFactory.scala | 7 +++- .../utilities/ExecutableServiceConf.scala | 2 +- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/build.sbt b/build.sbt index 690e2ca..22716e0 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ name := "MixedEmotionsExampleOrchestrator" -version := "0.16" +version := "0.17" scalaVersion := "2.11.5" diff --git a/src/main/scala/services/ExecutableService.scala b/src/main/scala/services/ExecutableService.scala index 16a320b..f80e146 100644 --- a/src/main/scala/services/ExecutableService.scala +++ b/src/main/scala/services/ExecutableService.scala @@ -32,6 +32,7 @@ abstract class ExecutableService(serviceConf: ExecutableServiceConf, requestExec val pivotId = serviceConf.pivotId val requirementField = serviceConf.requirementField val requirementRegex = serviceConf.requirementRegex + val pollingCondition = serviceConf.pollingCondition def getIpAndPort(): (String, Int) @@ -44,19 +45,10 @@ abstract class ExecutableService(serviceConf: ExecutableServiceConf, requestExec logger.debug(s"Requirement not met: ${requirementRegex} in ${requirementField}") input } - /*val (ip, port) = getIpAndPort() - val url = ServiceConfCompleter.completeUrl(ip, port, requestUrl, input) - logger.debug("Executing Service:"+url) - val bodyContent = if(body.isDefined) Some(ServiceConfCompleter.completeBody(body.get, input)) else None - val fileUploadData : Option[Map[String, String]] = if(fileUploadConf.isDefined) ServiceConfCompleter.completeFileUploadData(fileUploadConf.get, input) else None - val response = requestExecutor.executeRequest(method, url, body=bodyContent, requestDelay = requestDelayMs, requestTimeout = requestTimeoutMs, - fileUploadData=fileUploadData) - val selectedResult = parseResponse(response, responsePath, responseMap, responseParseString, None) - logger.debug(s"SelectedResult: ${selectedResult}") - val result = input + ((outputField,selectedResult)) - result*/ + } + //this is the real execute def executeServiceAndObtainList(input: Map[String, Any]): List[Map[String, Any]] = { if (requirementMet(input)) { val selectedResult = performExecution(input) @@ -72,17 +64,28 @@ abstract class ExecutableService(serviceConf: ExecutableServiceConf, requestExec } } + //check if polling is defined the status def performExecution(input: Map[String, Any]): Any = { val (ip, port) = getIpAndPort() val url = ServiceConfCompleter.completeUrl(ip, port, requestUrl, input) logger.debug("Executing Service:"+url) val bodyContent = if(body.isDefined) Some(ServiceConfCompleter.completeBody(body.get, input)) else None val fileUploadData : Option[Map[String, String]] = if(fileUploadConf.isDefined) ServiceConfCompleter.completeFileUploadData(fileUploadConf.get, input) else None + val response = requestExecutor.executeRequest(method, url, body=bodyContent, requestDelay = requestDelayMs, requestTimeout = requestTimeoutMs, fileUploadData=fileUploadData) //logger.debug(s"Response: ${response}") - val selectedResult = parseResponse(response, responsePath, responseMap, responseParseString, pivotPath) + val selectedResult = parseResponse(response, responsePath, responseMap, responseParseString, pivotPath, pollingCondition) logger.debug(s"SelectedResult: ${selectedResult}") + if(pollingCondition.isDefined){ + if(!selectedResult.asInstanceOf[Some[List[String]]].get.contains(pollingCondition.get)){ + logger.debug("response service polling: "+selectedResult) + performExecution(input) + } else{ + logger.debug("polling condition is DONE: "+selectedResult) + } + } + selectedResult } @@ -115,7 +118,7 @@ abstract class ExecutableService(serviceConf: ExecutableServiceConf, requestExec } def parseResponse(response: String, responsePath: Option[String], responseMap: Option[Map[String,String]], - responseParseString: Option[String], pivotPath: Option[String]) : Any = { + responseParseString: Option[String], pivotPath: Option[String], pollingCondition: Option[String]) : Any = { if(responseParseString.isDefined){ val pattern = new Regex(responseParseString.get) val matchData = pattern.findFirstMatchIn(response) @@ -124,10 +127,11 @@ abstract class ExecutableService(serviceConf: ExecutableServiceConf, requestExec }else{ logger.debug(s"Pattern ${responseParseString.get} not found in ${response}") } - }else if(pivotPath.isDefined){ - JsonPathsTraversor.getJsonFlatMap(responseMap.get,pivotPath.get, response, deleteString) - + }else if(pivotPath.isDefined) { + JsonPathsTraversor.getJsonFlatMap(responseMap.get, pivotPath.get, response, deleteString) + }else if(pollingCondition.isDefined){ + JsonPathsTraversor.getJsonPath(responsePath.get, response, deleteString) }else if(responseMap.isDefined){ JsonPathsTraversor.getJsonMapPath(responseMap.get, response, deleteString) }else if(responsePath.isDefined){ @@ -164,6 +168,7 @@ abstract class ExecutableService(serviceConf: ExecutableServiceConf, requestExec } + //this is the real service def executeServiceAsList(jsonString: String): List[String] = { val temp = JSON.parseFull(jsonString).asInstanceOf[Option[Map[String,Any]]] temp match { diff --git a/src/main/scala/services/ServiceFactory.scala b/src/main/scala/services/ServiceFactory.scala index e53319e..992180c 100644 --- a/src/main/scala/services/ServiceFactory.scala +++ b/src/main/scala/services/ServiceFactory.scala @@ -37,6 +37,7 @@ object ServiceFactory { val PivotPathPath = "response.json.pivotPath" val RequirementFieldPath = "requirementField" val RequirementRegexPath = "requirementRegex" + val PollingCondition = "polling.condition" @@ -69,6 +70,7 @@ object ServiceFactory { } } + //this is the real execute def createAndExecuteListService(serviceName: String, configurationMap: Config): List[String] => List[String] = { logger.debug(s"Going to create and execute ${serviceName}") if(serviceName.startsWith("rest")){ @@ -77,7 +79,7 @@ object ServiceFactory { }else if(serviceName.startsWith("docker")){ val service = dockerService(serviceName.replace("docker_",""), configurationMap) service.executeServiceAsFlatMap - }else { + } else { throw new Exception(s"Service name '${serviceName}' starts with an unknown type. Service names should start with 'rest_' or 'docker_'") } } @@ -124,8 +126,9 @@ object ServiceFactory { val pivotPath: Option[String] = if(conf.hasPath(PivotPathPath)) Some(conf.getString(PivotPathPath)) else None val requirementField: Option[String] = if(conf.hasPath(RequirementFieldPath)) Some(conf.getString(RequirementFieldPath)) else None val requirementRegex: Option[String] = if(conf.hasPath(RequirementRegexPath)) Some(conf.getString(RequirementRegexPath)) else None + val pollingCondition: Option[String] = if(conf.hasPath(PollingCondition)) Some(conf.getString(PollingCondition)) else None new ExecutableServiceConf(requestUrl, method, body, outputField, responsePath, responseMap, deleteString, - requestDelayMs, requestTimeoutMs, fileUploadConf, responseParseString, pivotPath,pivotName, pivotId, requirementField, requirementRegex) + requestDelayMs, requestTimeoutMs, fileUploadConf, responseParseString, pivotPath,pivotName, pivotId, requirementField, requirementRegex, pollingCondition) } diff --git a/src/main/scala/utilities/ExecutableServiceConf.scala b/src/main/scala/utilities/ExecutableServiceConf.scala index 9c812e8..18deb2f 100644 --- a/src/main/scala/utilities/ExecutableServiceConf.scala +++ b/src/main/scala/utilities/ExecutableServiceConf.scala @@ -6,6 +6,6 @@ package utilities case class ExecutableServiceConf(requestUrl: String, method: String, body: Option[String], outputField:String, responsePath: Option[String], responseMap: Option[Map[String,String]], deleteString: Option[String], requestDelayMs: Int, requestTimeoutMs: Int, fileUploadConf: Option[Map[String, String]], responseParseString: Option[String], pivotPath: Option[String], - pivotName: Option[String], pivotId: Option[String], requirementField: Option[String], requirementRegex: Option[String]) { + pivotName: Option[String], pivotId: Option[String], requirementField: Option[String], requirementRegex: Option[String], pollingCondition: Option[String]) { }