-
Notifications
You must be signed in to change notification settings - Fork 18
Kitchensink client activity [Python, TS, .NET] #188
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
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
197 changes: 193 additions & 4 deletions
197
workers/dotnet/Temporalio.Omes/ClientActionsExecutor.cs
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 |
|---|---|---|
| @@ -1,16 +1,205 @@ | ||
| using Temporalio.Client; | ||
| using Temporalio.Exceptions; | ||
| using Temporal.Omes.KitchenSink; | ||
| using Temporalio.Api.Enums.V1; | ||
|
|
||
| namespace Temporalio.Omes; | ||
|
|
||
| public class ClientActionsExecutor | ||
| { | ||
| private readonly ITemporalClient _client; | ||
| private readonly string _workflowType = "kitchenSink"; | ||
| private readonly string _taskQueue; | ||
| private object? _workflowInput = null; | ||
| private string _runId = ""; | ||
|
|
||
| public string? WorkflowId { get; set; } | ||
|
|
||
| public ClientActionsExecutor(ITemporalClient client, string workflowId, string taskQueue) | ||
| { | ||
| _client = client; | ||
| WorkflowId = workflowId; | ||
| _taskQueue = taskQueue; | ||
| } | ||
|
|
||
| public async Task ExecuteClientSequence(ClientSequence clientSeq) | ||
| { | ||
| foreach (var actionSet in clientSeq.ActionSets) | ||
| { | ||
| await ExecuteClientActionSet(actionSet); | ||
| } | ||
| } | ||
|
|
||
| private async Task ExecuteClientActionSet(ClientActionSet actionSet) | ||
| { | ||
| if (actionSet.Concurrent) | ||
| { | ||
| throw new ApplicationFailureException("concurrent client actions are not supported", "UnsupportedOperation", nonRetryable: true); | ||
| } | ||
|
|
||
| foreach (var action in actionSet.Actions) | ||
| { | ||
| await ExecuteClientAction(action); | ||
| } | ||
| } | ||
|
|
||
| private async Task ExecuteClientAction(ClientAction action) | ||
| { | ||
| if (action.DoSignal != null) | ||
| { | ||
| await ExecuteSignalAction(action.DoSignal); | ||
| } | ||
| else if (action.DoUpdate != null) | ||
| { | ||
| await ExecuteUpdateAction(action.DoUpdate); | ||
| } | ||
| else if (action.DoQuery != null) | ||
| { | ||
| await ExecuteQueryAction(action.DoQuery); | ||
| } | ||
| else if (action.NestedActions != null) | ||
| { | ||
| await ExecuteClientActionSet(action.NestedActions); | ||
| } | ||
| else | ||
| { | ||
| throw new ArgumentException("Client action must have a recognized variant"); | ||
| } | ||
| } | ||
|
|
||
| private async Task ExecuteSignalAction(DoSignal signal) | ||
| { | ||
| string signalName; | ||
| object? signalArgs = null; | ||
|
|
||
| if (signal.DoSignalActions != null) | ||
| { | ||
| signalName = "do_actions_signal"; | ||
| signalArgs = signal.DoSignalActions; | ||
| } | ||
| else if (signal.Custom != null) | ||
| { | ||
| signalName = signal.Custom.Name; | ||
| signalArgs = signal.Custom.Args?.ToArray(); | ||
| } | ||
| else | ||
| { | ||
| throw new ArgumentException("DoSignal must have a recognizable variant"); | ||
| } | ||
|
|
||
| var args = NormalizeArgsToArray(signalArgs); | ||
| if (signal.WithStart) | ||
| { | ||
| var options = new WorkflowOptions(id: WorkflowId!, taskQueue: _taskQueue); | ||
| options.SignalWithStart(signalName, args); | ||
|
|
||
| var handle = await _client.StartWorkflowAsync( | ||
| _workflowType, | ||
| NormalizeArgsToArray(_workflowInput), | ||
| options); | ||
|
|
||
| WorkflowId = handle.Id; | ||
| _runId = handle.RunId ?? ""; | ||
| } | ||
| else | ||
| { | ||
| var handle = _client.GetWorkflowHandle(WorkflowId!); | ||
| await handle.SignalAsync(signalName, args); | ||
| } | ||
| } | ||
|
|
||
| // Always unsupported in this branch | ||
| public Task ExecuteClientSequence(ClientSequence _) | ||
| => throw new ApplicationFailureException("client actions activity is not supported", "UnsupportedOperation", nonRetryable: true); | ||
| } | ||
| private async Task ExecuteUpdateAction(DoUpdate update) | ||
| { | ||
| string updateName; | ||
| object? updateArgs; | ||
| if (update.DoActions != null) | ||
| { | ||
| updateName = "do_actions_update"; | ||
| updateArgs = update.DoActions; | ||
| } | ||
| else if (update.Custom != null) | ||
| { | ||
| updateName = update.Custom.Name; | ||
| updateArgs = update.Custom.Args.Count > 0 ? update.Custom.Args : null; | ||
| } | ||
| else | ||
| { | ||
| throw new ArgumentException("DoUpdate must have a recognizable variant"); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var args = NormalizeArgsToArray(updateArgs); | ||
| if (update.WithStart) | ||
| { | ||
| var startOperation = WithStartWorkflowOperation.Create( | ||
| _workflowType, | ||
| NormalizeArgsToArray(_workflowInput), | ||
| new(id: WorkflowId!, taskQueue: _taskQueue) | ||
| { | ||
| IdConflictPolicy = WorkflowIdConflictPolicy.UseExisting | ||
| }); | ||
|
|
||
| await _client.ExecuteUpdateWithStartWorkflowAsync( | ||
| updateName, | ||
| args, | ||
| new(startOperation)); | ||
|
|
||
| var handle = await startOperation.GetHandleAsync(); | ||
| WorkflowId = handle.Id; | ||
| _runId = handle.RunId ?? ""; | ||
| } | ||
| else | ||
| { | ||
| var handle = _client.GetWorkflowHandle(WorkflowId!); | ||
| await handle.ExecuteUpdateAsync(updateName, args); | ||
| } | ||
| } | ||
| catch (Exception) | ||
| { | ||
| if (!update.FailureExpected) | ||
| { | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task ExecuteQueryAction(DoQuery query) | ||
| { | ||
| try | ||
| { | ||
| if (query.ReportState != null) | ||
| { | ||
| var handle = _client.GetWorkflowHandle(WorkflowId!); | ||
| await handle.QueryAsync<WorkflowState>("report_state", new object[] { query.ReportState }); | ||
| } | ||
| else if (query.Custom != null) | ||
| { | ||
| var handle = _client.GetWorkflowHandle(WorkflowId!); | ||
| var queryArgs = query.Custom.Args.Count > 0 ? query.Custom.Args.ToArray() : null; | ||
| await handle.QueryAsync<object>(query.Custom.Name, queryArgs ?? Array.Empty<object>()); | ||
| } | ||
| else | ||
| { | ||
| throw new ArgumentException("DoQuery must have a recognizable variant"); | ||
| } | ||
| } | ||
| catch (Exception) | ||
| { | ||
| if (!query.FailureExpected) | ||
| { | ||
| throw; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static object[] NormalizeArgsToArray(object? args) | ||
| { | ||
| return args switch | ||
| { | ||
| null => Array.Empty<object>(), | ||
| object[] array => array, | ||
| _ => new[] { args } | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Trying to mitigate flaky tests. Better solution might be to add
AllHandlersFinishedbut I'm not quite sure how to.