| | | 1 | | using Elsa.Mediator.Contracts; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.Runtime; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Invokes activities from a background worker within the context of its workflow instance using a local background wor |
| | | 8 | | /// </summary> |
| | 141 | 9 | | public class LocalBackgroundActivityScheduler(IJobQueue jobQueue, IServiceScopeFactory scopeFactory) : IBackgroundActivi |
| | | 10 | | { |
| | | 11 | | public Task<string> CreateAsync(ScheduledBackgroundActivity scheduledBackgroundActivity, CancellationToken cancellat |
| | | 12 | | { |
| | 6 | 13 | | var jobId = jobQueue.Create(async ct => await InvokeBackgroundActivity(scheduledBackgroundActivity, ct)); |
| | 3 | 14 | | return Task.FromResult(jobId); |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | public Task ScheduleAsync(string jobId, CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 3 | 19 | | jobQueue.Enqueue(jobId); |
| | 3 | 20 | | return Task.CompletedTask; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public Task<string> ScheduleAsync(ScheduledBackgroundActivity scheduledBackgroundActivity, CancellationToken cancell |
| | | 25 | | { |
| | 0 | 26 | | var jobId = jobQueue.Enqueue(async ct => await InvokeBackgroundActivity(scheduledBackgroundActivity, ct)); |
| | 0 | 27 | | return Task.FromResult(jobId); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public Task UnscheduledAsync(string jobId, CancellationToken cancellationToken = default) |
| | | 31 | | { |
| | 3 | 32 | | jobQueue.Dequeue(jobId); |
| | 3 | 33 | | return Task.CompletedTask; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public Task CancelAsync(string jobId, CancellationToken cancellationToken = default) |
| | | 38 | | { |
| | 0 | 39 | | jobQueue.Cancel(jobId); |
| | 0 | 40 | | return Task.CompletedTask; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private async Task InvokeBackgroundActivity(ScheduledBackgroundActivity scheduledBackgroundActivity, CancellationTok |
| | | 44 | | { |
| | 3 | 45 | | using var scope = scopeFactory.CreateScope(); |
| | 3 | 46 | | var backgroundActivityInvoker = scope.ServiceProvider.GetRequiredService<IBackgroundActivityInvoker>(); |
| | 3 | 47 | | await backgroundActivityInvoker.ExecuteAsync(scheduledBackgroundActivity, cancellationToken); |
| | 3 | 48 | | } |
| | | 49 | | } |