Skip to content

Commit

Permalink
Merge pull request #3 from johndelcastillo/versioning-cookbook
Browse files Browse the repository at this point in the history
Add Cadence versioning cookbook
  • Loading branch information
johndelcastillo authored Apr 5, 2023
2 parents c4a11bd + 726861b commit e63f55d
Show file tree
Hide file tree
Showing 16 changed files with 1,047 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.compile.nullAnalysis.mode": "automatic"
}
23 changes: 15 additions & 8 deletions cookbooks/child-workflows/child-workflows-megafood.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ to get our Instafood application running we first need to register a domain for
*Connection Info* tab, and will look like this: "ab-cd12ef23-45gh-4baf-ad99-df4xy-azba45bc0c8da111.elb.us-east-1.amazonaws.com". We'll call this the <cadence_host>.



3. We can now test our connection by listing current domains:

```bash
Expand Down Expand Up @@ -193,15 +192,23 @@ to get our Instafood application running we first need to register a domain for
cadenceHost=<cadence_host>
```

3. You can now run the app by
```bash
cadence-cookbooks-instafood/instafood$ ./gradlew run
```
or executing *InstafoodApplication* main class from your IDE:
3. Run the megaburgers API:

```bash
cadence-cookbooks-instafood/megaburgers$ ./gradlew run
```

4. You can now run the app by

```bash
cadence-cookbooks-instafood/instafood$ ./gradlew run
```

or executing *InstafoodApplication* main class from your IDE:

![Running Instafood app](images/run_instafood.png)
![Running Instafood app](images/run_instafood.png)

4. Check it is running by looking into its terminal output:
5. Check it is running by looking into its terminal output:

