diff --git a/src/bundles/Elsa.Server.Web/Program.cs b/src/bundles/Elsa.Server.Web/Program.cs index 229c9380f..7df6fea32 100644 --- a/src/bundles/Elsa.Server.Web/Program.cs +++ b/src/bundles/Elsa.Server.Web/Program.cs @@ -24,7 +24,7 @@ using Proto.Persistence.SqlServer; const bool useMongoDb = false; const bool useSqlServer = false; const bool useDapper = false; -const bool useProtoActor = true; +const bool useProtoActor = false; const bool useHangfire = false; const bool useQuartz = true; const bool useMassTransit = true; diff --git a/src/modules/Elsa.Workflows.Core/Extensions/ModuleExtensions.cs b/src/modules/Elsa.Workflows.Core/Extensions/ModuleExtensions.cs index 70f989e5b..a591ccba7 100644 --- a/src/modules/Elsa.Workflows.Core/Extensions/ModuleExtensions.cs +++ b/src/modules/Elsa.Workflows.Core/Extensions/ModuleExtensions.cs @@ -16,6 +16,8 @@ public static class ModuleExtensions public static IServiceCollection AddStorageDriver(this IServiceCollection services) where T : class, IStorageDriver { - return services.AddSingleton(); + return services + .AddSingleton() + .AddSingleton(); } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowRuntime.cs b/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowRuntime.cs index 746481379..3421c4cb6 100644 --- a/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowRuntime.cs +++ b/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowRuntime.cs @@ -88,4 +88,9 @@ public interface IWorkflowRuntime /// Counts the number of workflow instances based on the provided query args. /// Task CountRunningWorkflowsAsync(CountRunningWorkflowsRequest request, CancellationToken cancellationToken = default); + + /// + /// Merges the specified workflow state into the workflow runtime. + /// + Task MergeWorkflowStateAsync(WorkflowState workflowState, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Runtime/Services/DefaultBackgroundActivityInvoker.cs b/src/modules/Elsa.Workflows.Runtime/Services/DefaultBackgroundActivityInvoker.cs index bc40b4473..55d0f8697 100644 --- a/src/modules/Elsa.Workflows.Runtime/Services/DefaultBackgroundActivityInvoker.cs +++ b/src/modules/Elsa.Workflows.Runtime/Services/DefaultBackgroundActivityInvoker.cs @@ -57,6 +57,7 @@ public class DefaultBackgroundActivityInvoker : IBackgroundActivityInvoker public async Task ExecuteAsync(ScheduledBackgroundActivity scheduledBackgroundActivity, CancellationToken cancellationToken = default) { var workflowInstanceId = scheduledBackgroundActivity.WorkflowInstanceId; + var workflowState = await _workflowRuntime.ExportWorkflowStateAsync(workflowInstanceId, cancellationToken); if (workflowState == null) @@ -118,7 +119,8 @@ public class DefaultBackgroundActivityInvoker : IBackgroundActivityInvoker // - Bookmarks workflowState = _workflowStateExtractor.Extract(workflowExecutionContext); await _variablePersistenceManager.SaveVariablesAsync(workflowExecutionContext); - await _workflowRuntime.ImportWorkflowStateAsync(workflowState, cancellationToken); + await _workflowRuntime.MergeWorkflowStateAsync(workflowState, cancellationToken); + //await _workflowRuntime.ImportWorkflowStateAsync(workflowState, cancellationToken); // Process bookmarks. var newBookmarks = workflowExecutionContext.Bookmarks.ToList(); diff --git a/src/modules/Elsa.Workflows.Runtime/Services/DefaultWorkflowRuntime.cs b/src/modules/Elsa.Workflows.Runtime/Services/DefaultWorkflowRuntime.cs index f97926a0c..4c00e1e67 100644 --- a/src/modules/Elsa.Workflows.Runtime/Services/DefaultWorkflowRuntime.cs +++ b/src/modules/Elsa.Workflows.Runtime/Services/DefaultWorkflowRuntime.cs @@ -292,6 +292,19 @@ public class DefaultWorkflowRuntime : IWorkflowRuntime return await _workflowInstanceStore.CountAsync(filter, cancellationToken); } + public async Task MergeWorkflowStateAsync(WorkflowState workflowState, CancellationToken cancellationToken = default) + { + var existingWorkflowInstance = (await _workflowInstanceStore.FindAsync(workflowState.Id, cancellationToken))!; + var workflowInstance = _workflowStateMapper.Map(workflowState)!; + + foreach (var bookmark in workflowState.Bookmarks) + { + existingWorkflowInstance.WorkflowState.Bookmarks.RemoveWhere(x => x.Id == bookmark.Id); + existingWorkflowInstance.WorkflowState.Bookmarks.Add(bookmark); + } + await _workflowInstanceManager.SaveAsync(workflowInstance, cancellationToken); + } + private async Task StartWorkflowAsync(IWorkflowHost workflowHost, StartWorkflowRuntimeOptions options) { var workflowInstanceId = string.IsNullOrEmpty(options.InstanceId) ? _identityGenerator.GenerateId() : options.InstanceId; @@ -423,11 +436,5 @@ public class DefaultWorkflowRuntime : IWorkflowRuntime private async Task AcquireLockAsync(string resource, CancellationToken cancellationToken) { return await _distributedLockProvider.AcquireLockAsync(resource, TimeSpan.FromMinutes(2), cancellationToken); - // if (AcquiredLock.Value?.Key == resource) - // return AcquiredLock.Value.Lock; - // - // var distributedLock = await _distributedLockProvider.AcquireLockAsync(resource, TimeSpan.FromMinutes(2), cancellationToken); - // AcquiredLock.Value = new AcquiredLock { Lock = distributedLock, Key = resource }; - // return distributedLock; } } \ No newline at end of file