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

Commit

Permalink
Replaces 'IsLongRunning' by a 'IgnoreParallelism' and a 'Options' pro…
Browse files Browse the repository at this point in the history
…perty
  • Loading branch information
bt-88 committed Feb 1, 2023
1 parent 757f0a7 commit 005ece1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
8 changes: 4 additions & 4 deletions DeltaSight.SimpleBackgroundWorker.Tests/ParallelismTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public async Task Test_WithManyDegreesOfParallelism()
await Task.Delay(jobTimeMs, t);
ended.Add(DateTime.Now);
semaphore.Release();
}, $"Job {i}", e =>
}, name: $"Job {i}", errorCallback: ex =>
{
Assert.Fail(e.Message);
Assert.Fail(ex.Message);

return Task.CompletedTask;
}))
Expand All @@ -49,7 +49,7 @@ public async Task Test_WithManyDegreesOfParallelism()
await bgWorker.QueueAsync(jobs);

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

var startAt = DateTime.Now;

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

await host.StopAsync();
Expand Down
25 changes: 13 additions & 12 deletions DeltaSight.SimpleBackgroundWorker/BackgroundWorkItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ namespace DeltaSight.SimpleBackgroundWorker;

public struct BackgroundWorkItem
{
public BackgroundWorkItem(Func<CancellationToken, Task> execute, string name = "Untitled", Func<Exception, Task>? onError = null, bool isLongRunning = false, TimeSpan? cancelAfter = null)
private BackgroundWorkItem(Func<CancellationToken, Task> execute, string name, Func<Exception, Task>? onError, bool ignoreParallelism, TaskCreationOptions options, TimeSpan? cancelAfter)
{
Name = name;
Execute = execute;
OnError = onError;
IsLongRunning = isLongRunning;
Options = options;
CancelAfter = cancelAfter;
IgnoreParallelism = ignoreParallelism;
}

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

/// <summary>
/// Flag to indicate the task is long running
/// <remarks>Long running tasks will not impact maximum degree of parallelism
/// </summary>
public bool IsLongRunning { get; }
public TaskCreationOptions Options { get; }

public bool IgnoreParallelism { get; }

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

public static BackgroundWorkItem Create(Func<CancellationToken, Task> executeWork,
[CallerArgumentExpression("executeWork")] string name = "Untitled",
string? name = null,
Func<Exception, Task>? errorCallback = null,
bool isLongRunning = false,
TimeSpan? cancelAfter = null
)
bool ignoreParallelism = false,
TaskCreationOptions options = TaskCreationOptions.None,
TimeSpan? cancelAfter = null,
[CallerMemberName] string callerMemberName = "",
[CallerArgumentExpression("executeWork")] string callerArgumentExpression = "")
{
return new BackgroundWorkItem(executeWork, name, errorCallback, isLongRunning, cancelAfter);
return new BackgroundWorkItem(executeWork, name ?? $"{callerMemberName}::{callerArgumentExpression}", errorCallback, ignoreParallelism, options, cancelAfter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<Title>DeltaSight.SimpleBackgroundWorker</Title>
<Authors>Bas Timmermans</Authors>
<Version>2.2.0</Version>
<Version>3.0.0</Version>
<Description>A very simple and light weight background worker queue that respects a maximum degree of parallelism.</Description>
<Company>Deltasight</Company>
<PackageProjectUrl>https://github.com/bt-88/Deltasight.SimpleBackgroundWorker</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ private async Task Run(BackgroundWorkItem workItem, CancellationToken stoppingTo

var task = await Task.Factory.StartNew(
() => workItem.Execute(cts.Token),
workItem.IsLongRunning
? TaskCreationOptions.None
: TaskCreationOptions.LongRunning).ConfigureAwait(false);
workItem.Options).ConfigureAwait(false);

await task;

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

private async ValueTask WaitForWorker(BackgroundWorkItem item, CancellationToken cancellationToken)
{
if (item.IsLongRunning) return;
if (item.IgnoreParallelism) return;

await _semaphore.WaitAsync(cancellationToken);
}

private void ReleaseWorker(BackgroundWorkItem item)
{
if (item.IsLongRunning) return;
if (item.IgnoreParallelism) return;

_semaphore.Release();
}
Expand Down

0 comments on commit 005ece1

Please sign in to comment.