Skip to content

Commit a3e37f5

Browse files
author
Eddie Wood
committed
WorkflowOptions support for configuring max number of concurrent tasks.
1 parent cf002b8 commit a3e37f5

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/WorkflowCore/Models/WorkflowOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace WorkflowCore.Models
88
{
99
public class WorkflowOptions
1010
{
11+
internal static readonly int MinimumNumberOfConcurrentItems = 2;
12+
1113
internal Func<IServiceProvider, IPersistenceProvider> PersistanceFactory;
1214
internal Func<IServiceProvider, IQueueProvider> QueueFactory;
1315
internal Func<IServiceProvider, IDistributedLockProvider> LockFactory;
@@ -16,6 +18,7 @@ public class WorkflowOptions
1618
internal TimeSpan PollInterval;
1719
internal TimeSpan IdleTime;
1820
internal TimeSpan ErrorRetryInterval;
21+
internal int? MaxConcurrentItems;
1922

2023
public IServiceCollection Services { get; private set; }
2124

@@ -67,6 +70,16 @@ public void UseErrorRetryInterval(TimeSpan interval)
6770
{
6871
ErrorRetryInterval = interval;
6972
}
73+
74+
public void UseMaxConcurrentItems(int? maxConcurrentItems)
75+
{
76+
if (maxConcurrentItems.HasValue && maxConcurrentItems.Value < MinimumNumberOfConcurrentItems)
77+
{
78+
throw new ArgumentOutOfRangeException($"If {nameof(maxConcurrentItems)} is specified, it cannot be less than ${MinimumNumberOfConcurrentItems}.");
79+
}
80+
81+
MaxConcurrentItems = maxConcurrentItems;
82+
}
7083
}
7184

7285
}

src/WorkflowCore/Services/BackgroundTasks/EventConsumer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal class EventConsumer : QueueConsumer, IBackgroundTask
1313
private readonly IPersistenceProvider _persistenceStore;
1414
private readonly IDistributedLockProvider _lockProvider;
1515
private readonly IDateTimeProvider _datetimeProvider;
16-
protected override int MaxConcurrentItems => 2;
16+
protected override int MaxConcurrentItems => WorkflowOptions.MinimumNumberOfConcurrentItems;
1717
protected override QueueType Queue => QueueType.Event;
1818

1919
public EventConsumer(IPersistenceProvider persistenceStore, IQueueProvider queueProvider, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, IWorkflowRegistry registry, IDistributedLockProvider lockProvider, WorkflowOptions options, IDateTimeProvider datetimeProvider)

src/WorkflowCore/Services/BackgroundTasks/QueueConsumer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace WorkflowCore.Services.BackgroundTasks
1111
internal abstract class QueueConsumer : IBackgroundTask
1212
{
1313
protected abstract QueueType Queue { get; }
14-
protected virtual int MaxConcurrentItems => Math.Max(Environment.ProcessorCount, 2);
14+
protected virtual int MaxConcurrentItems => Options.MaxConcurrentItems ?? Math.Max(Environment.ProcessorCount, WorkflowOptions.MinimumNumberOfConcurrentItems);
1515
protected virtual bool EnableSecondPasses => false;
1616

1717
protected readonly IQueueProvider QueueProvider;

0 commit comments

Comments
 (0)