From dd8138006a019d2a5908efa5dacf88f406ca5df1 Mon Sep 17 00:00:00 2001 From: julio <107879411+julioalex-rezende@users.noreply.github.com> Date: Tue, 13 Jun 2023 14:27:15 +0100 Subject: [PATCH] implementing getIsReplaying() method for Authoring API (#7) --- .../runtime/DaprWorkflowContextImpl.java | 5 +++++ .../dapr/workflows/runtime/WorkflowContext.java | 17 +++++++++++++++++ .../runtime/DaprWorkflowContextImplTest.java | 10 ++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/sdk-workflows/src/main/java/io/dapr/workflows/runtime/DaprWorkflowContextImpl.java b/sdk-workflows/src/main/java/io/dapr/workflows/runtime/DaprWorkflowContextImpl.java index 39e715c33..e81359c56 100644 --- a/sdk-workflows/src/main/java/io/dapr/workflows/runtime/DaprWorkflowContextImpl.java +++ b/sdk-workflows/src/main/java/io/dapr/workflows/runtime/DaprWorkflowContextImpl.java @@ -95,4 +95,9 @@ public void complete(Object output) { public Task waitForExternalEvent(String eventName, Duration timeout) { return this.innerContext.waitForExternalEvent(eventName, timeout); } + + @Override + public boolean getIsReplaying() { + return this.innerContext.getIsReplaying(); + } } diff --git a/sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowContext.java b/sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowContext.java index 690a93c20..b13a2660b 100644 --- a/sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowContext.java +++ b/sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowContext.java @@ -66,4 +66,21 @@ public interface WorkflowContext { * @return Asynchronous task to {@code await()}. */ Task waitForExternalEvent(String eventName, Duration timeout); + + /** + * Gets a value indicating whether the workflow is currently replaying a previous execution. + * + *

Workflow functions are "replayed" after being unloaded from memory to reconstruct local variable state. + * During a replay, previously executed tasks will be completed automatically with previously seen values + * that are stored in the workflow history. Once the workflow reaches the point where it's no longer + * replaying existing history, this method will return {@code false}. + * + *

You can use this method if you have logic that needs to run only when not replaying. For example, + * certain types of application logging may become too noisy when duplicated as part of replay. The + * application code could check to see whether the function is being replayed and then issue the log statements + * when this value is {@code false}. + * + * @return {@code true} if the workflow is replaying, otherwise {@code false} + */ + boolean getIsReplaying(); } diff --git a/sdk-workflows/src/test/java/io/dapr/workflows/runtime/DaprWorkflowContextImplTest.java b/sdk-workflows/src/test/java/io/dapr/workflows/runtime/DaprWorkflowContextImplTest.java index 4cd628b5d..b5278599d 100644 --- a/sdk-workflows/src/test/java/io/dapr/workflows/runtime/DaprWorkflowContextImplTest.java +++ b/sdk-workflows/src/test/java/io/dapr/workflows/runtime/DaprWorkflowContextImplTest.java @@ -65,10 +65,16 @@ public void completeTest() { verify(mockInnerContext, times(1)).complete(null); } + @Test + public void getIsReplaying() { + context.getIsReplaying(); + verify(mockInnerContext, times(1)).getIsReplaying(); + } + @Test public void getLoggerReplayingTest() { Logger mockLogger = mock(Logger.class); - when(mockInnerContext.getIsReplaying()).thenReturn(true); + when(context.getIsReplaying()).thenReturn(true); DaprWorkflowContextImpl testContext = new DaprWorkflowContextImpl(mockInnerContext, mockLogger); String expectedArg = "test print"; @@ -80,7 +86,7 @@ public void getLoggerReplayingTest() { @Test public void getLoggerFirstTimeTest() { Logger mockLogger = mock(Logger.class); - when(mockInnerContext.getIsReplaying()).thenReturn(false); + when(context.getIsReplaying()).thenReturn(false); DaprWorkflowContextImpl testContext = new DaprWorkflowContextImpl(mockInnerContext, mockLogger); String expectedArg = "test print";