chore: Wait for dapr script

When running mechanical markdown examples wait for dapr to be running

Signed-off-by: Javier Aliaga <javier@diagrid.io>
This commit is contained in:
Javier Aliaga 2025-08-25 17:44:57 +02:00
parent 1358cd809f
commit ddbecba409
8 changed files with 100 additions and 39 deletions

25
.github/scripts/health_check_script.sh vendored Executable file
View File

@ -0,0 +1,25 @@
#!/bin/sh
wait_for_dapr() {
local port="${1:-3500}"
local max_tries="${2:-60}"
local url="http://localhost:${port}/v1.0/healthz"
local code=""
for i in $(seq 1 "$max_tries"); do
code=$(curl -s -o /dev/null -w "%{http_code}" "$url" || true)
if [ "$code" = "204" ]; then
echo "Ready (204) on port ${port}"
return 0 # do not exit; just return
fi
sleep 1
done
echo "Timeout after ${max_tries}s waiting for 204 on port ${port} (last code: ${code})"
return 0 # keep returning success to avoid exiting callers using set -e
}
# Example usage:
# wait_for_dapr # uses defaults: port 3500, tries 60
# wait_for_dapr 3501 # custom port, default tries
# wait_for_dapr 3501 30 # custom port and tries

View File

@ -147,7 +147,7 @@ background: true
-->
Execute the following script in order to run DemoChainWorker:
```sh
dapr run --app-id demoworkflowworker --resources-path ./components/workflows --dapr-grpc-port 50001 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.chain.DemoChainWorker 50001
dapr run --app-id chainingworker --resources-path ./components/workflows --dapr-grpc-port 50001 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.chain.DemoChainWorker 50001
```
Once running, the logs will start displaying the different steps: First, you can see workflow is starting:
@ -169,7 +169,8 @@ timeout_seconds: 20
-->
Then, execute the following script in order to run DemoChainClient:
```sh
sleep 10 && java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.chain.DemoChainClient 50001
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.chain.DemoChainClient 50001
dapr stop --app-id chainingworker
```
<!-- END_STEP -->
@ -266,7 +267,7 @@ background: true
Execute the following script in order to run DemoFanInOutWorker:
```sh
dapr run --app-id demoworkflowworker --resources-path ./components/workflows --dapr-grpc-port 50002 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.faninout.DemoFanInOutWorker 50002
dapr run --app-id faninoutworker --resources-path ./components/workflows --dapr-grpc-port 50002 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.faninout.DemoFanInOutWorker 50002
```
<!-- END_STEP -->
@ -282,7 +283,8 @@ timeout_seconds: 20
Execute the following script in order to run DemoFanInOutClient:
```sh
sleep 10 && java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.faninout.DemoFanInOutClient 50002
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.faninout.DemoFanInOutClient 50002
dapr stop --app-id faninoutworker
```
<!-- END_STEP -->
@ -660,7 +662,8 @@ timeout_seconds: 30
-->
Once running, execute the following script to run the BookTripClient:
```sh
sleep 15 && java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.compensation.BookTripClient 50003
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.compensation.BookTripClient 50003
dapr stop --app-id book-trip-worker
```
<!-- END_STEP -->
@ -702,7 +705,7 @@ timeout_seconds: 30
-->
```sh
dapr run --app-id demoworkflowworker --resources-path ./components/workflows --dapr-grpc-port 50004 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.suspendresume.DemoSuspendResumeWorker 50004
dapr run --app-id suspendresumeworker --resources-path ./components/workflows --dapr-grpc-port 50004 -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.suspendresume.DemoSuspendResumeWorker 50004
```
<!-- END_STEP -->
@ -720,7 +723,8 @@ expected_stdout_lines:
timeout_seconds: 30
-->
```sh
sleep 15 && java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.suspendresume.DemoSuspendResumeClient 50004
java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.workflows.suspendresume.DemoSuspendResumeClient 50004
dapr stop --app-id suspendresumeworker
```
<!-- END_STEP -->

