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

Register job as scoped #32

Merged
merged 3 commits 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
5 changes: 4 additions & 1 deletion src/Runly/JobHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +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)
{
var action = host.Services.GetService<IHostAction>();
using var scope = host.Services.CreateAsyncScope();

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

return action?.RunAsync(cancellationToken) ?? Task.CompletedTask;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Runly/Processing/ExecutionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ async Task ExecuteParallelTasksAsync()
{
try
{
await ProcessScopeAsync(provider.CreateScope());
using var scope = provider.CreateAsyncScope();

await ProcessScopeAsync(scope);
}
catch (Exception ex) when (Job.Config.Execution.HandleExceptions)
{
Expand All @@ -275,7 +277,7 @@ async Task ExecuteParallelTasksAsync()
/// </summary>
/// <param name="scope">The <see cref="IServiceScope"/> containing a scoped <see cref="IServiceProvider"/> to get services from.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous execution of this method.</returns>
async Task ProcessScopeAsync(IServiceScope scope)
async Task ProcessScopeAsync(AsyncServiceScope scope)
{
bool @continue = true;
var stopwatch = new Stopwatch();
Expand Down
4 changes: 2 additions & 2 deletions src/Runly/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ static void AddRunAction(this IServiceCollection services, JobCache cache, Confi
));
}

services.AddSingleton<Execution>(s =>
services.AddScoped<Execution>(s =>
{
var cache = s.GetRequiredService<JobCache>();

Expand Down Expand Up @@ -369,7 +369,7 @@ public static IServiceCollection AddJob(this IServiceCollection services, JobCac
{
var info = cache.Get(config.Job.Type);

services.AddTransient(info.JobType);
services.AddScoped(info.JobType);

var type = config.GetType();

Expand Down
6 changes: 4 additions & 2 deletions test/Runly.Tests/Dependencies.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace Runly.Tests
{
public class Dep1 { }
public class Dep2 { }
public interface IDep1 { }
public class Dep1 : IDep1 { }
public interface IDep2 { }
public class Dep2 : IDep2 { }
public class Dep3 { }
public class Dep4 { }
public class Dep5 { }
Expand Down
34 changes: 32 additions & 2 deletions test/Runly.Tests/Jobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,22 @@ public override Task<Result> ProcessAsync(int item, Dep1 arg1)
}
}

public class Job2 : Job<Config, int, Dep1, Dep2>
public class Job1WithConstructorDep : Job<Config, int>
{
public Job1WithConstructorDep(IDep1 dep1) : base(new Config()) { }

public override IAsyncEnumerable<int> GetItemsAsync()
{
throw new NotImplementedException();
}

public override Task<Result> ProcessAsync(int item)
{
throw new NotImplementedException();
}
}

public class Job2 : Job<Config, int, Dep1, Dep2>
{
public Job2() : base(new Config()) { }

Expand All @@ -61,7 +76,22 @@ public override Task<Result> ProcessAsync(int item, Dep1 arg1, Dep2 arg2)
}
}

public class Job3 : Job<Config, int, Dep1, Dep2, Dep3>
public class Job2WithConstructorDep : Job<Config, int>
{
public Job2WithConstructorDep(IDep1 dep1, IDep2 dep2) : base(new Config()) { }

public override IAsyncEnumerable<int> GetItemsAsync()
{
throw new NotImplementedException();
}

public override Task<Result> ProcessAsync(int item)
{
throw new NotImplementedException();
}
}

public class Job3 : Job<Config, int, Dep1, Dep2, Dep3>
{
public Job3() : base(new Config()) { }

Expand Down
22 changes: 21 additions & 1 deletion test/Runly.Tests/Scenarios/Running/Running_a_job.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Runly.Hosting;
using Runly.Testing;
using System;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -28,5 +31,22 @@ public async Task should_run_a_single_item_job()
runner.Execution.IsComplete.Should().BeTrue();
runner.Execution.Disposition.Should().Be(Disposition.Successful);
}
}

[Fact]
public async Task should_run_a_job_with_scoped_dependency_in_constructor()
{
// CreateDefaultBuilder is more strict with Environment = Dev
Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", "Development");

var action = JobHost.CreateDefaultBuilder(["Job1WithConstructorDep"], typeof(UnitTest).Assembly)
.ConfigureServices((context, services) =>
{
services.AddScoped<IDep1>(s => new Dep1());
services.AddSingleton<IDep2>(s => new Dep2());
})
.Build();

await action.RunJobAsync();
}
}
}
Loading