-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from maitlandmarshall/chore/no-startup_backgrou…
…ndJobContext-refactor Chore/no startup background job context refactor and unit tests
- Loading branch information
Showing
20 changed files
with
292 additions
and
135 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
MAD.Integration.Common.EFCore/MAD.Integration.Common.EFCore.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.7" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.7" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.7" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
20 changes: 20 additions & 0 deletions
20
MAD.Integration.Common.EFCore/UseDesignDefaultsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace MAD.Integration.Common.EFCore | ||
{ | ||
public static class UseDesignDefaultsExtensions | ||
{ | ||
public static DbContextOptionsBuilder UseDesignDefaults(this DbContextOptionsBuilder dbContextOptionsBuilder) | ||
{ | ||
var json = JObject.Parse(File.ReadAllText("settings.default.json")); | ||
dbContextOptionsBuilder.UseSqlServer(json["connectionString"].ToString()); | ||
|
||
return dbContextOptionsBuilder; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Hangfire; | ||
using Hangfire.MemoryStorage; | ||
using MAD.Integration.Common.Jobs; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace MAD.Integration.Common.Tests | ||
{ | ||
[TestClass] | ||
public class BackgroundJobContextTests | ||
{ | ||
private static readonly TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>(); | ||
public static async Task BackgroundJobContext_Job() | ||
{ | ||
try | ||
{ | ||
await Task.Delay(5); | ||
var bg = BackgroundJobContext.Current; | ||
|
||
taskCompletionSource.SetResult(bg != null); | ||
} | ||
catch (Exception) | ||
{ | ||
taskCompletionSource.SetResult(false); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task CurrentJob_InAsyncExecutingJob_NotNull() | ||
{ | ||
var host = IntegrationHost.CreateDefaultBuilder() | ||
.UseHangfire(cfg => cfg.UseMemoryStorage()) | ||
.Build(); | ||
|
||
BackgroundJob.Enqueue(() => BackgroundJobContext_Job()); | ||
|
||
_ = host.RunAsync(); | ||
|
||
var result = await taskCompletionSource.Task; | ||
Assert.IsTrue(result); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
MAD.Integration.Common.Tests/MAD.Integration.Common.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Hangfire.MemoryStorage" Version="1.7.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MAD.Integration.Common\MAD.Integration.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
MAD.Integration.Common/Jobs/AutofacLifecycleJobActivator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Autofac; | ||
using Hangfire; | ||
using Hangfire.Annotations; | ||
using System; | ||
|
||
namespace MAD.Integration.Common.Jobs | ||
{ | ||
public class AutofacLifecycleJobActivator : JobActivator | ||
{ | ||
public const string LifetimeScopeTag = "BackgroundJobScope"; | ||
|
||
private readonly ILifetimeScope lifetimeScope; | ||
|
||
public AutofacLifecycleJobActivator([NotNull] ILifetimeScope lifetimeScope) | ||
{ | ||
this.lifetimeScope = lifetimeScope; | ||
} | ||
|
||
public override object ActivateJob(Type jobType) | ||
{ | ||
var jobInstance = this.lifetimeScope.Resolve(jobType); | ||
|
||
if (jobInstance is IJobActivated jobInitialize) | ||
{ | ||
jobInitialize.Activated(); | ||
} | ||
|
||
return jobInstance; | ||
} | ||
|
||
public override JobActivatorScope BeginScope(JobActivatorContext context) | ||
{ | ||
return new AutofacScope(this.lifetimeScope.BeginLifetimeScope(LifetimeScopeTag)); | ||
} | ||
|
||
private class AutofacScope : JobActivatorScope | ||
{ | ||
private readonly ILifetimeScope lifetimeScope; | ||
|
||
public AutofacScope(ILifetimeScope lifetimeScope) | ||
{ | ||
this.lifetimeScope = lifetimeScope; | ||
} | ||
|
||
public override object Resolve(Type type) | ||
{ | ||
var jobInstance = this.lifetimeScope.Resolve(type); | ||
|
||
if (jobInstance is IJobActivated jobInitialize) | ||
{ | ||
jobInitialize.Activated(); | ||
} | ||
|
||
return jobInstance; | ||
} | ||
|
||
public override void DisposeScope() | ||
{ | ||
this.lifetimeScope.Dispose(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Hangfire; | ||
using Hangfire.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace MAD.Integration.Common.Jobs | ||
{ | ||
public static class BackgroundJobExtensions | ||
{ | ||
public static BackgroundJob SetJobParameter(this BackgroundJob job, string name, object value) | ||
{ | ||
if (String.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name)); | ||
|
||
JobStorage.Current.GetConnection().SetJobParameter(job.Id, name, SerializationHelper.Serialize(value, SerializationOption.User)); | ||
return job; | ||
} | ||
|
||
public static T GetJobParameter<T>(this BackgroundJob job, string name) | ||
{ | ||
if (String.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name)); | ||
|
||
try | ||
{ | ||
return SerializationHelper.Deserialize<T>(JobStorage.Current.GetConnection().GetJobParameter(job.Id, name), SerializationOption.User); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new InvalidOperationException( | ||
$"Could not get a value of the job parameter `{name}`. See inner exception for details.", ex); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.