-
Notifications
You must be signed in to change notification settings - Fork 53
Extract DurableTask.Core shims to common Worker package #55
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
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 was deleted.
Oops, something went wrong.
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,11 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.DurableTask; | ||
|
|
||
| /// <summary> | ||
| /// Represents a parent orchestration details. | ||
| /// </summary> | ||
| /// <param name="Name">The name of the parent orchestration.</param> | ||
| /// <param name="InstanceId">The instance ID of the parent orchestration.</param> | ||
| public record ParentOrchestrationInstance(TaskName Name, string InstanceId); |
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,14 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.DurableTask | ||
| { | ||
| // NOTE: Trying to make logs consistent with https://github.com/Azure/durabletask/blob/main/src/DurableTask.Core/Logging/LogEvents.cs. | ||
| static partial class Logs | ||
| { | ||
| [LoggerMessage(EventId = 55, Level = LogLevel.Information, Message = "{instanceId}: Evaluating custom retry handler for failed '{name}' task. Attempt = {attempt}.")] | ||
| public static partial void RetryingTask(this ILogger logger, string instanceId, string name, int attempt); | ||
| } | ||
| } |
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,103 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using DurableTask.Core; | ||
| using Microsoft.DurableTask.Converters; | ||
| using Microsoft.DurableTask.Options; | ||
| using Microsoft.DurableTask.Shims; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| namespace Microsoft.DurableTask.Worker.Shims; | ||
|
|
||
| /// <summary> | ||
| /// A shim factory for bridging between types from DurableTask.Core and those from Microsoft.DurableTask.Abstractions. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This class is intended for use with alternate .NET-based durable task runtimes. It's not intended for use | ||
| /// in application code. | ||
| /// </remarks> | ||
| public class DurableTaskShimFactory | ||
| { | ||
| readonly DataConverter dataConverter; | ||
| readonly ILoggerFactory loggerFactory; | ||
| readonly TimerOptions timerOptions; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="DurableTaskShimFactory" />. | ||
| /// </summary> | ||
| /// <param name="dataConverter">The data converter.</param> | ||
| /// <param name="loggerFactory">The logger factory.</param> | ||
| /// <param name="timerOptions">The timer options.</param> | ||
| public DurableTaskShimFactory( | ||
| DataConverter? dataConverter = null, | ||
| ILoggerFactory? loggerFactory = null, | ||
| TimerOptions? timerOptions = null) | ||
| { | ||
| this.dataConverter = dataConverter ?? JsonDataConverter.Default; | ||
| this.loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; | ||
| this.timerOptions = timerOptions ?? new(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the default <see cref="DurableTaskShimFactory" /> with default values: | ||
| /// <see cref="JsonDataConverter" />, <see cref="NullLoggerFactory" />, and | ||
| /// <see cref="TimerOptions" />. | ||
| /// </summary> | ||
| public static DurableTaskShimFactory Default { get; } = new(); | ||
|
|
||
| /// <summary> | ||
| /// Creates a <see cref="TaskActivity" /> from a <see cref="ITaskActivity" />. | ||
| /// </summary> | ||
| /// <param name="name"> | ||
| /// The name of the activity. This should be the name the activity was invoked with. | ||
| /// </param> | ||
| /// <param name="activity">The activity to wrap.</param> | ||
| /// <returns>A new <see cref="TaskActivity" />.</returns> | ||
| public TaskActivity CreateActivity(TaskName name, ITaskActivity activity) | ||
| => new TaskActivityShim(this.dataConverter, name, activity); | ||
|
|
||
| /// <summary> | ||
| /// Creates a <see cref="TaskActivity" /> from a delegate. | ||
| /// </summary> | ||
| /// <param name="name"> | ||
| /// The name of the activity. This should be the name the activity was invoked with. | ||
| /// </param> | ||
| /// <param name="implementation">The activity delegate to wrap.</param> | ||
| /// <returns>A new <see cref="TaskActivity" />.</returns> | ||
| public TaskActivity CreateActivity<TInput, TOutput>( | ||
| TaskName name, Func<TaskActivityContext, TInput?, Task<TOutput?>> implementation) | ||
| => this.CreateActivity(name, FuncTaskActivity.Create(implementation)); | ||
|
|
||
| /// <summary> | ||
| /// Creates a <see cref="TaskOrchestration" /> from a <see cref="ITaskOrchestrator" />. | ||
| /// </summary> | ||
| /// <param name="name"> | ||
| /// The name of the orchestration. This should be the name the orchestration was invoked with. | ||
| /// </param> | ||
| /// <param name="orchestrator">The orchestration to wrap.</param> | ||
| /// <param name="parent">The orchestration parent details or <c>null</c> if no parent.</param> | ||
| /// <returns>A new <see cref="TaskOrchestration" />.</returns> | ||
| public TaskOrchestration CreateOrchestration( | ||
| TaskName name, ITaskOrchestrator orchestrator, ParentOrchestrationInstance? parent = null) | ||
| { | ||
| OrchestrationInvocationContext context = new( | ||
| name, this.dataConverter, this.loggerFactory, this.timerOptions, parent); | ||
| return new TaskOrchestrationShim(context, orchestrator); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a <see cref="TaskOrchestration" /> from a <see cref="ITaskOrchestrator" />. | ||
| /// </summary> | ||
| /// <param name="name"> | ||
| /// The name of the orchestration. This should be the name the orchestration was invoked with. | ||
| /// </param> | ||
| /// <param name="implementation">The orchestration delegate to wrap.</param> | ||
| /// <param name="parent">The orchestration parent details or <c>null</c> if no parent.</param> | ||
| /// <returns>A new <see cref="TaskOrchestration" />.</returns> | ||
| public TaskOrchestration CreateOrchestration<TInput, TOutput>( | ||
| TaskName name, | ||
| Func<TaskOrchestrationContext, TInput?, Task<TOutput?>> implementation, | ||
| ParentOrchestrationInstance? parent = null) | ||
| => this.CreateOrchestration(name, FuncTaskOrchestrator.Create(implementation), parent); | ||
| } | ||
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,51 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.DurableTask.Shims; | ||
|
|
||
| /// <summary> | ||
| /// Helpers for creating <see cref="ITaskActivity" />. | ||
| /// </summary> | ||
| public static class FuncTaskActivity | ||
| { | ||
| /// <summary> | ||
| /// Creates a new <see cref="TaskActivityBase{TInput, TOutput}" /> with | ||
| /// the provided function as the implementation. | ||
| /// </summary> | ||
| /// <typeparam name="TInput">The input type.</typeparam> | ||
| /// <typeparam name="TOutput">The output type.</typeparam> | ||
| /// <param name="implementation">The activity implementation.</param> | ||
| /// <returns>A new activity.</returns> | ||
| public static TaskActivityBase<TInput, TOutput> Create<TInput, TOutput>( | ||
| Func<TaskActivityContext, TInput?, Task<TOutput?>> implementation) | ||
| { | ||
| return new Implementation<TInput, TOutput>(implementation); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Implementation of <see cref="TaskActivityBase{TInput, TOutput}"/> that uses | ||
| /// a <see cref="Func{T, TResult}"/> delegate as its implementation. | ||
| /// </summary> | ||
| /// <typeparam name="TInput">The Activity input type.</typeparam> | ||
| /// <typeparam name="TOutput">The Activity output type.</typeparam> | ||
| class Implementation<TInput, TOutput> : TaskActivityBase<TInput, TOutput> | ||
| { | ||
| readonly Func<TaskActivityContext, TInput?, Task<TOutput?>> implementation; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Implementation{TInput, TOutput}"/> class. | ||
| /// </summary> | ||
| /// <param name="implementation">The Activity function.</param> | ||
| public Implementation(Func<TaskActivityContext, TInput?, Task<TOutput?>> implementation) | ||
| { | ||
| this.implementation = implementation; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override Task<TOutput?> OnRunAsync(TaskActivityContext context, TInput? input) | ||
| { | ||
| return this.implementation(context, input); | ||
| } | ||
| } | ||
|
|
||
| } |
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,51 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.DurableTask.Shims; | ||
|
|
||
| /// <summary> | ||
| /// Helpers for creating <see cref="ITaskOrchestrator" />. | ||
| /// </summary> | ||
| public static class FuncTaskOrchestrator | ||
| { | ||
| /// <summary> | ||
| /// Creates a new <see cref="TaskOrchestratorBase{TInput, TOutput}" /> with | ||
| /// the provided function as the implementation. | ||
| /// </summary> | ||
| /// <typeparam name="TInput">The input type.</typeparam> | ||
| /// <typeparam name="TOutput">The output type.</typeparam> | ||
| /// <param name="implementation">The orchestrator implementation.</param> | ||
| /// <returns>A new orchestrator.</returns> | ||
| public static TaskOrchestratorBase<TInput, TOutput> Create<TInput, TOutput>( | ||
| Func<TaskOrchestrationContext, TInput?, Task<TOutput?>> implementation) | ||
| { | ||
| return new Implementation<TInput, TOutput>(implementation); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Implementation of <see cref="TaskOrchestratorBase{TInput, TOutput}"/> that uses | ||
| /// a <see cref="Func{T, TResult}"/> delegate as its implementation. | ||
| /// </summary> | ||
| /// <typeparam name="TInput">The orchestrator input type.</typeparam> | ||
| /// <typeparam name="TOutput">The orchestrator output type.</typeparam> | ||
| class Implementation<TInput, TOutput> : TaskOrchestratorBase<TInput, TOutput> | ||
| { | ||
| readonly Func<TaskOrchestrationContext, TInput?, Task<TOutput?>> implementation; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Implementation{TInput, TOutput}"/> class. | ||
| /// </summary> | ||
| /// <param name="implementation">The orchestrator function.</param> | ||
| public Implementation(Func<TaskOrchestrationContext, TInput?, Task<TOutput?>> implementation) | ||
| { | ||
| this.implementation = implementation; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override Task<TOutput?> OnRunAsync(TaskOrchestrationContext context, TInput? input) | ||
| { | ||
| return this.implementation(context, input); | ||
| } | ||
| } | ||
|
|
||
| } |
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,28 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using CoreJsonDataConverter = DurableTask.Core.Serializing.JsonDataConverter; | ||
|
|
||
| namespace Microsoft.DurableTask.Worker.Shims; | ||
|
|
||
| /// <summary> | ||
| /// A shim to go from <see cref="DataConverter" /> to <see cref="CoreJsonDataConverter" />. | ||
| /// </summary> | ||
| sealed class JsonDataConverterShim : CoreJsonDataConverter | ||
| { | ||
| readonly DataConverter innerConverter; | ||
|
|
||
| public JsonDataConverterShim(DataConverter innerConverter) | ||
| { | ||
| this.innerConverter = innerConverter; | ||
| } | ||
|
|
||
| public override string Serialize(object value) | ||
| => this.innerConverter.Serialize(value); | ||
|
|
||
| public override string Serialize(object value, bool formatted) | ||
| => this.Serialize(value); | ||
|
|
||
| public override object Deserialize(string data, Type objectType) | ||
| => this.innerConverter.Deserialize(data, objectType); | ||
| } |
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,22 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.DurableTask.Options; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.DurableTask.Worker.Shims; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="OrchestrationInvocationContext"/> class. | ||
| /// </summary> | ||
| /// <param name="Name">The invoked orchestration name.</param> | ||
| /// <param name="DataConverter">The data converter for this orchestration.</param> | ||
| /// <param name="LoggerFactory">The logger factory for this orchestration.</param> | ||
| /// <param name="TimerOptions">The configuration options for durable timers.</param> | ||
| /// <param name="Parent">The orchestration parent details.</param> | ||
| record OrchestrationInvocationContext( | ||
| TaskName Name, | ||
| DataConverter DataConverter, | ||
| ILoggerFactory LoggerFactory, | ||
| TimerOptions TimerOptions, | ||
| ParentOrchestrationInstance? Parent = null); |
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
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.