|
| 1 | +using DurableTask.AspNetCore.Extensions; |
| 2 | +using DurableTask.Core; |
| 3 | +using DurableTask.Core.Entities; |
| 4 | +using Microsoft.DurableTask; |
| 5 | +using Microsoft.DurableTask.Client; |
| 6 | +using Microsoft.DurableTask.Client.Entities; |
| 7 | +using Microsoft.DurableTask.Entities; |
| 8 | +using System.Diagnostics.CodeAnalysis; |
| 9 | + |
| 10 | +namespace DurableTask.AspNetCore; |
| 11 | + |
| 12 | +internal sealed class SelfHostedDurableEntityClient : DurableEntityClient |
| 13 | +{ |
| 14 | + private readonly IOrchestrationServiceClient serviceClient; |
| 15 | + private readonly EntityBackendQueries entityBackendQueries; |
| 16 | + private readonly DataConverter dataConverter; |
| 17 | + |
| 18 | + public SelfHostedDurableEntityClient( |
| 19 | + IOrchestrationServiceClient serviceClient, |
| 20 | + EntityBackendQueries entityBackendQueries, |
| 21 | + DataConverter dataConverter, |
| 22 | + string name) : base(name) |
| 23 | + { |
| 24 | + this.serviceClient = serviceClient; |
| 25 | + this.entityBackendQueries = entityBackendQueries; |
| 26 | + this.dataConverter = dataConverter; |
| 27 | + } |
| 28 | + |
| 29 | + public override async Task<CleanEntityStorageResult> CleanEntityStorageAsync(CleanEntityStorageRequest? request = null, bool continueUntilComplete = true, CancellationToken cancellation = default) |
| 30 | + { |
| 31 | + var result = await entityBackendQueries |
| 32 | + .CleanEntityStorageAsync(new EntityBackendQueries.CleanEntityStorageRequest() |
| 33 | + { |
| 34 | + RemoveEmptyEntities = request?.RemoveEmptyEntities ?? true, |
| 35 | + ReleaseOrphanedLocks = request?.ReleaseOrphanedLocks ?? true, |
| 36 | + ContinuationToken = request?.ContinuationToken, |
| 37 | + }, cancellation); |
| 38 | + |
| 39 | + return new CleanEntityStorageResult() |
| 40 | + { |
| 41 | + ContinuationToken = result.ContinuationToken, |
| 42 | + EmptyEntitiesRemoved = result.EmptyEntitiesRemoved, |
| 43 | + OrphanedLocksReleased = result.OrphanedLocksReleased, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + public override AsyncPageable<EntityMetadata> GetAllEntitiesAsync(EntityQuery? filter = null) |
| 48 | + { |
| 49 | + return GetAllEntitiesAsync(m => Convert(m), filter); |
| 50 | + } |
| 51 | + |
| 52 | + public override AsyncPageable<EntityMetadata<T>> GetAllEntitiesAsync<T>(EntityQuery? filter = null) |
| 53 | + { |
| 54 | + return GetAllEntitiesAsync(m => Convert<T>(m), filter); |
| 55 | + } |
| 56 | + |
| 57 | + public override async Task<EntityMetadata?> GetEntityAsync( |
| 58 | + EntityInstanceId id, |
| 59 | + bool includeState = true, |
| 60 | + CancellationToken cancellation = default) |
| 61 | + { |
| 62 | + var entity = await entityBackendQueries.GetEntityAsync( |
| 63 | + id.ToCore(), includeState, false, cancellation); |
| 64 | + |
| 65 | + return Convert(entity); |
| 66 | + } |
| 67 | + |
| 68 | + public override async Task<EntityMetadata<T>?> GetEntityAsync<T>( |
| 69 | + EntityInstanceId id, |
| 70 | + bool includeState = true, |
| 71 | + CancellationToken cancellation = default) |
| 72 | + { |
| 73 | + var entity = await entityBackendQueries.GetEntityAsync( |
| 74 | + id.ToCore(), includeState, false, cancellation); |
| 75 | + |
| 76 | + return Convert<T>(entity); |
| 77 | + } |
| 78 | + |
| 79 | + public override Task SignalEntityAsync( |
| 80 | + EntityInstanceId id, |
| 81 | + string operationName, |
| 82 | + object? input = null, |
| 83 | + SignalEntityOptions? options = null, |
| 84 | + CancellationToken cancellation = default) |
| 85 | + { |
| 86 | + var scheduledTime = options?.SignalTime; |
| 87 | + var serializedInput = dataConverter.Serialize(input); |
| 88 | + |
| 89 | + var eventToSend = ClientEntityHelpers.EmitOperationSignal(new OrchestrationInstance() { InstanceId = id.ToString() }, |
| 90 | + Guid.NewGuid(), |
| 91 | + operationName, |
| 92 | + serializedInput, |
| 93 | + EntityMessageEvent.GetCappedScheduledTime( |
| 94 | + DateTime.UtcNow, |
| 95 | + TimeSpan.FromDays(3), |
| 96 | + scheduledTime?.UtcDateTime)); |
| 97 | + |
| 98 | + return serviceClient.SendTaskOrchestrationMessageAsync(eventToSend.AsTaskMessage()); |
| 99 | + } |
| 100 | + |
| 101 | + private AsyncPageable<TMetadata> GetAllEntitiesAsync<TMetadata>( |
| 102 | + Func<EntityBackendQueries.EntityMetadata, TMetadata> select, |
| 103 | + EntityQuery? filter) |
| 104 | + where TMetadata : notnull |
| 105 | + { |
| 106 | + return Pageable.Create(async (continuation, size, cancellation) => |
| 107 | + { |
| 108 | + continuation ??= filter?.ContinuationToken; |
| 109 | + size ??= filter?.PageSize; |
| 110 | + EntityBackendQueries.EntityQueryResult result = await entityBackendQueries.QueryEntitiesAsync( |
| 111 | + new EntityBackendQueries.EntityQuery() |
| 112 | + { |
| 113 | + InstanceIdStartsWith = filter?.InstanceIdStartsWith ?? string.Empty, |
| 114 | + LastModifiedFrom = filter?.LastModifiedFrom?.UtcDateTime, |
| 115 | + LastModifiedTo = filter?.LastModifiedTo?.UtcDateTime, |
| 116 | + IncludeTransient = filter?.IncludeTransient ?? false, |
| 117 | + IncludeState = filter?.IncludeState ?? true, |
| 118 | + ContinuationToken = continuation, |
| 119 | + PageSize = size, |
| 120 | + }, |
| 121 | + cancellation); |
| 122 | + |
| 123 | + return new Page<TMetadata>([.. result.Results.Select(select)], result.ContinuationToken); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + [return: NotNullIfNotNull(nameof(metadata))] |
| 128 | + private EntityMetadata<T>? Convert<T>(EntityBackendQueries.EntityMetadata? metadata) |
| 129 | + { |
| 130 | + if (metadata is null) |
| 131 | + { |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + return new EntityMetadata<T>( |
| 136 | + metadata.Value.EntityId.ToExtended(), |
| 137 | + dataConverter.Deserialize<T>(metadata.Value.SerializedState)) |
| 138 | + { |
| 139 | + LastModifiedTime = metadata.Value.LastModifiedTime, |
| 140 | + BacklogQueueSize = metadata.Value.BacklogQueueSize, |
| 141 | + LockedBy = metadata.Value.LockedBy, |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + [return: NotNullIfNotNull(nameof(metadata))] |
| 146 | + private EntityMetadata? Convert(EntityBackendQueries.EntityMetadata? metadata) |
| 147 | + { |
| 148 | + if (metadata is null) |
| 149 | + { |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | + var data = metadata.Value.SerializedState is null |
| 154 | + ? null |
| 155 | + : new SerializedData(metadata.Value.SerializedState, dataConverter); |
| 156 | + |
| 157 | + return new EntityMetadata( |
| 158 | + metadata.Value.EntityId.ToExtended(), |
| 159 | + data) |
| 160 | + { |
| 161 | + LastModifiedTime = metadata.Value.LastModifiedTime, |
| 162 | + BacklogQueueSize = metadata.Value.BacklogQueueSize, |
| 163 | + LockedBy = metadata.Value.LockedBy, |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments