Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't dispose scope until job completes #33

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
Loading