Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/Abstractions/FuncTaskOrchestrator.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Abstractions/IDurableTaskRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;

namespace Microsoft.DurableTask;

/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Abstractions/ParentOrchestrationInstance.cs
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);
2 changes: 1 addition & 1 deletion src/Abstractions/TaskOrchestrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class TaskOrchestrationContext
/// <summary>
/// Gets the parent instance or <c>null</c> if there is no parent orchestration.
/// </summary>
public abstract ParentInstance? Parent { get; }
public abstract ParentOrchestrationInstance? Parent { get; }

/// <summary>
/// Gets the current orchestration time in UTC.
Expand Down
14 changes: 14 additions & 0 deletions src/Worker/Core/Logs.cs
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);
}
}
103 changes: 103 additions & 0 deletions src/Worker/Core/Shims/DurableTaskShimFactory.cs
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);
}
51 changes: 51 additions & 0 deletions src/Worker/Core/Shims/FuncTaskActivity.cs
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);
}
}

}
51 changes: 51 additions & 0 deletions src/Worker/Core/Shims/FuncTaskOrchestrator.cs
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);
}
}

}
28 changes: 28 additions & 0 deletions src/Worker/Core/Shims/JsonDataConverterShim.cs
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);
}
22 changes: 22 additions & 0 deletions src/Worker/Core/Shims/OrchestrationInvocationContext.cs
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);
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,7 @@

using DurableTask.Core;

namespace Microsoft.DurableTask;

class TaskActivityShim<TInput, TOutput> : TaskActivityShim
{
public TaskActivityShim(
DataConverter dataConverter,
TaskName name,
Func<TaskActivityContext, TInput?, Task<TOutput?>> implementation)
: base(dataConverter, name, new LambdaActivity(implementation))
{
}

sealed class LambdaActivity : TaskActivityBase<TInput, TOutput>
{
readonly Func<TaskActivityContext, TInput?, Task<TOutput?>> implementation;

public LambdaActivity(Func<TaskActivityContext, TInput?, Task<TOutput?>> implementation)
{
this.implementation = implementation;
}

protected override Task<TOutput?> OnRunAsync(TaskActivityContext context, TInput? input)
{
return this.implementation(context, input);
}
}
}
namespace Microsoft.DurableTask.Worker.Shims;

class TaskActivityShim : TaskActivity
{
Expand Down
Loading