diff --git a/src/LLL.DurableTask.EFCore/EFCoreOrchestrationServiceClient.cs b/src/LLL.DurableTask.EFCore/EFCoreOrchestrationServiceClient.cs index 9122778..1612fd2 100644 --- a/src/LLL.DurableTask.EFCore/EFCoreOrchestrationServiceClient.cs +++ b/src/LLL.DurableTask.EFCore/EFCoreOrchestrationServiceClient.cs @@ -99,25 +99,37 @@ public async Task GetOrchestrationHistoryAsync(string instanceId, string public async Task> GetOrchestrationStateAsync(string instanceId, bool allExecutions) { - var state = await GetOrchestrationStateAsync(instanceId, null); - if (state is null) - return Array.Empty(); + using var dbContext = _dbContextFactory.CreateDbContext(); + + var query = dbContext.Executions + .Where(e => e.InstanceId == instanceId); + + if (!allExecutions) + query = query.Join(dbContext.Instances, x => x.ExecutionId, x => x.LastExecutionId, (x, y) => x); - return new[] { state }; + var executions = await query.ToArrayAsync(); + + return executions.Select(_executionMapper.MapToState).ToArray(); } public async Task GetOrchestrationStateAsync(string instanceId, string executionId) { using var dbContext = _dbContextFactory.CreateDbContext(); - var instance = await dbContext.Executions - .Where(e => e.InstanceId == instanceId && (executionId == null || e.ExecutionId == executionId)) - .OrderByDescending(e => e.CreatedTime) - .FirstOrDefaultAsync(); - if (instance is null) + var query = dbContext.Executions + .Where(e => e.InstanceId == instanceId); + + if (executionId is null) + query = query.Join(dbContext.Instances, x => x.ExecutionId, x => x.LastExecutionId, (x, y) => x); + else + query = query.Where(e => e.ExecutionId == executionId); + + var execution = await query.FirstOrDefaultAsync(); + + if (execution is null) return null; - return _executionMapper.MapToState(instance); + return _executionMapper.MapToState(execution); } public async Task PurgeOrchestrationHistoryAsync(DateTime thresholdDateTimeUtc, OrchestrationStateTimeRangeFilterType timeRangeFilterType)