conductor icon indicating copy to clipboard operation
conductor copied to clipboard

How to create a HTTPTask using conductor-client

Open smkb80 opened this issue 9 years ago • 24 comments

Hi Team,

I would like to create a HTTPTask using conductor-client apis. But the Type enum within WorkflowTask contains all the types barring HTTP.

` public static enum Type {

	SIMPLE, DYNAMIC, FORK_JOIN, FORK_JOIN_DYNAMIC, DECISION, JOIN, SUB_WORKFLOW, EVENT, WAIT, USER_DEFINED;
	
	private static Set<String> systemTasks = new HashSet<>();
	static {
		systemTasks.add(Type.SIMPLE.name());
		systemTasks.add(Type.DYNAMIC.name());
		systemTasks.add(Type.FORK_JOIN.name());
		systemTasks.add(Type.FORK_JOIN_DYNAMIC.name());
		systemTasks.add(Type.DECISION.name());
		systemTasks.add(Type.JOIN.name());
		systemTasks.add(Type.SUB_WORKFLOW.name());
		systemTasks.add(Type.EVENT.name());
		systemTasks.add(Type.WAIT.name());
		//Do NOT add USER_DEFINED here...
	}
	
	public static boolean is(String name) {
		return systemTasks.contains(name);
	}
}

`

Pls help me in defining both TaskDef and WorkflowTask for HTTPTask. Also, do I need to provide a Worker implementation for this?

Regards, Sameek

smkb80 avatar Mar 29 '17 12:03 smkb80

Hi Sameek,

The setter takes a string and you can pass HTTP as the task type (Case sensitive).

v1r3n avatar Mar 30 '17 06:03 v1r3n

Hi Viren,

Thanks for the response. But I am unable to set the input parameters of the HTTPTask.

Without any input parameter, I got the error message - "Missing HTTP request. Task input MUST have a 'http_request' key wiht HttpTask.Input as value. See documentation for HttpTask for required input parameters"

I understand there has to a input parameter named http_request. But what should be the mapping of the parameter? I've tried HttpTask.Input which didn't work and got the error message - "Invalid input expression for http_request , paramPathComponents.size=2, expression=HttpTask.Input"

I feel we've to set the mapping as HttpTask.Input.uri or likewise. But HttpTask.Input contains multiple parameters like uri, method, headers, body, accept, vipAddress and contentType. How do I map all these to http_request?

Though the documentation speaks on HTTPTask sample payload but there's nothing on how to create the HTTP task using conductor-client apis. Javadoc would also have helped in this case.

A sample code snippet to create the HTTPTask using conductor-client will be helpful because I am afraid that there will be future showstoppers after I am able to define the mapping correctly.

smkb80 avatar Mar 30 '17 11:03 smkb80

Hi Sameek,

Take a look at the kitchensink workflow that has a task with http:

https://netflix.github.io/conductor/metadata/kitchensink/

Here is the snippet of an http task:


      "name": "search_elasticsearch",
      "taskReferenceName": "get_es_1",
      "inputParameters": {
        "http_request": {
          "uri": "http://localhost:9200/conductor/_search?size=10",
          "method": "GET"
        }
      },
      "type": "HTTP"
    }

v1r3n avatar Mar 30 '17 14:03 v1r3n

Hi Viren,

I've understood this JSON to define a HTTPTask. But I am not creating this json manually and pushing it using Swagger to create a HTTPTask in the Conductor Server. Instead I am using conductor-client and conductor-common apis to define tasks and a workflow by using classes like:

com.netflix.conductor.common.metadata.tasks.TaskDef com.netflix.conductor.common.metadata.workflow.WorkflowTask

The WorkflowTask class accepts a Map<String>,<Object> as inputParameters. The comment just above the declaration in the WorkflowTask class says:

//Key: Name of the input parameter. MUST be one of the keys defined in TaskDef (e.g. fileName) //Value: mapping of the parameter from another task (e.g. task1.someOutputParameterAsFileName)

I understand key should be http_request in this case. But what should be the value in this case, so that the HTTPTask gets created properly? Passing HttpTask.Input as value didn't work.

Also, TaskDef accepts List<String> as inputKeys. I've put http_request in this list. Is that correct? Or anything else needs to be done?

I am looking forward to help/guidance in using conductor-client/common apis to define a HTTPTask as I am not creating the task definition json manually.

smkb80 avatar Mar 31 '17 06:03 smkb80

