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
2 changes: 1 addition & 1 deletion examples/ConductorSharp.Definitions/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"LongPollInterval": 100,
"MaxConcurrentWorkers": 10,
"SleepInterval": 500,
"PreventErrorOnBadRequest": true
"PreventErrorOnBadRequest": false
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,88 @@
using ConductorSharp.Engine.Interface;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConductorSharp.Engine.Service
{
public class WorkflowEngineBackgroundService : IHostedService
public class WorkflowEngineBackgroundService : IHostedService, IDisposable
{
private readonly ILogger<WorkflowEngineBackgroundService> _logger;
private readonly IHostApplicationLifetime _hostApplicationLifetime;
private readonly IDeploymentService _deploymentService;
private readonly ExecutionManager _executionManager;
private readonly ModuleDeployment _deployment;
private Task _executingTask;
private readonly CancellationTokenSource _stoppingCts = new();

public WorkflowEngineBackgroundService(IDeploymentService deploymentService, ExecutionManager executionManager, ModuleDeployment deployment)
public WorkflowEngineBackgroundService(
ILogger<WorkflowEngineBackgroundService> logger,
IHostApplicationLifetime hostApplicationLifetime,
IDeploymentService deploymentService,
ExecutionManager executionManager,
ModuleDeployment deployment
)
{
_logger = logger;
_hostApplicationLifetime = hostApplicationLifetime;
_deploymentService = deploymentService;
_executionManager = executionManager;
_deployment = deployment;
}

public Task StartAsync(CancellationToken cancellationToken)
{
_executingTask = RunAsync(cancellationToken);
_executingTask = RunAsync(_stoppingCts.Token);

if (_executingTask.IsCompleted)
{
return _executingTask;
}

return Task.CompletedTask;
}

private async Task RunAsync(CancellationToken cancellationToken)
{
await _deploymentService.Deploy(_deployment);
await _executionManager.StartAsync(cancellationToken);
try
{
await _deploymentService.Deploy(_deployment);
await _executionManager.StartAsync(cancellationToken);
}
catch (Exception exception)
{
_logger.LogCritical(exception, "Workflow Engine Background Service encountered an error");
throw;
}
finally
{
_hostApplicationLifetime.StopApplication();
}
}

public async Task StopAsync(CancellationToken cancellationToken)
{
if (_executingTask == null)
{
return;
}

try
{
_stoppingCts.Cancel();
}
finally
{
_logger.LogDebug("Stopping Workflow Engine Background Service");
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
}

public async Task StopAsync(CancellationToken cancellationToken) => await _executingTask;
public virtual void Dispose()
{
_stoppingCts.Cancel();
}
}
}