| | | 1 | | using Elsa.Alterations.Core.Contracts; |
| | | 2 | | using Elsa.Alterations.Core.Entities; |
| | | 3 | | using Elsa.Alterations.Core.Enums; |
| | | 4 | | using Elsa.Alterations.Core.Filters; |
| | | 5 | | using Elsa.Alterations.Core.Notifications; |
| | | 6 | | using Elsa.Common; |
| | | 7 | | using Elsa.Mediator.Contracts; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Alterations.Services; |
| | | 10 | | |
| | | 11 | | /// <inheritdoc /> |
| | | 12 | | public class DefaultAlterationJobRunner : IAlterationJobRunner |
| | | 13 | | { |
| | | 14 | | private readonly IAlterationPlanStore _alterationPlanStore; |
| | | 15 | | private readonly IAlterationJobStore _alterationJobStore; |
| | | 16 | | private readonly IAlterationRunner _alterationRunner; |
| | | 17 | | private readonly INotificationSender _notificationSender; |
| | | 18 | | private readonly ISystemClock _systemClock; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Initializes a new instance of the <see cref="DefaultAlterationJobRunner"/> class. |
| | | 22 | | /// </summary> |
| | 0 | 23 | | public DefaultAlterationJobRunner( |
| | 0 | 24 | | IAlterationPlanStore alterationPlanStore, |
| | 0 | 25 | | IAlterationJobStore alterationJobStore, |
| | 0 | 26 | | IAlterationRunner alterationRunner, |
| | 0 | 27 | | INotificationSender notificationSender, |
| | 0 | 28 | | ISystemClock systemClock) |
| | | 29 | | { |
| | 0 | 30 | | _alterationPlanStore = alterationPlanStore; |
| | 0 | 31 | | _alterationJobStore = alterationJobStore; |
| | 0 | 32 | | _alterationRunner = alterationRunner; |
| | 0 | 33 | | _notificationSender = notificationSender; |
| | 0 | 34 | | _systemClock = systemClock; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public async Task<AlterationJob> RunAsync(string jobId, CancellationToken cancellationToken = default) |
| | | 39 | | { |
| | 0 | 40 | | var job = (await _alterationJobStore.FindAsync(new AlterationJobFilter { Id = jobId }, cancellationToken))!; |
| | 0 | 41 | | var plan = (await _alterationPlanStore.FindAsync(new AlterationPlanFilter { Id = job.PlanId }, cancellationToken |
| | 0 | 42 | | var workflowInstanceId = job.WorkflowInstanceId; |
| | | 43 | | |
| | 0 | 44 | | job.Status = AlterationJobStatus.Running; |
| | 0 | 45 | | job.StartedAt = _systemClock.UtcNow; |
| | 0 | 46 | | await _alterationJobStore.SaveAsync(job, cancellationToken); |
| | | 47 | | |
| | 0 | 48 | | var result = await _alterationRunner.RunAsync(workflowInstanceId, plan.Alterations, cancellationToken); |
| | | 49 | | |
| | 0 | 50 | | job.Status = result.IsSuccessful ? AlterationJobStatus.Completed : AlterationJobStatus.Failed; |
| | 0 | 51 | | job.Log = result.Log.LogEntries.ToList(); |
| | 0 | 52 | | job.CompletedAt = _systemClock.UtcNow; |
| | 0 | 53 | | await _alterationJobStore.SaveAsync(job, cancellationToken); |
| | 0 | 54 | | await _notificationSender.SendAsync(new AlterationJobCompleted(job, result.WorkflowHasScheduledWork), cancellati |
| | | 55 | | |
| | 0 | 56 | | return job; |
| | 0 | 57 | | } |
| | | 58 | | } |