@sameekb, have you tried with output params for http task. in documentation there are 3 parameters categorised as output but not sure on how to use them. help on this if you have any idea on this.

amoolya19 avatar Apr 03 '17 09:04 amoolya19

Hi @amoolya19 ,

Yes, was able to process the output of the HTTPTask. While defining the task where you want to process the HTTPTask output, define the parameter value in the json as ${name_of_httpTask.output} . You will get the entire response including header and body.

If the HTTP response is a JSON, remember that Conductor unmarshals the json and passes the the response as nested Map objects. Before processing the response, just do a sysout on the response to understand the response that conductor passes to the next task. However, if the response cannot be parsed as JSON or Text, a string representation is stored as a text value.

smkb80 avatar Apr 03 '17 12:04 smkb80

Hi @sameekb , Thank you. i was able to get the output. Also have you tried HTTP Task Output with response parameter? i could not find any examples on this. let me know if you have succeeded in this.

amoolya19 avatar Apr 04 '17 12:04 amoolya19

Hi @amoolya19,

Do you mean request parameter?

smkb80 avatar Apr 04 '17 14:04 smkb80

@sameekb,

No. response. image

amoolya19 avatar Apr 05 '17 06:04 amoolya19

@amoolya19

response is the output of the HTTPTask which includes header, body and status code. If the response of the called service is a JSON, conductor unmarshals it and passes it as nested Map objects to the task processing the response. Within the processing task you've to programatically traverse through the Map objects to reach to the data you are interested in.

Before writing the traversal logic, just print the response in console/log. Once printed you'll get the JSON equivalent of the nested Map objects. Once the structure is known to you, coding the traversal logic will be easy for you.

If you look closely at the response, its nothing but a wrap on the HTTPResponse returned by the called service.

If the response is not JSON (say a XML), a string representation of the response is stored as a text value

Hope it helps!!

smkb80 avatar Apr 05 '17 06:04 smkb80

@sameekb

once i executed my workflow, i took the json from UI and i was able to see whatever my microservices were retruning in body. but dint get to know how to use response tag to capture the response from http task. is my understanding correct?

what i am asking is how to use reponse tag in json. for ex

"http_request": { "uri": "http://localhost:8081/", "method": "GET" "response": ? ${name_of_httpTask.output}? }

amoolya19 avatar Apr 05 '17 09:04 amoolya19

@amoolya19

No need to define response within http_request as its output of the HTTPTask. HTTPTask should contain inputParameters like uri, method, accept, contentType etc. Instead define a simple task which will process the response of the HTTPTask. While defining the above simple task define a inputParameter with the value ${name_of_httpTask.output}. In the corresponding Worker implementation of the simple task you need to execute the following line

task.getInputData().get("inputParameterName")

to get the response object in terms of nested maps if the HTTPResponse is a JSON. inputParameterName is the name of the parameter with the value ${name_of_httpTask.output}

smkb80 avatar Apr 05 '17 10:04 smkb80

@sameekb, Thank you. yes i have done the way you have explained. but just was wondering if i could use response tag. Also when i tried to input, like below [{ "name": "test_workflow", "description": "test http workflow", "version": 1, "tasks": [{ "name": "test_task1", "taskReferenceName": "tt0", "inputParameters": { "http_request": { "uri": "http://localhost:8081/2", "method": "GET" } }, "type": "HTTP" }, { "name": "test_task1", "taskReferenceName": "tt1", "inputParameters": { "http_request": { "uri": "http://localhost:8081/tt1", "method": "POST", "body" : "${tt0.output.number}" } }, "type": "HTTP" } ] "schemaVersion": 2 }]

output of tt0 is { "number": 2 }

now i try to put this in body for tt1 but it fails here. "taskType": "HTTP", "status": "FAILED", "inputData": { "http_request": { "uri": "http://localhost:8081/tt1", "method": "POST", "body": null } }, "referenceTaskName": "tt1",

body is null.

error is {timestamp=1491397088076, status=400, error=Bad Request, exception=org.springframework.http.converter.HttpMessageNotReadableException, message=Required request body is missing: public com.hp.model.ResponseBody com.hp.services.HelloController.tt1(com.hp.model.ResponseBody), path=/tt1}

amoolya19 avatar Apr 05 '17 11:04 amoolya19

@amoolya19

Both the task names are test_task1. Change the name of the second one to test_task2. Second task method is GET, but you are setting the parameter in body. Parameters in body should be used while using POST requests.

