This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workflow as Code SDK for Conductor (#2856)
* Java SDK * Update Javascript.java * Update Javascript.java * refactor and clean up * Update WorkflowCreationTests.java * Update workflow_sdk.md * Update WorkflowCreationTests.java * Add event task * Update WorkflowExecutor.java * Update WorkflowExecutor.java * make fields private
- Loading branch information
Showing
59 changed files
with
7,189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# SDK for Conductor | ||
Conductor SDK allows developers to create, test and execute workflows using code. | ||
|
||
There are three main features of the SDK: | ||
|
||
1. [Create and run workflows using code](workflow_sdk.md) | ||
2. [Create and run strongly typed workers](worker_sdk.md) | ||
3. [Unit Testing framework for workflows and workers](testing_framework.md) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
apply plugin: 'groovy' | ||
|
||
dependencies { | ||
|
||
implementation project(':conductor-common') | ||
implementation project(':conductor-client') | ||
|
||
implementation "com.fasterxml.jackson.core:jackson-databind" | ||
implementation "com.google.guava:guava:${revGuava}" | ||
implementation "cglib:cglib:3.2.4" | ||
implementation "com.sun.jersey:jersey-client:${revJersey}" | ||
|
||
testImplementation "org.springframework:spring-web" | ||
testImplementation "org.spockframework:spock-core:${revSpock}" | ||
testImplementation "org.spockframework:spock-spring:${revSpock}" | ||
|
||
testImplementation "com.fasterxml.jackson.core:jackson-core" | ||
testImplementation "org.apache.commons:commons-lang3" | ||
|
||
testImplementation "org.codehaus.groovy:groovy-all:${revGroovy}" | ||
} | ||
|
||
test { | ||
testLogging { | ||
exceptionFormat = 'full' | ||
} | ||
} | ||
sourceSets.main.java.srcDirs += ['example/java', 'example/resources'] | ||
|
103 changes: 103 additions & 0 deletions
103
java-sdk/example/java/com/netflix/conductor/sdk/example/shipment/Order.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.sdk.example.shipment; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Order { | ||
|
||
public enum ShippingMethod { | ||
GROUND, | ||
NEXT_DAY_AIR, | ||
SAME_DAY | ||
} | ||
|
||
private String orderNumber; | ||
|
||
private String sku; | ||
|
||
private int quantity; | ||
|
||
private BigDecimal unitPrice; | ||
|
||
private String zipCode; | ||
|
||
private String countryCode; | ||
|
||
private ShippingMethod shippingMethod; | ||
|
||
public Order(String orderNumber, String sku, int quantity, BigDecimal unitPrice) { | ||
this.orderNumber = orderNumber; | ||
this.sku = sku; | ||
this.quantity = quantity; | ||
this.unitPrice = unitPrice; | ||
} | ||
|
||
public Order() {} | ||
|
||
public String getOrderNumber() { | ||
return orderNumber; | ||
} | ||
|
||
public void setOrderNumber(String orderNumber) { | ||
this.orderNumber = orderNumber; | ||
} | ||
|
||
public String getSku() { | ||
return sku; | ||
} | ||
|
||
public void setSku(String sku) { | ||
this.sku = sku; | ||
} | ||
|
||
public int getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public void setQuantity(int quantity) { | ||
this.quantity = quantity; | ||
} | ||
|
||
public BigDecimal getUnitPrice() { | ||
return unitPrice; | ||
} | ||
|
||
public void setUnitPrice(BigDecimal unitPrice) { | ||
this.unitPrice = unitPrice; | ||
} | ||
|
||
public String getZipCode() { | ||
return zipCode; | ||
} | ||
|
||
public void setZipCode(String zipCode) { | ||
this.zipCode = zipCode; | ||
} | ||
|
||
public String getCountryCode() { | ||
return countryCode; | ||
} | ||
|
||
public void setCountryCode(String countryCode) { | ||
this.countryCode = countryCode; | ||
} | ||
|
||
public ShippingMethod getShippingMethod() { | ||
return shippingMethod; | ||
} | ||
|
||
public void setShippingMethod(ShippingMethod shippingMethod) { | ||
this.shippingMethod = shippingMethod; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
java-sdk/example/java/com/netflix/conductor/sdk/example/shipment/Shipment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.sdk.example.shipment; | ||
|
||
public class Shipment { | ||
|
||
private String userId; | ||
|
||
private String orderNo; | ||
|
||
public Shipment(String userId, String orderNo) { | ||
this.userId = userId; | ||
this.orderNo = orderNo; | ||
} | ||
|
||
public Shipment() {} | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(String userId) { | ||
this.userId = userId; | ||
} | ||
|
||
public String getOrderNo() { | ||
return orderNo; | ||
} | ||
|
||
public void setOrderNo(String orderNo) { | ||
this.orderNo = orderNo; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
java-sdk/example/java/com/netflix/conductor/sdk/example/shipment/ShipmentState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.sdk.example.shipment; | ||
|
||
public class ShipmentState { | ||
|
||
private boolean paymentCompleted; | ||
|
||
private boolean emailSent; | ||
|
||
private boolean shipped; | ||
|
||
private String trackingNumber; | ||
|
||
public boolean isPaymentCompleted() { | ||
return paymentCompleted; | ||
} | ||
|
||
public void setPaymentCompleted(boolean paymentCompleted) { | ||
this.paymentCompleted = paymentCompleted; | ||
} | ||
|
||
public boolean isEmailSent() { | ||
return emailSent; | ||
} | ||
|
||
public void setEmailSent(boolean emailSent) { | ||
this.emailSent = emailSent; | ||
} | ||
|
||
public boolean isShipped() { | ||
return shipped; | ||
} | ||
|
||
public void setShipped(boolean shipped) { | ||
this.shipped = shipped; | ||
} | ||
|
||
public String getTrackingNumber() { | ||
return trackingNumber; | ||
} | ||
|
||
public void setTrackingNumber(String trackingNumber) { | ||
this.trackingNumber = trackingNumber; | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
java-sdk/example/java/com/netflix/conductor/sdk/example/shipment/ShipmentWorkers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.sdk.example.shipment; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.*; | ||
|
||
import com.netflix.conductor.sdk.workflow.def.tasks.DynamicForkInput; | ||
import com.netflix.conductor.sdk.workflow.def.tasks.SubWorkflow; | ||
import com.netflix.conductor.sdk.workflow.def.tasks.Task; | ||
import com.netflix.conductor.sdk.workflow.task.InputParam; | ||
import com.netflix.conductor.sdk.workflow.task.OutputParam; | ||
import com.netflix.conductor.sdk.workflow.task.WorkerTask; | ||
|
||
public class ShipmentWorkers { | ||
|
||
@WorkerTask("generateDynamicFork") | ||
public DynamicForkInput generateDynamicFork( | ||
@InputParam("orderDetails") List<Order> orderDetails, | ||
@InputParam("userDetails") User userDetails) { | ||
DynamicForkInput input = new DynamicForkInput(); | ||
List<Task<?>> tasks = new ArrayList<>(); | ||
Map<String, Object> inputs = new HashMap<>(); | ||
|
||
for (int i = 0; i < orderDetails.size(); i++) { | ||
Order detail = orderDetails.get(i); | ||
String referenceName = "order_flow_sub_" + i; | ||
tasks.add( | ||
new SubWorkflow(referenceName, "order_flow", null) | ||
.input("orderDetail", detail) | ||
.input("userDetails", userDetails)); | ||
inputs.put(referenceName, new HashMap<>()); | ||
} | ||
input.setInputs(inputs); | ||
input.setTasks(tasks); | ||
return input; | ||
} | ||
|
||
@WorkerTask("get_order_details") | ||
public List<Order> getOrderDetails(@InputParam("orderNo") String orderNo) { | ||
int lineItemCount = new Random().nextInt(10); | ||
List<Order> orderDetails = new ArrayList<>(); | ||
for (int i = 0; i < lineItemCount; i++) { | ||
Order orderDetail = new Order(orderNo, "sku_" + i, 2, BigDecimal.valueOf(20.5)); | ||
orderDetail.setOrderNumber(UUID.randomUUID().toString()); | ||
orderDetail.setCountryCode(i % 2 == 0 ? "US" : "CA"); | ||
if (i % 3 == 0) { | ||
orderDetail.setCountryCode("UK"); | ||
} | ||
|
||
if (orderDetail.getCountryCode().equals("US")) | ||
orderDetail.setShippingMethod(Order.ShippingMethod.SAME_DAY); | ||
else if (orderDetail.getCountryCode().equals("CA")) | ||
orderDetail.setShippingMethod(Order.ShippingMethod.NEXT_DAY_AIR); | ||
else orderDetail.setShippingMethod(Order.ShippingMethod.GROUND); | ||
|
||
orderDetails.add(orderDetail); | ||
} | ||
return orderDetails; | ||
} | ||
|
||
@WorkerTask("get_user_details") | ||
public User getUserDetails(@InputParam("userId") String userId) { | ||
User user = | ||
new User( | ||
"User Name", | ||
userId + "@example.com", | ||
"1234 forline street", | ||
"mountain view", | ||
"95030", | ||
"US", | ||
"Paypal", | ||
"biling_001"); | ||
|
||
return user; | ||
} | ||
|
||
@WorkerTask("calculate_tax_and_total") | ||
public @OutputParam("total_amount") BigDecimal calculateTax( | ||
@InputParam("orderDetail") Order orderDetails) { | ||
BigDecimal preTaxAmount = | ||
orderDetails.getUnitPrice().multiply(new BigDecimal(orderDetails.getQuantity())); | ||
BigDecimal tax = BigDecimal.valueOf(0.2).multiply(preTaxAmount); | ||
if (!"US".equals(orderDetails.getCountryCode())) { | ||
tax = BigDecimal.ZERO; | ||
} | ||
return preTaxAmount.add(tax); | ||
} | ||
|
||
@WorkerTask("ground_shipping_label") | ||
public @OutputParam("reference_number") String prepareGroundShipping( | ||
@InputParam("name") String name, | ||
@InputParam("address") String address, | ||
@InputParam("orderNo") String orderNo) { | ||
|
||
return "Ground_" + orderNo; | ||
} | ||
|
||
@WorkerTask("air_shipping_label") | ||
public @OutputParam("reference_number") String prepareAirShipping( | ||
@InputParam("name") String name, | ||
@InputParam("address") String address, | ||
@InputParam("orderNo") String orderNo) { | ||
|
||
return "Air_" + orderNo; | ||
} | ||
|
||
@WorkerTask("same_day_shipping_label") | ||
public @OutputParam("reference_number") String prepareSameDayShipping( | ||
@InputParam("name") String name, | ||
@InputParam("address") String address, | ||
@InputParam("orderNo") String orderNo) { | ||
|
||
return "SameDay_" + orderNo; | ||
} | ||
|
||
@WorkerTask("charge_payment") | ||
public @OutputParam("reference") String chargePayment( | ||
@InputParam("amount") BigDecimal amount, | ||
@InputParam("billingId") String billingId, | ||
@InputParam("billingType") String billingType) { | ||
|
||
return UUID.randomUUID().toString(); | ||
} | ||
|
||
@WorkerTask("send_email") | ||
public void sendEmail( | ||
@InputParam("name") String name, | ||
@InputParam("email") String email, | ||
@InputParam("orderNo") String orderNo) {} | ||
} |
Oops, something went wrong.