Skip to content

Commit 64adb03

Browse files
authoredSep 14, 2021
examples: custom task for runtime-v2 (walmartlabs#469)
1 parent 155d8dd commit 64adb03

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed
 

‎examples/custom_task/pom.xml

+28-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1616

17-
<concord.version>1.12.0</concord.version>
17+
<concord.version>1.88.0</concord.version>
1818
<jackson.version>2.9.8</jackson.version>
1919
<okhttp.version>3.12.1</okhttp.version>
2020
</properties>
@@ -27,6 +27,14 @@
2727
<version>${concord.version}</version>
2828
<scope>provided</scope>
2929
</dependency>
30+
<!-- for runtime-v2 support -->
31+
<dependency>
32+
<groupId>com.walmartlabs.concord.runtime.v2</groupId>
33+
<artifactId>concord-runtime-sdk-v2</artifactId>
34+
<version>${concord.version}</version>
35+
<scope>provided</scope>
36+
</dependency>
37+
3038
<dependency>
3139
<groupId>javax.inject</groupId>
3240
<artifactId>javax.inject</artifactId>
@@ -69,4 +77,23 @@
6977
<scope>test</scope>
7078
</dependency>
7179
</dependencies>
80+
81+
<build>
82+
<plugins>
83+
<!-- required for runtime-v2 -->
84+
<plugin>
85+
<groupId>org.eclipse.sisu</groupId>
86+
<artifactId>sisu-maven-plugin</artifactId>
87+
<version>0.3.3</version>
88+
<executions>
89+
<execution>
90+
<id>index</id>
91+
<goals>
92+
<goal>main-index</goal>
93+
</goals>
94+
</execution>
95+
</executions>
96+
</plugin>
97+
</plugins>
98+
</build>
7299
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.walmartlabs.concord.examples.customtask;
2+
3+
import com.walmartlabs.concord.runtime.v2.sdk.Task;
4+
5+
import javax.inject.Named;
6+
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.walmartlabs.concord.runtime.v2.sdk.TaskResult;
9+
import com.walmartlabs.concord.runtime.v2.sdk.Variables;
10+
import okhttp3.OkHttpClient;
11+
import okhttp3.Request;
12+
import okhttp3.Response;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
17+
/**
18+
* A custom task example. All tasks must implement {@link Task} interface and
19+
* be marked with {@link Named} annotation.
20+
*
21+
* The task fetches a specified URL and parses the response as JSON, converting
22+
* the data into regular Java objects.
23+
*/
24+
@Named("customTask")
25+
public class CustomTaskV2 implements Task {
26+
27+
private static final Logger log = LoggerFactory.getLogger(CustomTaskV2.class);
28+
29+
@Override
30+
public TaskResult execute(Variables input) throws Exception {
31+
String url = input.assertString("url");
32+
33+
OkHttpClient client = new OkHttpClient();
34+
Request req = new Request.Builder()
35+
.url(url)
36+
.build();
37+
38+
try (Response resp = client.newCall(req).execute()) {
39+
if (!resp.isSuccessful()) {
40+
log.warn("Error while fetching {}: {}", url, resp.code());
41+
return TaskResult.error("Error while fetching")
42+
.value("errorCode", resp.code());
43+
}
44+
45+
ObjectMapper om = new ObjectMapper();
46+
Object data = om.readValue(resp.body().byteStream(), Object.class);
47+
48+
return TaskResult.success()
49+
.value("data", data);
50+
}
51+
}
52+
}

‎examples/custom_task/test-v2.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
configuration:
2+
runtime: concord-v2
3+
4+
flows:
5+
default:
6+
- task: customTask
7+
in:
8+
url: "https://jsonplaceholder.typicode.com/todos/1"
9+
out: result
10+
11+
- if: "${!result.ok}"
12+
then:
13+
- throw: "The request returned ${result.errorCode}"
14+
15+
- log: "Data: ${result.data}"

‎examples/custom_task/test.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
set -e
44

55
SERVER_ADDR="$1"
6+
CONCORD_YAML="$2"
67

78
# build the task
89
mvn clean package -DskipTests
@@ -11,7 +12,7 @@ mvn clean package -DskipTests
1112
rm -rf target/test && mkdir -p target/test
1213

1314
# copy the test flow
14-
cp test.yml target/test/concord.yml
15+
cp ${CONCORD_YAML:-test.yml} target/test/concord.yml
1516

1617
# copy the task's JAR...
1718
mkdir -p target/test/lib/

0 commit comments

Comments
 (0)
Please sign in to comment.