If localhost:8081 is a GET service, the URL in the second task should be http://localhost:8081/${test_task1.output.number}. Remove the body in the second task.

If the service is a POST service, in body you've to mention request_param_name : request_param_value . Request param name should be the name of the parameter that the service accepts. Request param value should be in this case ${test_task1.output.number}

smkb80 avatar Apr 05 '17 13:04 smkb80

@sameekb , i have actually tried with post as well but luck. i changed only request_param_name : request_param_value as you suggested. but still the value is getting passed. i dont want to pass the parameter through url to the second task. i want it to be in body.

Task1 image

Task2 image

screen shots if you can see, task 1 gives out as {"number": 2}. but in task 2 input is "body": { "number": null }

i have created 2 seperate tasks. my workflow json looks like

[{ "name": "test_workflow", "description": "test http workflow", "version": 1, "tasks": [{ "name": "test_task1", "taskReferenceName": "tt1", "inputParameters": { "http_request": { "uri": "http://localhost:8081/2", "method": "GET" } }, "type": "HTTP" }, { "name": "test_task2", "taskReferenceName": "tt2", "inputParameters": { "http_request": { "uri": "http://localhost:8081/tt1", "method": "POST", "body": { "number": "${tt0.output.number}" } } }, "type": "HTTP" }], "schemaVersion": 2 }]

amoolya19 avatar Apr 06 '17 09:04 amoolya19

@amoolya19

Is localhost:8081 a GET service or Post service? In test_task1, the URI is getting invoked as GET whereas in test_task2 it is getting invoked as POST.

Since, it gives out {"number": 2} in test_task1, I assume its GET service.

If localhost:8081 designed to handle both GET and POST, then only test_task2 will work which sends out a POST request. The uri in test_task2 should be http://localhost:8081/ if you want to send a POST request.

In the body of test_task2 you've mentioned '${tt0.output.number}'. Now I don't see any task with tt0 as name or taskReferenceName. Whenever you use placeholders you've to use name (and not taskReferenceName) of the task whose output variable value has to be passed. Since, Conductor can't find a task with the name tt0, it passing null to test_task2 body.

So, if you want pass the output of test_task1, your body should look like :

"body": { "number": "${test_task1.output.number}" }

If ${test_task1.output.number} doesn't work (I doubt it would), then you've to introduce a simple task with input parameter having value ${test_task1.output} between test_task1 and test_task2. So, the simple task will have the entire response in terms of nested maps. Then you've to implement a corresponding worker which will traverse through the response, extract the of value of number and populate the value in another output variable. This output variable of the simple task will be an input to the test_task2. The input variable in test_task2 should have the value "${name_of_simple_task.output.name_of_output_parameter}".

smkb80 avatar Apr 06 '17 09:04 smkb80

@sameekb sorry to confuse you. my WS has 2 endpoints. one is get and other is post. task1 calls

 @RequestMapping(value ="/{id}", method=RequestMethod.GET , produces={"application/json"})
    public ResponseBody home(@PathVariable("id") int number) { //something }

task2 calls

@RequestMapping(value ="/tt1", method=RequestMethod.POST , produces={"application/json"}) public ResponseBody tt1(@RequestBody ResponseBody response) { //something}

i even added below but even count is null. "outputParameters": { "count": "${test_task1.output.number}" },

could you please elobrate on "implement a corresponding worker".

amoolya19 avatar Apr 06 '17 10:04 amoolya19

@amoolya19

Pls visit the below URL for Worker implementation

Conductor Worker

smkb80 avatar Apr 06 '17 10:04 smkb80

@sameekb ,

will try that. the reason i was using taskreference name looking at kitchen sink example. { "name": "search_elasticsearch", "taskReferenceName": "get_es_1", "inputParameters": { "http_request": { "uri": "http://localhost:9200/conductor/_search?size=10", "method": "GET" } }, "type": "HTTP" }, { "name": "task_30", "taskReferenceName": "task_30", "inputParameters": { "statuses": "${get_es_1.output..status}", "workflowIds": "${get_es_1.output..workflowId}" }, "type": "SIMPLE" }

will try simple task.

amoolya19 avatar Apr 06 '17 10:04 amoolya19

Yeah. It should be taskReferenceName

Since I used same name and taskReferenceName in my POC, didn't face problem wrt this.

smkb80 avatar Apr 06 '17 10:04 smkb80