elsa-core/src/modules/Elsa.Jobs/Implementations/JobRunner.cs
2022-08-11 12:45:07 +02:00

28 lines
867 B
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Jobs.Models;
using Elsa.Jobs.Notifications;
using Elsa.Jobs.Services;
using Elsa.Mediator.Services;
namespace Elsa.Jobs.Implementations;
public class JobRunner : IJobRunner
{
private readonly IEventPublisher _eventPublisher;
private readonly IServiceProvider _serviceProvider;
public JobRunner(IEventPublisher eventPublisher, IServiceProvider serviceProvider)
{
_eventPublisher = eventPublisher;
_serviceProvider = serviceProvider;
}
public async Task RunJobAsync(IJob job, CancellationToken cancellationToken = default)
{
var context = new JobExecutionContext(_serviceProvider, cancellationToken);
await job.ExecuteAsync(context);
await _eventPublisher.PublishAsync(new JobExecuted(job), cancellationToken);
}
}