Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
Handling cancellation on main engine
  • Loading branch information
mikolaj-milewski committed Aug 27, 2024
1 parent 9ecc71b commit e51d3c5
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Core/Stateflows/Common/Engine/StateflowsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Stateflows.Common.Engine;
using Stateflows.Common.Classes;
Expand All @@ -14,7 +13,7 @@

namespace Stateflows.Common
{
internal class StateflowsEngine : /*BackgroundService, */IHostedService
internal class StateflowsEngine : IHostedService
{
private readonly IServiceScope Scope;
private IServiceProvider ServiceProvider => Scope.ServiceProvider;
Expand All @@ -23,6 +22,8 @@ internal class StateflowsEngine : /*BackgroundService, */IHostedService
private readonly IStateflowsTenantProvider TenantProvider;
private readonly ITenantAccessor TenantAccessor;
private Dictionary<string, IEventProcessor> processors;
private readonly CancellationTokenSource CancellationTokenSource = new CancellationTokenSource();

private Dictionary<string, IEventProcessor> Processors
=> processors ??= ServiceProvider.GetRequiredService<IEnumerable<IEventProcessor>>().ToDictionary(p => p.BehaviorType, p => p);

Expand Down Expand Up @@ -55,9 +56,9 @@ public Task StartAsync(CancellationToken cancellationToken)
{
executionTask = Task.Run(() =>
{
while (!cancellationToken.IsCancellationRequested)
while (!CancellationTokenSource.Token.IsCancellationRequested)
{
if (!EventQueue.WaitAsync(cancellationToken).GetAwaiter().GetResult())
if (!EventQueue.WaitAsync(CancellationTokenSource.Token).GetAwaiter().GetResult())
{
continue;
}
Expand Down Expand Up @@ -147,6 +148,8 @@ public async Task<EventStatus> ProcessEventAsync<TEvent>(BehaviorId id, TEvent @

public Task StopAsync(CancellationToken cancellationToken)
{
CancellationTokenSource.Cancel();

executionTask.Wait();

return Task.CompletedTask;
Expand Down

0 comments on commit e51d3c5

Please sign in to comment.