Skip to content

Commit

Permalink
Don't dispose scope until job completes
Browse files Browse the repository at this point in the history
  • Loading branch information
WillSoss committed Feb 15, 2024
1 parent 2ae54d1 commit 5456795
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Diagnostics/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Diagnostics": {
"commandName": "Project",
"commandLineArgs": "diagnosticjob --milliseconddelayperitem 10 --numberofitems 5000 --throwexceptioningetitemidasync"
"commandLineArgs": "diagnosticjob --milliseconddelayperitem 10 --numberofitems 5000"
}
}
}
5 changes: 2 additions & 3 deletions src/Runly/JobHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ public static Task RunJobAsync(this IHost host)
/// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
public static Task RunJobAsync(this IHost host, CancellationToken cancellationToken)
{
using var scope = host.Services.CreateAsyncScope();

var scope = host.Services.CreateAsyncScope();
var action = scope.ServiceProvider.GetRequiredService<IHostAction>();

return action?.RunAsync(cancellationToken) ?? Task.CompletedTask;
return action?.RunAsync(cancellationToken).ContinueWith(async action => await scope.DisposeAsync()) ?? Task.CompletedTask;
}
}
}
23 changes: 20 additions & 3 deletions test/Runly.Tests/Dependencies.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
namespace Runly.Tests
using System;
using System.Threading.Tasks;

namespace Runly.Tests
{
public interface IDep1 { }
public class Dep1 : IDep1 { }
public interface IDep2 { }
public class Dep1 : IDep1, IAsyncDisposable
{
public static bool IsDisposed { get; set; }

public Dep1()
{
IsDisposed = false;
}

public ValueTask DisposeAsync()
{
IsDisposed = true;
return ValueTask.CompletedTask;
}
}
public interface IDep2 { }
public class Dep2 : IDep2 { }
public class Dep3 { }
public class Dep4 { }
Expand Down
10 changes: 9 additions & 1 deletion test/Runly.Tests/Scenarios/Running/Running_a_job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@ public async Task should_run_a_job_with_scoped_dependency_in_constructor()
})
.Build();

await action.RunJobAsync();
Dep1.IsDisposed.Should().BeFalse();

var run = action.RunJobAsync();

Dep1.IsDisposed.Should().BeFalse();

await run;

Dep1.IsDisposed.Should().BeTrue();
}
}
}

0 comments on commit 5456795

Please sign in to comment.