-
Notifications
You must be signed in to change notification settings - Fork 42
Initial implementation of plugins #548
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
Open
tconley1428
wants to merge
14
commits into
main
Choose a base branch
from
plugin/initial
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8845ce0
More plugin work
tconley1428 bac24ca
Plugins work
tconley1428 f2b204d
Reset core
tconley1428 5372194
Add some tests, change workflow/activity options
tconley1428 ab09a5c
Formatting
tconley1428 350f155
PR feedback
tconley1428 22fee96
Support both replay methods
tconley1428 0f529f6
Abstractions for replayer and worker in plugins
cretz 8f40ac4
Add test for run context
tconley1428 cde74f5
Add connection intercept test
tconley1428 4d59619
Merge branch 'main' into plugin/initial
tconley1428 5a2ca86
Merge branch 'main' into plugin/initial
tconley1428 74a2543
Condition compiler services using
tconley1428 bd30c6d
Merge branch 'plugin/initial' of https://github.com/temporalio/sdk-do…
tconley1428 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Temporalio.Client | ||
| { | ||
| /// <summary> | ||
| /// Interface for temporal client plugins. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// WARNING: This API is experimental and may change in the future. | ||
| /// </remarks> | ||
| public interface ITemporalClientPlugin | ||
| { | ||
| /// <summary> | ||
| /// Gets the plugin name. | ||
| /// </summary> | ||
| string Name { get; } | ||
|
|
||
| /// <summary> | ||
| /// Configures the client options. | ||
| /// </summary> | ||
| /// <param name="options">The client options to configure.</param> | ||
| void ConfigureClient(TemporalClientOptions options); | ||
|
|
||
| /// <summary> | ||
| /// Handles temporal connection asynchronously. | ||
| /// </summary> | ||
| /// <param name="options">The connection options.</param> | ||
| /// <param name="continuation">The continuation function.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| Task<TemporalConnection> ConnectAsync(TemporalClientConnectOptions options, Func<TemporalClientConnectOptions, Task<TemporalConnection>> continuation); | ||
| } | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Runtime.CompilerServices; | ||
|
Check failure on line 4 in src/Temporalio/Common/SimplePlugin.cs
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Temporalio.Client; | ||
| using Temporalio.Worker; | ||
|
|
||
| namespace Temporalio.Common | ||
| { | ||
| /// <summary> | ||
| /// A simple plugin that implements both client and worker plugin interfaces. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// WARNING: This API is experimental and may change in the future. | ||
| /// </remarks> | ||
| public class SimplePlugin : ITemporalClientPlugin, ITemporalWorkerPlugin | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="SimplePlugin"/> class. | ||
| /// </summary> | ||
| /// <param name="name">The plugin name.</param> | ||
| /// <param name="options">The plugin options.</param> | ||
| public SimplePlugin(string name, SimplePluginOptions? options = null) | ||
| { | ||
| Name = name; | ||
| Options = options ?? new SimplePluginOptions(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the plugin options. | ||
| /// </summary> | ||
| public SimplePluginOptions Options { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the plugin name. | ||
| /// </summary> | ||
| public string Name { get; } | ||
|
|
||
| /// <summary> | ||
| /// Configures the client options. | ||
| /// </summary> | ||
| /// <param name="options">The client options to configure.</param> | ||
| public virtual void ConfigureClient(TemporalClientOptions options) | ||
| { | ||
| options.DataConverter = Resolve(options.DataConverter, Options.DataConverterOption); | ||
| options.Interceptors = ResolveAppend(options.Interceptors, Options.ClientInterceptorsOption); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Handles temporal connection asynchronously. | ||
| /// </summary> | ||
| /// <param name="options">The connection options.</param> | ||
| /// <param name="continuation">The continuation function.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| public virtual Task<TemporalConnection> ConnectAsync( | ||
| TemporalClientConnectOptions options, | ||
| Func<TemporalClientConnectOptions, Task<TemporalConnection>> continuation) | ||
| { | ||
| return continuation(options); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the worker options. | ||
| /// </summary> | ||
| /// <param name="options">The worker options to configure.</param> | ||
| public virtual void ConfigureWorker(TemporalWorkerOptions options) | ||
| { | ||
| DoAppend(options.Activities, Options.Activities); | ||
| DoAppend(options.Workflows, Options.Workflows); | ||
| DoAppend(options.NexusServices, Options.NexusServices); | ||
| options.Interceptors = ResolveAppend(options.Interceptors, Options.WorkerInterceptorsOption); | ||
| options.WorkflowFailureExceptionTypes = ResolveAppend( | ||
| options.WorkflowFailureExceptionTypes, Options.WorkflowFailureExceptionTypesOption); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Runs the worker asynchronously. | ||
| /// </summary> | ||
| /// <typeparam name="TResult">Result type. For most worker run calls, this is | ||
| /// <see cref="ValueTuple"/>.</typeparam> | ||
| /// <param name="worker">The worker to run.</param> | ||
| /// <param name="continuation">The continuation function.</param> | ||
| /// <param name="stoppingToken">Cancellation token to stop the worker.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| public virtual async Task<TResult> RunWorkerAsync<TResult>( | ||
| TemporalWorker worker, | ||
| Func<TemporalWorker, CancellationToken, Task<TResult>> continuation, | ||
| CancellationToken stoppingToken) | ||
| { | ||
| if (Options.RunContextBefore is { } before) | ||
| { | ||
| await before().ConfigureAwait(false); | ||
| } | ||
| try | ||
| { | ||
| return await continuation(worker, stoppingToken).ConfigureAwait(false); | ||
| } | ||
| finally | ||
| { | ||
| if (Options.RunContextAfter is { } after) | ||
| { | ||
| await after().ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the replayer options. | ||
| /// </summary> | ||
| /// <param name="options">The replayer options to configure.</param> | ||
| public virtual void ConfigureReplayer(WorkflowReplayerOptions options) | ||
| { | ||
| options.DataConverter = Resolve(options.DataConverter, Options.DataConverterOption); | ||
| DoAppend(options.Workflows, Options.Workflows); | ||
| options.Interceptors = ResolveAppend(options.Interceptors, Options.WorkerInterceptorsOption); | ||
| options.WorkflowFailureExceptionTypes = ResolveAppend( | ||
| options.WorkflowFailureExceptionTypes, Options.WorkflowFailureExceptionTypesOption); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Runs the replayer asynchronously. | ||
| /// </summary> | ||
| /// <param name="replayer">The replayer to run.</param> | ||
| /// <param name="continuation">The continuation function.</param> | ||
| /// <param name="cancellationToken">Cancellation token to stop the replay.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| public virtual async Task<IEnumerable<WorkflowReplayResult>> ReplayWorkflowsAsync( | ||
| WorkflowReplayer replayer, | ||
| Func<WorkflowReplayer, CancellationToken, Task<IEnumerable<WorkflowReplayResult>>> continuation, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| if (Options.RunContextBefore is { } before) | ||
| { | ||
| await before().ConfigureAwait(false); | ||
| } | ||
| try | ||
| { | ||
| return await continuation(replayer, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| finally | ||
| { | ||
| if (Options.RunContextAfter is { } after) | ||
| { | ||
| await after().ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #if NETCOREAPP3_0_OR_GREATER | ||
| /// <summary> | ||
| /// Runs the replayer asynchronously. | ||
| /// </summary> | ||
| /// <param name="replayer">The replayer to run.</param> | ||
| /// <param name="continuation">The continuation function.</param> | ||
| /// <param name="cancellationToken">Cancellation token to stop the replay.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| public virtual async IAsyncEnumerable<WorkflowReplayResult> ReplayWorkflowsAsync( | ||
| WorkflowReplayer replayer, | ||
| Func<WorkflowReplayer, IAsyncEnumerable<WorkflowReplayResult>> continuation, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken) | ||
| { | ||
| if (Options.RunContextBefore is { } before) | ||
| { | ||
| await before().ConfigureAwait(false); | ||
| } | ||
| try | ||
| { | ||
| var asyncEnum = continuation(replayer); | ||
| await foreach (var res in asyncEnum.ConfigureAwait(false).WithCancellation(cancellationToken)) | ||
| { | ||
| yield return res; | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| if (Options.RunContextAfter is { } after) | ||
| { | ||
| await after().ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| private static T Resolve<T>(T existing, SimplePluginOptions.SimplePluginOption<T>? parameter) | ||
| { | ||
| if (parameter == null) | ||
| { | ||
| return existing; | ||
| } | ||
| var option = parameter.Constant ?? existing; | ||
| if (parameter.Configurable != null) | ||
| { | ||
| return parameter.Configurable(option); | ||
| } | ||
| return option; | ||
| } | ||
|
|
||
| private static IReadOnlyCollection<T>? ResolveAppend<T>( | ||
| IReadOnlyCollection<T>? existing, | ||
| SimplePluginOptions.SimplePluginOption<IReadOnlyCollection<T>?>? parameter) | ||
| { | ||
| if (parameter == null) | ||
| { | ||
| return existing; | ||
| } | ||
|
|
||
| var option = existing; | ||
| if (existing != null && parameter.Constant != null) | ||
| { | ||
| option = existing.Concat(parameter.Constant).ToList(); | ||
| } | ||
| else if (parameter.Constant != null) | ||
| { | ||
| option = parameter.Constant; | ||
| } | ||
| if (parameter.Configurable != null) | ||
| { | ||
| return parameter.Configurable(option); | ||
| } | ||
| return option; | ||
| } | ||
|
|
||
| private static void DoAppend<T>( | ||
| IList<T> existing, | ||
| IList<T>? parameter) | ||
| { | ||
| if (parameter == null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var item in parameter) | ||
| { | ||
| existing.Add(item); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.