Skip to content

Commit

Permalink
Add IBackgroundJobClientFactoryV2 and IRecurringJobManagerFactoryV2
Browse files Browse the repository at this point in the history
In order to make `IBackgroundJobClientV2` and `IRecurringJobManagerV2` injectable.
  • Loading branch information
0xced authored and odinserj committed Dec 15, 2022
1 parent baea57b commit d765faa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Hangfire.NetCore/DefaultClientManagerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Hangfire
{
internal sealed class DefaultClientManagerFactory : IBackgroundJobClientFactory, IRecurringJobManagerFactory
internal sealed class DefaultClientManagerFactory : IBackgroundJobClientFactoryV2, IRecurringJobManagerFactoryV2
{
private readonly IServiceProvider _serviceProvider;

Expand All @@ -29,7 +29,7 @@ public DefaultClientManagerFactory([NotNull] IServiceProvider serviceProvider)
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
}

public IBackgroundJobClient GetClient(JobStorage storage)
public IBackgroundJobClientV2 GetClientV2(JobStorage storage)
{
if (HangfireServiceCollectionExtensions.GetInternalServices(_serviceProvider, out var factory, out var stateChanger, out _))
{
Expand All @@ -41,7 +41,12 @@ public IBackgroundJobClient GetClient(JobStorage storage)
_serviceProvider.GetRequiredService<IJobFilterProvider>());
}

public IRecurringJobManager GetManager(JobStorage storage)
public IBackgroundJobClient GetClient(JobStorage storage)
{
return GetClientV2(storage);
}

public IRecurringJobManagerV2 GetManagerV2(JobStorage storage)
{
if (HangfireServiceCollectionExtensions.GetInternalServices(_serviceProvider, out var factory, out _, out _))
{
Expand All @@ -56,5 +61,10 @@ public IRecurringJobManager GetManager(JobStorage storage)
_serviceProvider.GetRequiredService<IJobFilterProvider>(),
_serviceProvider.GetRequiredService<ITimeZoneResolver>());
}

public IRecurringJobManager GetManager(JobStorage storage)
{
return GetManagerV2(storage);
}
}
}
6 changes: 6 additions & 0 deletions src/Hangfire.NetCore/HangfireServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ public static IServiceCollection AddHangfire(

services.TryAddSingleton(x => new DefaultClientManagerFactory(x));
services.TryAddSingletonChecked<IBackgroundJobClientFactory>(x => x.GetService<DefaultClientManagerFactory>());
services.TryAddSingletonChecked<IBackgroundJobClientFactoryV2>(x => x.GetService<DefaultClientManagerFactory>());
services.TryAddSingletonChecked<IRecurringJobManagerFactory>(x => x.GetService<DefaultClientManagerFactory>());
services.TryAddSingletonChecked<IRecurringJobManagerFactoryV2>(x => x.GetService<DefaultClientManagerFactory>());

services.TryAddSingletonChecked(x => x
.GetService<IBackgroundJobClientFactory>().GetClient(x.GetService<JobStorage>()));
services.TryAddSingletonChecked(x => x
.GetService<IBackgroundJobClientFactoryV2>().GetClientV2(x.GetService<JobStorage>()));

services.TryAddSingletonChecked(x => x
.GetService<IRecurringJobManagerFactory>().GetManager(x.GetService<JobStorage>()));
services.TryAddSingletonChecked(x => x
.GetService<IRecurringJobManagerFactoryV2>().GetManagerV2(x.GetService<JobStorage>()));

// IGlobalConfiguration serves as a marker indicating that Hangfire's services
// were added to the service container (checked by IApplicationBuilder extensions).
Expand Down
5 changes: 5 additions & 0 deletions src/Hangfire.NetCore/IBackgroundJobClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

namespace Hangfire
{
public interface IBackgroundJobClientFactoryV2 : IBackgroundJobClientFactory
{
IBackgroundJobClientV2 GetClientV2(JobStorage storage);
}

public interface IBackgroundJobClientFactory
{
IBackgroundJobClient GetClient(JobStorage storage);
Expand Down
5 changes: 5 additions & 0 deletions src/Hangfire.NetCore/IRecurringJobManagerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

namespace Hangfire
{
public interface IRecurringJobManagerFactoryV2 : IRecurringJobManagerFactory
{
IRecurringJobManagerV2 GetManagerV2(JobStorage storage);
}

public interface IRecurringJobManagerFactory
{
IRecurringJobManager GetManager(JobStorage storage);
Expand Down

0 comments on commit d765faa

Please sign in to comment.