Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit 005ece1

Browse files
committed
Replaces 'IsLongRunning' by a 'IgnoreParallelism' and a 'Options' property
1 parent 757f0a7 commit 005ece1

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

DeltaSight.SimpleBackgroundWorker.Tests/ParallelismTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public async Task Test_WithManyDegreesOfParallelism()
3838
await Task.Delay(jobTimeMs, t);
3939
ended.Add(DateTime.Now);
4040
semaphore.Release();
41-
}, $"Job {i}", e =>
41+
}, name: $"Job {i}", errorCallback: ex =>
4242
{
43-
Assert.Fail(e.Message);
43+
Assert.Fail(ex.Message);
4444

4545
return Task.CompletedTask;
4646
}))
@@ -49,7 +49,7 @@ public async Task Test_WithManyDegreesOfParallelism()
4949
await bgWorker.QueueAsync(jobs);
5050

5151
// Queue some long running jobs (that shouldn't interfere with max. degree of parallelism)
52-
await bgWorker.QueueAsync(Enumerable.Range(1, 10).Select(x => new BackgroundWorkItem(t => Task.Delay(-1, t), isLongRunning:true)));
52+
await bgWorker.QueueAsync(Enumerable.Range(1, 10).Select(x => BackgroundWorkItem.Create(t => Task.Delay(-1, t), ignoreParallelism:true)));
5353

5454
var startAt = DateTime.Now;
5555

@@ -62,7 +62,7 @@ public async Task Test_WithManyDegreesOfParallelism()
6262
Assert.Multiple(() =>
6363
{
6464
Assert.That(ended, Has.Count.EqualTo(count));
65-
Assert.That((ended.Max() - startAt).TotalMilliseconds, Is.EqualTo(m * jobTimeMs).Within(jobTimeMs));
65+
Assert.That((ended.Max() - startAt).TotalMilliseconds, Is.EqualTo(m * jobTimeMs).Within(2 * jobTimeMs));
6666
});
6767

6868
await host.StopAsync();

DeltaSight.SimpleBackgroundWorker/BackgroundWorkItem.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ namespace DeltaSight.SimpleBackgroundWorker;
44

55
public struct BackgroundWorkItem
66
{
7-
public BackgroundWorkItem(Func<CancellationToken, Task> execute, string name = "Untitled", Func<Exception, Task>? onError = null, bool isLongRunning = false, TimeSpan? cancelAfter = null)
7+
private BackgroundWorkItem(Func<CancellationToken, Task> execute, string name, Func<Exception, Task>? onError, bool ignoreParallelism, TaskCreationOptions options, TimeSpan? cancelAfter)
88
{
99
Name = name;
1010
Execute = execute;
1111
OnError = onError;
12-
IsLongRunning = isLongRunning;
12+
Options = options;
1313
CancelAfter = cancelAfter;
14+
IgnoreParallelism = ignoreParallelism;
1415
}
1516

1617
public Guid Guid { get; } = Guid.NewGuid();
@@ -19,24 +20,24 @@ public BackgroundWorkItem(Func<CancellationToken, Task> execute, string name = "
1920
public Func<CancellationToken, Task> Execute { get; }
2021
public Func<Exception, Task>? OnError { get; } = null;
2122

22-
/// <summary>
23-
/// Flag to indicate the task is long running
24-
/// <remarks>Long running tasks will not impact maximum degree of parallelism
25-
/// </summary>
26-
public bool IsLongRunning { get; }
23+
public TaskCreationOptions Options { get; }
24+
25+
public bool IgnoreParallelism { get; }
2726

2827
/// <summary>
2928
/// If set, the job will be cancelled automatically if the execution takes longer
3029
/// </summary>
3130
public TimeSpan? CancelAfter { get; }
3231

3332
public static BackgroundWorkItem Create(Func<CancellationToken, Task> executeWork,
34-
[CallerArgumentExpression("executeWork")] string name = "Untitled",
33+
string? name = null,
3534
Func<Exception, Task>? errorCallback = null,
36-
bool isLongRunning = false,
37-
TimeSpan? cancelAfter = null
38-
)
35+
bool ignoreParallelism = false,
36+
TaskCreationOptions options = TaskCreationOptions.None,
37+
TimeSpan? cancelAfter = null,
38+
[CallerMemberName] string callerMemberName = "",
39+
[CallerArgumentExpression("executeWork")] string callerArgumentExpression = "")
3940
{
40-
return new BackgroundWorkItem(executeWork, name, errorCallback, isLongRunning, cancelAfter);
41+
return new BackgroundWorkItem(executeWork, name ?? $"{callerMemberName}::{callerArgumentExpression}", errorCallback, ignoreParallelism, options, cancelAfter);
4142
}
4243
}

DeltaSight.SimpleBackgroundWorker/DeltaSight.SimpleBackgroundWorker.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Nullable>enable</Nullable>
66
<Title>DeltaSight.SimpleBackgroundWorker</Title>
77
<Authors>Bas Timmermans</Authors>
8-
<Version>2.2.0</Version>
8+
<Version>3.0.0</Version>
99
<Description>A very simple and light weight background worker queue that respects a maximum degree of parallelism.</Description>
1010
<Company>Deltasight</Company>
1111
<PackageProjectUrl>https://github.com/bt-88/Deltasight.SimpleBackgroundWorker</PackageProjectUrl>

DeltaSight.SimpleBackgroundWorker/SimpleBackgroundWorkerHost.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ private async Task Run(BackgroundWorkItem workItem, CancellationToken stoppingTo
5151

5252
var task = await Task.Factory.StartNew(
5353
() => workItem.Execute(cts.Token),
54-
workItem.IsLongRunning
55-
? TaskCreationOptions.None
56-
: TaskCreationOptions.LongRunning).ConfigureAwait(false);
54+
workItem.Options).ConfigureAwait(false);
5755

5856
await task;
5957

@@ -99,14 +97,14 @@ private async Task Run(BackgroundWorkItem workItem, CancellationToken stoppingTo
9997

10098
private async ValueTask WaitForWorker(BackgroundWorkItem item, CancellationToken cancellationToken)
10199
{
102-
if (item.IsLongRunning) return;
100+
if (item.IgnoreParallelism) return;
103101

104102
await _semaphore.WaitAsync(cancellationToken);
105103
}
106104

107105
private void ReleaseWorker(BackgroundWorkItem item)
108106
{
109-
if (item.IsLongRunning) return;
107+
if (item.IgnoreParallelism) return;
110108

111109
_semaphore.Release();
112110
}

0 commit comments

Comments
 (0)