![Instafood running terminal output](images/instafood_app_running.png)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
484 changes: 484 additions & 0 deletions cookbooks/workflow versioning/workflow-versioning-megafood.md

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion instafood/src/main/java/InstafoodApplication.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.google.common.base.Strings;
import com.instafood.orders.delivery.CourierDeliveryWorkflowImpl;
import com.instafood.orders.delivery.activities.CourierGPSActivitiesImpl;
import com.instafood.orders.dispatcher.OrderWorkflowImpl;
import com.instafood.orders.megaburger.MegaBurgerOrderWorkflowImpl;
import com.instafood.orders.megaburger.activities.MegaBurgerRestApiOrderActivities;
Expand Down Expand Up @@ -30,7 +31,8 @@ public static void main(String[] args) {
Worker worker = factory.newWorker(TASK_LIST);
worker.registerWorkflowImplementationTypes(OrderWorkflowImpl.class, MegaBurgerOrderWorkflowImpl.class,
CourierDeliveryWorkflowImpl.class);
worker.registerActivitiesImplementations(new MegaBurgerRestApiOrderActivities());
worker.registerActivitiesImplementations(new MegaBurgerRestApiOrderActivities(),
new CourierGPSActivitiesImpl());
factory.start();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.instafood.orders.delivery;

import com.uber.cadence.workflow.QueryMethod;
import com.uber.cadence.workflow.SignalMethod;
import com.uber.cadence.workflow.WorkflowMethod;

Expand All @@ -9,4 +10,7 @@ public interface CourierDeliveryWorkflow {

@SignalMethod
void updateStatus(CourierDeliveryStatus status);

@QueryMethod
boolean courierSupportsGPSTracking();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package com.instafood.orders.delivery;

import java.time.Duration;

import com.instafood.orders.delivery.activities.CourierGPSActivities;
import com.instafood.orders.dispatcher.OrderWorkflow;
import com.instafood.orders.dispatcher.domain.OrderStatus;
import com.uber.cadence.activity.ActivityOptions;
import com.uber.cadence.common.RetryOptions;
import com.uber.cadence.workflow.Workflow;

public class CourierDeliveryWorkflowImpl implements CourierDeliveryWorkflow {

private CourierDeliveryStatus currentStatus = CourierDeliveryStatus.CREATED;
private boolean supportsGpsTracking = false;

private final CourierGPSActivities courierGPSActivities = Workflow.newActivityStub(CourierGPSActivities.class,
new ActivityOptions.Builder()
.setRetryOptions(new RetryOptions.Builder()
.setInitialInterval(Duration.ofSeconds(10))
.setMaximumAttempts(3)
.build())
.setScheduleToCloseTimeout(Duration.ofMinutes(5)).build());

@Override
public void deliverOrder(CourierDeliveryJob courierDeliveryJob) {
Expand All @@ -20,6 +34,14 @@ public void deliverOrder(CourierDeliveryJob courierDeliveryJob) {
}
parentOrderWorkflow.updateStatus(OrderStatus.COURIER_ACCEPTED);

// Added new GPS tracking functionality
int workflowVersion = Workflow.getVersion("GPSTrackingSupported", Workflow.DEFAULT_VERSION, 1);
if (workflowVersion >= 1) {
supportsGpsTracking = courierGPSActivities.registerDeliveryGPSTracking(
courierDeliveryJob.getRestaurant().toString(),
courierDeliveryJob.getAddress());
}

Workflow.await(() -> CourierDeliveryStatus.PICKED_UP.equals(currentStatus));
parentOrderWorkflow.updateStatus(OrderStatus.PICKED_UP);

Expand All @@ -36,4 +58,10 @@ private OrderWorkflow getParentOrderWorkflow() {
public void updateStatus(CourierDeliveryStatus status) {
this.currentStatus = status;
}

@Override
public boolean courierSupportsGPSTracking() {
// TODO Auto-generated method stub
return supportsGpsTracking;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.instafood.orders.delivery.activities;

import com.uber.cadence.activity.ActivityMethod;

public interface CourierGPSActivities {
@ActivityMethod
boolean registerDeliveryGPSTracking(String pickupLocation, String deliveryLocation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.instafood.orders.delivery.activities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CourierGPSActivitiesImpl implements CourierGPSActivities{

private static Logger logger = LoggerFactory.getLogger(CourierGPSActivitiesImpl.class);

public CourierGPSActivitiesImpl() {

}

@Override
public boolean registerDeliveryGPSTracking(String pickupLocation, String deliveryLocation) {
// register a delivery trip
logger.info("GPS tracking enabled. Pickup: {}, delivery to {}", pickupLocation, deliveryLocation);
return true;
}
}
23 changes: 23 additions & 0 deletions instafood/src/test/java/InstafoodApplicationTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.instafood.orders.delivery.CourierDeliveryStatus;
import com.instafood.orders.delivery.CourierDeliveryWorkflow;
import com.instafood.orders.delivery.CourierDeliveryWorkflowImpl;
import com.instafood.orders.dispatcher.OrderWorkflow;
import com.instafood.orders.dispatcher.domain.FoodOrder;
import com.instafood.orders.dispatcher.domain.OrderStatus;
Expand All @@ -20,6 +21,7 @@
import com.uber.cadence.client.WorkflowOptions;
import com.uber.cadence.serviceclient.ClientOptions;
import com.uber.cadence.serviceclient.WorkflowServiceTChannel;
import com.uber.cadence.testing.WorkflowReplayer;
import org.apache.thrift.TException;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -31,6 +33,7 @@
import java.util.stream.Collectors;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertTrue;

class InstafoodApplicationTest {

Expand Down Expand Up @@ -174,6 +177,26 @@ public void givenAnOrderWithDeliveryItShoulBeSentToMegaBurgerAndDeliveredByACour
await().until(
() -> workflowHistoryHasEvent(workflowClient, workflowExecution,
EventType.WorkflowExecutionCompleted));

// All new courier workflows should support GPS tracking, since this is a new
// job it will return true
assertTrue(courierDeliveryWorkflow.courierSupportsGPSTracking());
}

@Test
public void givenCourierWorkflowWhenGpsNotSupportedThenHistoryReplaysCorrectly() throws Exception {
// We have stored the history for a workflow that was executed before GPS
// support was added into a file - "resources/history-gps-not-supported.json"

// We use the workflow replayer to ensure that our legacy workflow can still
// execute correctly.

WorkflowReplayer.replayWorkflowExecutionFromResource("history-gps-not-supported.json",
CourierDeliveryWorkflowImpl.class);

// If we did not implement our version check, this method would throw an
// exception -- try it yourself by editing the CourierDeliveryWorkflowImpl
// class!
}

private List<WorkflowExecutionInfo> getOpenCourierDeliveryWorkflowsWithParentId(String parentWorkflowId) {
Expand Down
Loading

0 comments on commit e63f55d

Please sign in to comment.