View File

@ -14,9 +14,11 @@ limitations under the License.
package io.dapr.examples.workflows.chain;
import io.dapr.examples.workflows.utils.PropertyUtils;
import io.dapr.examples.workflows.utils.RetryUtils;
import io.dapr.workflows.client.DaprWorkflowClient;
import io.dapr.workflows.client.WorkflowInstanceStatus;
import java.time.Duration;
import java.util.concurrent.TimeoutException;
public class DemoChainClient {
@ -28,15 +30,15 @@ public class DemoChainClient {
*/
public static void main(String[] args) {
try (DaprWorkflowClient client = new DaprWorkflowClient(PropertyUtils.getProperties(args))) {
String instanceId = client.scheduleNewWorkflow(DemoChainWorkflow.class);
System.out.printf("Started a new chaining model workflow with instance ID: %s%n", instanceId);
WorkflowInstanceStatus workflowInstanceStatus =
client.waitForInstanceCompletion(instanceId, null, true);
String result = workflowInstanceStatus.readOutputAs(String.class);
System.out.printf("workflow instance with ID: %s completed with result: %s%n", instanceId, result);
String instanceId = RetryUtils.callWithRetry(() -> client.scheduleNewWorkflow(DemoChainWorkflow.class),
Duration.ofSeconds(60));
System.out.printf("Started a new chaining model workflow with instance ID: %s%n", instanceId);
WorkflowInstanceStatus workflowInstanceStatus =
client.waitForInstanceCompletion(instanceId, null, true);
String result = workflowInstanceStatus.readOutputAs(String.class);
System.out.printf("workflow instance with ID: %s completed with result: %s%n", instanceId, result);
} catch (TimeoutException | InterruptedException e) {
throw new RuntimeException(e);
}

View File

@ -14,6 +14,7 @@ limitations under the License.
package io.dapr.examples.workflows.compensation;
import io.dapr.examples.workflows.utils.PropertyUtils;
import io.dapr.examples.workflows.utils.RetryUtils;
import io.dapr.workflows.client.DaprWorkflowClient;
import io.dapr.workflows.client.WorkflowInstanceStatus;
@ -23,7 +24,7 @@ import java.util.concurrent.TimeoutException;
public class BookTripClient {
public static void main(String[] args) {
try (DaprWorkflowClient client = new DaprWorkflowClient(PropertyUtils.getProperties(args))) {
String instanceId = client.scheduleNewWorkflow(BookTripWorkflow.class);
String instanceId = RetryUtils.callWithRetry(() -> client.scheduleNewWorkflow(BookTripWorkflow.class), Duration.ofSeconds(60));
System.out.printf("Started a new trip booking workflow with instance ID: %s%n", instanceId);
WorkflowInstanceStatus status = client.waitForInstanceCompletion(instanceId, Duration.ofMinutes(30), true);

View File

@ -14,6 +14,7 @@ limitations under the License.
package io.dapr.examples.workflows.faninout;
import io.dapr.examples.workflows.utils.PropertyUtils;
import io.dapr.examples.workflows.utils.RetryUtils;
import io.dapr.workflows.client.DaprWorkflowClient;
import io.dapr.workflows.client.WorkflowInstanceStatus;
@ -40,10 +41,12 @@ public class DemoFanInOutClient {
"Always remember that you are absolutely unique. Just like everyone else.");
// Schedule an orchestration which will reliably count the number of words in all the given sentences.
String instanceId = client.scheduleNewWorkflow(
String instanceId = RetryUtils.callWithRetry(() -> client.scheduleNewWorkflow(
DemoFanInOutWorkflow.class,
listOfStrings);
System.out.printf("Started a new fan out/fan in model workflow with instance ID: %s%n", instanceId);
listOfStrings), Duration.ofSeconds(60));
System.out.printf("Started a new fan out/fan in model workflow with instance ID: %s%n", instanceId);
// Block until the orchestration completes. Then print the final status, which includes the output.
WorkflowInstanceStatus workflowInstanceStatus = client.waitForInstanceCompletion(

View File

@ -15,9 +15,11 @@ package io.dapr.examples.workflows.suspendresume;
import io.dapr.examples.workflows.externalevent.DemoExternalEventWorkflow;
import io.dapr.examples.workflows.utils.PropertyUtils;
import io.dapr.examples.workflows.utils.RetryUtils;
import io.dapr.workflows.client.DaprWorkflowClient;
import io.dapr.workflows.client.WorkflowInstanceStatus;
import java.time.Duration;
import java.util.concurrent.TimeoutException;
public class DemoSuspendResumeClient {
@ -29,7 +31,7 @@ public class DemoSuspendResumeClient {
*/
public static void main(String[] args) {
try (DaprWorkflowClient client = new DaprWorkflowClient(PropertyUtils.getProperties(args))) {
String instanceId = client.scheduleNewWorkflow(DemoExternalEventWorkflow.class);
String instanceId = RetryUtils.callWithRetry(() -> client.scheduleNewWorkflow(DemoExternalEventWorkflow.class), Duration.ofSeconds(60));
System.out.printf("Started a new external-event workflow with instance ID: %s%n", instanceId);

View File

@ -0,0 +1,34 @@
package io.dapr.examples.workflows.utils;
import java.time.Duration;
import java.util.concurrent.Callable;
public class RetryUtils {
private static final long RETRY_WAIT_MILLISECONDS = 1000;
public static String callWithRetry(Callable<String> function, Duration retryTimeout) throws InterruptedException {
var retryTimeoutMilliseconds = retryTimeout.toMillis();
long started = System.currentTimeMillis();
while (true) {
Throwable exception;
try {
return function.call();
} catch (Exception | AssertionError e) {
exception = e;
}
long elapsed = System.currentTimeMillis() - started;
if (elapsed >= retryTimeoutMilliseconds) {
if (exception instanceof RuntimeException) {
throw (RuntimeException) exception;
}
throw new RuntimeException(exception);
}
long remaining = retryTimeoutMilliseconds - elapsed;
Thread.sleep(Math.min(remaining, RETRY_WAIT_MILLISECONDS));
}
}
}

View File

@ -65,7 +65,6 @@ match_order: none
output_match_mode: substring
expected_stdout_lines:
- 'TOKYO, LONDON, SEATTLE'
background: true
timeout_seconds: 90
-->
<!-- Timeout for above service must be more than sleep + timeout for the client-->
@ -73,7 +72,7 @@ timeout_seconds: 90
To start the workflow with the three chained activities you can run:
```sh
sleep 35 && curl -X POST localhost:8080/wfp/chain -H 'Content-Type: application/json'
curl -X POST localhost:8080/wfp/chain -H 'Content-Type: application/json' --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -142,7 +141,6 @@ match_order: none
output_match_mode: substring
expected_stdout_lines:
- '!wolfkroW rpaD olleH'
background: true
timeout_seconds: 90
-->
<!-- Timeout for above service must be more than sleep + timeout for the client-->
@ -150,7 +148,7 @@ timeout_seconds: 90
To start the workflow with the three chained activities you can run:
```sh
sleep 35 && curl -X POST localhost:8080/wfp/child -H 'Content-Type: application/json'
curl -X POST localhost:8080/wfp/child -H 'Content-Type: application/json' --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -191,13 +189,12 @@ match_order: none
output_match_mode: substring
expected_stdout_lines:
- '{"cleanUpTimes":5}'
background: true
timeout_seconds: 90
-->
<!-- Timeout for above service must be more than sleep + timeout for the client-->
```sh
sleep 30 && curl -X POST localhost:8080/wfp/continueasnew -H 'Content-Type: application/json'
curl -X POST localhost:8080/wfp/continueasnew -H 'Content-Type: application/json' --retry 10 --max-time 60 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -260,13 +257,12 @@ To start the workflow you can run:
name: Start External Event Workflow
match_order: none
output_match_mode: substring
background: true
timeout_seconds: 90
-->
<!-- Timeout for above service must be more than sleep + timeout for the client-->
```sh
sleep 30 && curl -X POST "localhost:8080/wfp/externalevent?orderId=123" -H 'Content-Type: application/json'
curl -X POST "localhost:8080/wfp/externalevent?orderId=123" -H 'Content-Type: application/json' --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -290,7 +286,6 @@ match_order: none
output_match_mode: substring
expected_stdout_lines:
- '{"approved":true}'
background: true
timeout_seconds: 90
-->
<!-- Timeout for above service must be more than sleep + timeout for the client-->
@ -298,7 +293,7 @@ timeout_seconds: 90
To send the event you can run:
```sh
sleep 42 && curl -X POST "localhost:8080/wfp/externalevent-continue?orderId=123&decision=true" -H 'Content-Type: application/json'
curl -X POST "localhost:8080/wfp/externalevent-continue?orderId=123&decision=true" -H 'Content-Type: application/json' --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -346,13 +341,12 @@ match_order: none
output_match_mode: substring
expected_stdout_lines:
- '{"wordCount":60}'
background: true
timeout_seconds: 90
-->
<!-- Timeout for above service must be more than sleep + timeout for the client-->
```sh
sleep 45 && curl -X POST localhost:8080/wfp/fanoutin -H 'Content-Type: application/json' -d @body.json
curl -X POST localhost:8080/wfp/fanoutin -H 'Content-Type: application/json' -d @body.json --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -398,13 +392,12 @@ To start the workflow, you can run:
name: Start Suspend/Resume Workflow
match_order: none
output_match_mode: substring
background: true
timeout_seconds: 90
-->
<!-- Timeout for above service must be more than sleep + timeout for the client-->
```sh
sleep 50 && curl -X POST "localhost:8080/wfp/suspendresume?orderId=456" -H 'Content-Type: application/json'
curl -X POST "localhost:8080/wfp/suspendresume?orderId=456" -H 'Content-Type: application/json' --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -431,7 +424,6 @@ match_order: none
output_match_mode: substring
expected_stdout_lines:
- 'SUSPENDED'
background: true
timeout_seconds: 90
-->
@ -439,7 +431,7 @@ timeout_seconds: 90
Let's suspend the workflow instance by sending the following request:
```sh
sleep 55 && curl -X POST "localhost:8080/wfp/suspendresume/suspend?orderId=456" -H 'Content-Type: application/json'
curl -X POST "localhost:8080/wfp/suspendresume/suspend?orderId=456" -H 'Content-Type: application/json' --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -459,7 +451,6 @@ match_order: none
output_match_mode: substring
expected_stdout_lines:
- 'RUNNING'
background: true
timeout_seconds: 90
-->
@ -467,7 +458,7 @@ timeout_seconds: 90
To send the event you can run:
```sh
sleep 60 && curl -X POST "localhost:8080/wfp/suspendresume/resume?orderId=456" -H 'Content-Type: application/json'
curl -X POST "localhost:8080/wfp/suspendresume/resume?orderId=456" -H 'Content-Type: application/json' --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->
@ -487,7 +478,6 @@ match_order: none
output_match_mode: substring
expected_stdout_lines:
- '{"approved":true}'
background: true
timeout_seconds: 90
-->
<!-- Timeout for above service must be more than sleep + timeout for the client-->
@ -495,7 +485,7 @@ timeout_seconds: 90
To send the event you can run:
```sh
sleep 65 && curl -X POST "localhost:8080/wfp/suspendresume/continue?orderId=456&decision=true" -H 'Content-Type: application/json'
curl -X POST "localhost:8080/wfp/suspendresume/continue?orderId=456&decision=true" -H 'Content-Type: application/json' --retry 10 --max-time 20 --retry-all-errors --retry-max-time 90
```
<!-- END_STEP -->