From 4fafe258f6ddf589912c7d0c8d868aed2966c9b1 Mon Sep 17 00:00:00 2001 From: Kevin Robayna Date: Wed, 21 Jan 2026 17:47:59 +0000 Subject: [PATCH] Add sleep method with options for timer The sleep function in a workflow is syntactic sugar over the timer method. Today I was going to do a change in one of my services to sleep for a few mins but wanted to add a summary and found out this is only available on the timer api. Let me know what you think of this change! --- .../interceptors/WorkflowOutboundCallsInterceptor.java | 2 ++ .../WorkflowOutboundCallsInterceptorBase.java | 5 +++++ .../io/temporal/internal/sync/SyncWorkflowContext.java | 5 +++++ .../temporal/testing/TestActivityEnvironmentInternal.java | 5 +++++ .../testing/internal/TracingWorkerInterceptor.java | 8 ++++++++ 5 files changed, 25 insertions(+) diff --git a/temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptor.java b/temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptor.java index d515817b2..6b6ebef69 100644 --- a/temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptor.java +++ b/temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptor.java @@ -738,6 +738,8 @@ public DynamicUpdateHandler getHandler() { void sleep(Duration duration); + void sleep(Duration duration, TimerOptions options); + boolean await(Duration timeout, String reason, Supplier unblockCondition); void await(String reason, Supplier unblockCondition); diff --git a/temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptorBase.java b/temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptorBase.java index 9d99d4c78..7186b9543 100644 --- a/temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptorBase.java +++ b/temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkflowOutboundCallsInterceptorBase.java @@ -65,6 +65,11 @@ public void sleep(Duration duration) { next.sleep(duration); } + @Override + public void sleep(Duration duration, TimerOptions options) { + next.sleep(duration, options); + } + @Override public boolean await(Duration timeout, String reason, Supplier unblockCondition) { return next.await(timeout, reason, unblockCondition); diff --git a/temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java b/temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java index 977d9754e..e67f798b3 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java @@ -1315,6 +1315,11 @@ public void sleep(Duration duration) { newTimer(duration).get(); } + @Override + public void sleep(Duration duration, TimerOptions options) { + newTimer(duration, options).get(); + } + @Override public boolean await(Duration timeout, String reason, Supplier unblockCondition) { Promise timer = newTimer(timeout); diff --git a/temporal-testing/src/main/java/io/temporal/testing/TestActivityEnvironmentInternal.java b/temporal-testing/src/main/java/io/temporal/testing/TestActivityEnvironmentInternal.java index 1221dec55..80d68cb20 100644 --- a/temporal-testing/src/main/java/io/temporal/testing/TestActivityEnvironmentInternal.java +++ b/temporal-testing/src/main/java/io/temporal/testing/TestActivityEnvironmentInternal.java @@ -384,6 +384,11 @@ public void sleep(Duration duration) { throw new UnsupportedOperationException("not implemented"); } + @Override + public void sleep(Duration duration, TimerOptions options) { + throw new UnsupportedOperationException("not implemented"); + } + @Override public boolean await(Duration timeout, String reason, Supplier unblockCondition) { throw new UnsupportedOperationException("not implemented"); diff --git a/temporal-testing/src/main/java/io/temporal/testing/internal/TracingWorkerInterceptor.java b/temporal-testing/src/main/java/io/temporal/testing/internal/TracingWorkerInterceptor.java index 77d82bdae..cf10914a8 100644 --- a/temporal-testing/src/main/java/io/temporal/testing/internal/TracingWorkerInterceptor.java +++ b/temporal-testing/src/main/java/io/temporal/testing/internal/TracingWorkerInterceptor.java @@ -217,6 +217,14 @@ public void sleep(Duration duration) { next.sleep(duration); } + @Override + public void sleep(Duration duration, TimerOptions options) { + if (!WorkflowUnsafe.isReplaying()) { + trace.add("sleep " + duration); + } + next.sleep(duration, options); + } + @Override public boolean await(Duration timeout, String reason, Supplier unblockCondition) { if (!WorkflowUnsafe.isReplaying()) {