-
Notifications
You must be signed in to change notification settings - Fork 192
Add support for activity pause #2476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Quinn-With-Two-Ns
merged 4 commits into
temporalio:master
from
Quinn-With-Two-Ns:issue-2465
Apr 15, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
temporal-sdk/src/main/java/io/temporal/client/ActivityPausedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. | ||
| * | ||
| * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this material except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.client; | ||
|
|
||
| import io.temporal.activity.ActivityInfo; | ||
|
|
||
| /*** | ||
| * Indicates that the activity was paused by the user. | ||
| * | ||
| * <p>Catching this exception directly is discouraged and catching the parent class {@link ActivityCompletionException} is recommended instead.<br> | ||
| */ | ||
| public final class ActivityPausedException extends ActivityCompletionException { | ||
| public ActivityPausedException(ActivityInfo info) { | ||
| super(info); | ||
| } | ||
|
|
||
| public ActivityPausedException() { | ||
| super(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
temporal-sdk/src/test/java/io/temporal/activity/ActivityPauseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. | ||
| * | ||
| * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Modifications copyright (C) 2017 Uber Technologies, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this material except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.temporal.activity; | ||
|
|
||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| import io.temporal.api.common.v1.WorkflowExecution; | ||
| import io.temporal.api.workflow.v1.PendingActivityInfo; | ||
| import io.temporal.api.workflowservice.v1.PauseActivityRequest; | ||
| import io.temporal.client.ActivityPausedException; | ||
| import io.temporal.client.WorkflowStub; | ||
| import io.temporal.testing.internal.SDKTestOptions; | ||
| import io.temporal.testing.internal.SDKTestWorkflowRule; | ||
| import io.temporal.workflow.Async; | ||
| import io.temporal.workflow.Workflow; | ||
| import io.temporal.workflow.shared.TestActivities; | ||
| import io.temporal.workflow.shared.TestWorkflows; | ||
| import java.time.Duration; | ||
| import org.junit.Assert; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| public class ActivityPauseTest { | ||
|
|
||
| @Rule | ||
| public SDKTestWorkflowRule testWorkflowRule = | ||
| SDKTestWorkflowRule.newBuilder() | ||
| .setWorkflowTypes(TestWorkflowImpl.class) | ||
| .setActivityImplementations(new HeartBeatingActivityImpl()) | ||
| .build(); | ||
|
|
||
| @Test | ||
| public void activityPause() { | ||
| assumeTrue( | ||
| "Test Server doesn't support activity pause", SDKTestWorkflowRule.useExternalService); | ||
|
|
||
| TestWorkflows.TestWorkflowReturnString workflow = | ||
| testWorkflowRule.newWorkflowStub(TestWorkflows.TestWorkflowReturnString.class); | ||
| Assert.assertEquals("I am stopped by Pause", workflow.execute()); | ||
| Assert.assertEquals( | ||
| 1, | ||
| WorkflowStub.fromTyped(workflow) | ||
| .describe() | ||
| .getRawDescription() | ||
| .getPendingActivitiesCount()); | ||
| PendingActivityInfo activityInfo = | ||
| WorkflowStub.fromTyped(workflow).describe().getRawDescription().getPendingActivities(0); | ||
| Assert.assertTrue(activityInfo.getPaused()); | ||
| } | ||
|
|
||
| public static class TestWorkflowImpl implements TestWorkflows.TestWorkflowReturnString { | ||
|
|
||
| private final TestActivities.TestActivity1 activities = | ||
| Workflow.newActivityStub( | ||
| TestActivities.TestActivity1.class, | ||
| SDKTestOptions.newActivityOptions20sScheduleToClose()); | ||
|
|
||
| @Override | ||
| public String execute() { | ||
| Async.function(activities::execute, ""); | ||
| Workflow.sleep(Duration.ofSeconds(1)); | ||
| return activities.execute("CompleteOnPause"); | ||
| } | ||
| } | ||
|
|
||
| public static class HeartBeatingActivityImpl implements TestActivities.TestActivity1 { | ||
| @Override | ||
| public String execute(String arg) { | ||
| ActivityInfo info = Activity.getExecutionContext().getInfo(); | ||
| // Have the activity pause itself | ||
| Activity.getExecutionContext() | ||
| .getWorkflowClient() | ||
| .getWorkflowServiceStubs() | ||
| .blockingStub() | ||
| .pauseActivity( | ||
| PauseActivityRequest.newBuilder() | ||
| .setNamespace(info.getNamespace()) | ||
| .setExecution( | ||
| WorkflowExecution.newBuilder().setWorkflowId(info.getWorkflowId()).build()) | ||
| .setId(info.getActivityId()) | ||
| .build()); | ||
| while (true) { | ||
| try { | ||
| Thread.sleep(1000); | ||
| // Heartbeat and verify that the correct exception is thrown | ||
| Activity.getExecutionContext().heartbeat("1"); | ||
| } catch (ActivityPausedException pe) { | ||
| if (arg.equals("CompleteOnPause")) { | ||
| // An activity should be able to succeed if paused | ||
| return "I am stopped by Pause"; | ||
| } | ||
| // This will fail the attempt, and the activity will not be retried if not unpaused | ||
| throw pe; | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrmm, so I see this is in the
clientpackage. I am guessing some other extensions of this activity completion exception are inclientpackage instead of theactivityone because they can be the cause of the outer activity failure when caught inside a workflow, correct? My concern with this exception is that it is only for inside activities and never for inside workflows.Not that it's wrong or anything, but I wonder if it's worth noting in javadoc that even if, say, max attempts were reached as a result of this being bubbled out of the activity, the workflow caller will never see this exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All other
ActivityCompletionExceptionare inclient, I agree it is not the ideal package but it is consistent with the rest of the SDK.That is just the basic failure conversion rules for activities https://docs.temporal.io/references/failures#errors-in-activities I don't think we should document it for every SDK exception.