Skip to content

Commit

Permalink
polling capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
jalmodovar committed Feb 27, 2017
1 parent d32aaea commit f6fc591
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "MixedEmotionsExampleOrchestrator"

version := "0.16"
version := "0.17"

scalaVersion := "2.11.5"

Expand Down
37 changes: 21 additions & 16 deletions src/main/scala/services/ExecutableService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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){
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions src/main/scala/services/ServiceFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ object ServiceFactory {
val PivotPathPath = "response.json.pivotPath"
val RequirementFieldPath = "requirementField"
val RequirementRegexPath = "requirementRegex"
val PollingCondition = "polling.condition"



Expand Down Expand Up @@ -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")){
Expand All @@ -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_'")
}
}
Expand Down Expand Up @@ -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)
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/utilities/ExecutableServiceConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {

}

0 comments on commit f6fc591

Please sign in to comment.