elsa-core/src/modules/Elsa.Jobs/Implementations/JobRunner.cs

28 lines
867 B
C#
Raw Normal View History

using System;
using System.Threading;
using System.Threading.Tasks;
2022-03-15 12:00:48 +00:00
using Elsa.Jobs.Models;
2022-08-10 14:02:53 +00:00
using Elsa.Jobs.Notifications;
using Elsa.Jobs.Services;
2022-08-10 14:02:53 +00:00
using Elsa.Mediator.Services;
namespace Elsa.Jobs.Implementations;
2022-03-07 21:56:16 +00:00
public class JobRunner : IJobRunner
{
2022-08-10 14:02:53 +00:00
private readonly IEventPublisher _eventPublisher;
2022-03-15 12:00:48 +00:00
private readonly IServiceProvider _serviceProvider;
2022-08-10 14:02:53 +00:00
public JobRunner(IEventPublisher eventPublisher, IServiceProvider serviceProvider)
{
2022-08-10 14:02:53 +00:00
_eventPublisher = eventPublisher;
2022-03-15 12:00:48 +00:00
_serviceProvider = serviceProvider;
}
2022-03-07 21:56:16 +00:00
public async Task RunJobAsync(IJob job, CancellationToken cancellationToken = default)
{
2022-03-15 12:00:48 +00:00
var context = new JobExecutionContext(_serviceProvider, cancellationToken);
await job.ExecuteAsync(context);
2022-08-10 14:02:53 +00:00
await _eventPublisher.PublishAsync(new JobExecuted(job), cancellationToken);
}
}