Skip to content

Commit e5eaab5

Browse files
committed
updates
Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent d9b43cb commit e5eaab5

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

core/src/main/java/io/temporal/samples/autoheartbeat/Starter.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ public static void main(String[] args) {
9191
} catch (Exception e) {
9292
System.out.println("Second run - Workflow exec exception: " + e.getClass().getName());
9393
}
94+
95+
System.out.println("\n\n**** Third Run: cause heartbeat timeout");
96+
// we disable autoheartbeat via env var
97+
System.setProperty("sample.disableAutoHeartbeat", "true");
98+
AutoWorkflow thirdRun =
99+
client.newWorkflowStub(
100+
AutoWorkflow.class,
101+
WorkflowOptions.newBuilder()
102+
.setWorkflowId(WORKFLOW_ID)
103+
.setTaskQueue(TASK_QUEUE)
104+
.build());
105+
106+
try {
107+
String thirdRunResult = thirdRun.exec("Auto heartbeating is cool");
108+
System.out.println("Third run result: " + thirdRunResult);
109+
} catch (Exception e) {
110+
System.out.println("Third run - Workflow exec exception: " + e.getClass().getName());
111+
}
94112
}
95113

96114
private static void doSleeps(int seconds) {

core/src/main/java/io/temporal/samples/autoheartbeat/interceptor/AutoHeartbeatActivityInboundCallsInterceptor.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ public class AutoHeartbeatActivityInboundCallsInterceptor
3636
private ActivityExecutionContext activityExecutionContext;
3737
private Duration activityHeartbeatTimeout;
3838
private AutoHeartbeatUtil autoHeartbeater;
39-
// private CompletableFuture autoHeartbeatFuture;
4039
private ScheduledFuture scheduledFuture;
4140

42-
// private ScheduledFuture scheduledFuture;
43-
4441
public AutoHeartbeatActivityInboundCallsInterceptor(ActivityInboundCallsInterceptor next) {
4542
super(next);
4643
}
@@ -56,7 +53,10 @@ public void init(ActivityExecutionContext context) {
5653
@SuppressWarnings({"FutureReturnValueIgnored", "CatchAndPrintStackTrace"})
5754
public ActivityOutput execute(ActivityInput input) {
5855
// If activity has heartbeat timeout defined we want to apply auto-heartbeter
59-
if (activityHeartbeatTimeout != null && activityHeartbeatTimeout.getSeconds() > 0) {
56+
// Unless we explicitly disabled autoheartbeating via system property
57+
if (activityHeartbeatTimeout != null
58+
&& activityHeartbeatTimeout.getSeconds() > 0
59+
&& !Boolean.parseBoolean(System.getProperty("sample.disableAutoHeartbeat"))) {
6060
System.out.println(
6161
"Auto heartbeating applied for activity: "
6262
+ activityExecutionContext.getInfo().getActivityType());
@@ -82,7 +82,8 @@ public ActivityOutput execute(ActivityInput input) {
8282
}
8383
});
8484
try {
85-
CompletableFuture.anyOf(autoHeartbeatFuture, activityExecFuture).get();
85+
return (ActivityOutput)
86+
CompletableFuture.anyOf(autoHeartbeatFuture, activityExecFuture).get();
8687
} catch (Exception e) {
8788
if (e instanceof ExecutionException) {
8889
ExecutionException ee = (ExecutionException) e;
@@ -96,8 +97,9 @@ public ActivityOutput execute(ActivityInput input) {
9697
autoHeartbeater.stop();
9798
}
9899
}
100+
} else {
101+
return super.execute(input);
99102
}
100-
return super.execute(input);
101103
}
102104

103105
public interface AutoHeartbeaterCancellationCallback {

core/src/main/java/io/temporal/samples/autoheartbeat/workflows/AutoWorkflowImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
import io.temporal.activity.ActivityCancellationType;
2323
import io.temporal.activity.ActivityOptions;
24+
import io.temporal.common.RetryOptions;
2425
import io.temporal.failure.ActivityFailure;
2526
import io.temporal.failure.CanceledFailure;
27+
import io.temporal.failure.TimeoutFailure;
2628
import io.temporal.samples.autoheartbeat.activities.AutoActivities;
2729
import io.temporal.workflow.CancellationScope;
2830
import io.temporal.workflow.Workflow;
@@ -40,6 +42,8 @@ public String exec(String input) {
4042
.setStartToCloseTimeout(Duration.ofSeconds(22))
4143
.setHeartbeatTimeout(Duration.ofSeconds(8))
4244
.setCancellationType(ActivityCancellationType.WAIT_CANCELLATION_COMPLETED)
45+
// for sample purposes
46+
.setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(3).build())
4347
.build());
4448

4549
AutoActivities activitiesTwo =
@@ -49,6 +53,8 @@ public String exec(String input) {
4953
.setStartToCloseTimeout(Duration.ofSeconds(20))
5054
.setHeartbeatTimeout(Duration.ofSeconds(7))
5155
.setCancellationType(ActivityCancellationType.WAIT_CANCELLATION_COMPLETED)
56+
// for sample purposes
57+
.setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(3).build())
5258
.build());
5359

5460
// Start our activity in CancellationScope so we can cancel it if needed
@@ -65,6 +71,9 @@ public String exec(String input) {
6571
if (e.getCause() instanceof CanceledFailure) {
6672
// We dont want workflow to fail in we canceled our scope, just log and return
6773
return "Workflow result after activity cancellation";
74+
} else if (e.getCause() instanceof TimeoutFailure) {
75+
return "Workflow result after activity timeout of type: "
76+
+ ((TimeoutFailure) e.getCause()).getTimeoutType().name();
6877
} else {
6978
throw e;
7079
}

0 commit comments

Comments
 (0)