Skip to content

Commit

Permalink
Use Autofac lifetime scope in server and client initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
hhalim committed Aug 1, 2017
1 parent 2b1ec66 commit 6b89b0a
Show file tree
Hide file tree
Showing 21 changed files with 130 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Shift.Cache.Redis/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.9")]
[assembly: AssemblyVersion("1.0.8.0")]
4 changes: 2 additions & 2 deletions Shift.Cache.Redis/Shift.Cache.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="StackExchange.Redis, Version=1.2.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\StackExchange.Redis.1.2.4\lib\net46\StackExchange.Redis.dll</HintPath>
<Reference Include="StackExchange.Redis, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\StackExchange.Redis.1.2.6\lib\net46\StackExchange.Redis.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Shift.Cache.Redis/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
<package id="StackExchange.Redis" version="1.2.4" targetFramework="net461" />
<package id="StackExchange.Redis" version="1.2.6" targetFramework="net461" />
</packages>
2 changes: 1 addition & 1 deletion Shift.DataLayer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.9")]
[assembly: AssemblyVersion("1.0.8.0")]
4 changes: 2 additions & 2 deletions Shift.DataLayer/Shift.DataLayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="StackExchange.Redis, Version=1.2.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\StackExchange.Redis.1.2.4\lib\net46\StackExchange.Redis.dll</HintPath>
<Reference Include="StackExchange.Redis, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\StackExchange.Redis.1.2.6\lib\net46\StackExchange.Redis.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Shift.DataLayer/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<package id="MongoDB.Driver" version="2.4.4" targetFramework="net461" />
<package id="MongoDB.Driver.Core" version="2.4.4" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
<package id="StackExchange.Redis" version="1.2.4" targetFramework="net461" />
<package id="StackExchange.Redis" version="1.2.6" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
</packages>
2 changes: 1 addition & 1 deletion Shift.Entities/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.9")]
[assembly: AssemblyVersion("1.0.8.0")]
2 changes: 1 addition & 1 deletion Shift.UnitTest.DataLayer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.9")]
[assembly: AssemblyVersion("1.0.8.0")]
4 changes: 2 additions & 2 deletions Shift.UnitTest.DataLayer/Shift.UnitTest.DataLayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
<HintPath>..\packages\Castle.Core.4.1.1\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Moq, Version=4.7.63.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.7.63\lib\net45\Moq.dll</HintPath>
<Reference Include="Moq, Version=4.7.99.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.7.99\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Shift.UnitTest.DataLayer/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="4.1.1" targetFramework="net461" />
<package id="Moq" version="4.7.63" targetFramework="net461" />
<package id="Moq" version="4.7.99" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
<package id="xunit" version="2.2.0" targetFramework="net461" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
Expand Down
41 changes: 41 additions & 0 deletions Shift.UnitTest/JobClientRedisAsyncTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using Xunit;
using Shift.Entities;
using Moq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Shift.UnitTest
{
public class JobClientRedisAsyncTest
{
JobClient jobClient;
private const string AppID = "TestAppID";

public JobClientRedisAsyncTest()
{
var appSettingsReader = new AppSettingsReader();

//Configure storage connection
var clientConfig = new ClientConfig();
clientConfig.DBConnectionString = appSettingsReader.GetValue("RedisConnectionString", typeof(string)) as string;
clientConfig.StorageMode = "redis";
jobClient = new JobClient(clientConfig);
}

[Fact]
public async Task GetJob_Valid()
{
var jobID = await jobClient.AddAsync(AppID, () => Console.WriteLine("Hello Test"));
var job = await jobClient.GetJobAsync(jobID);

await jobClient.DeleteJobsAsync(new List<string>() { jobID });

Assert.NotNull(job);
Assert.Equal(jobID, job.JobID);
}
}
}
41 changes: 41 additions & 0 deletions Shift.UnitTest/JobClientRedisTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using Xunit;
using Shift.Entities;
using Moq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Shift.UnitTest
{
public class JobClientRedisTest
{
JobClient jobClient;
private const string AppID = "TestAppID";

public JobClientRedisTest()
{
var appSettingsReader = new AppSettingsReader();

//Configure storage connection
var clientConfig = new ClientConfig();
clientConfig.DBConnectionString = appSettingsReader.GetValue("RedisConnectionString", typeof(string)) as string;
clientConfig.StorageMode = "redis";
jobClient = new JobClient(clientConfig);
}

[Fact]
public void GetJob_Valid()
{
var jobID = jobClient.Add(AppID, () => Console.WriteLine("Hello Test"));
var job = jobClient.GetJob(jobID);

jobClient.DeleteJobs(new List<string>() { jobID });

Assert.NotNull(job);
Assert.Equal(jobID, job.JobID);
}
}
}
2 changes: 1 addition & 1 deletion Shift.UnitTest/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.9")]
[assembly: AssemblyVersion("1.0.8.0")]
10 changes: 6 additions & 4 deletions Shift.UnitTest/Shift.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=4.6.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.4.6.0\lib\net45\Autofac.dll</HintPath>
<Reference Include="Autofac, Version=4.6.1.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.4.6.1\lib\net45\Autofac.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Castle.Core, Version=4.1.1.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
Expand All @@ -49,8 +49,8 @@
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Moq, Version=4.7.63.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.7.63\lib\net45\Moq.dll</HintPath>
<Reference Include="Moq, Version=4.7.99.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.7.99\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down Expand Up @@ -86,6 +86,8 @@
<ItemGroup>
<Compile Include="AssemblyHelpersTest.cs" />
<Compile Include="JobClientAsyncTest.cs" />
<Compile Include="JobClientRedisAsyncTest.cs" />
<Compile Include="JobClientRedisTest.cs" />
<Compile Include="JobClientTest.cs" />
<Compile Include="JobServerRedisAsyncTest.cs" />
<Compile Include="PIDGeneratorTest.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Shift.UnitTest/packages.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="4.6.0" targetFramework="net461" />
<package id="Autofac" version="4.6.1" targetFramework="net461" />
<package id="Castle.Core" version="4.1.1" targetFramework="net461" />
<package id="Moq" version="4.7.63" targetFramework="net461" />
<package id="Moq" version="4.7.99" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
<package id="xunit" version="2.2.0" targetFramework="net461" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
Expand Down
13 changes: 7 additions & 6 deletions Shift/JobClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace Shift
public class JobClient
{
private IJobDAL jobDAL = null;
private readonly ContainerBuilder builder;
private readonly IContainer container;

///<summary>
/// Initializes a new instance of JobClient class, injects data layer with connection and configuration strings.
Expand Down Expand Up @@ -53,12 +51,15 @@ public JobClient(ClientConfig config)
throw new ArgumentNullException("Unable to run without Cache configuration string.");
}

builder = new ContainerBuilder();
var builder = new ContainerBuilder();
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
RegisterAssembly.RegisterTypes(builder, config.StorageMode, config.DBConnectionString, config.UseCache, config.CacheConfigurationString, config.EncryptionKey, config.DBAuthKey);
container = builder.Build();

jobDAL = container.Resolve<IJobDAL>();
var container = builder.Build();
//Use lifetime scope to avoid memory leak http://docs.autofac.org/en/latest/resolve/
using (var scope = container.BeginLifetimeScope())
{
jobDAL = container.Resolve<IJobDAL>();
}
}

public JobClient(IJobDAL jobDAL)
Expand Down
35 changes: 13 additions & 22 deletions Shift/JobServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

using Autofac;
using Autofac.Features.ResolveAnything;
using Shift.Entities;

namespace Shift
{
public class JobServer
{
private ServerConfig config = null;
private readonly ContainerBuilder builder;
private readonly IContainer container;
private static System.Timers.Timer timer = null;
private static System.Timers.Timer timer2 = null;

Expand Down Expand Up @@ -61,37 +60,29 @@ public JobServer(ServerConfig config)

this.config = config;

builder = new ContainerBuilder();
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
RegisterAssembly.RegisterTypes(builder, config.StorageMode, config.DBConnectionString, config.UseCache, config.CacheConfigurationString, config.EncryptionKey, config.DBAuthKey);
container = builder.Build();

Initialize();
}

#region Startup
/// <summary>
/// Instantiate the data layer and loads all the referenced assemblies defined in the assembly list text file
/// in Options.AssemblyListPath and Options.AssemblyBaseDir
/// </summary>
private void Initialize()
{
this.workerList = new List<Worker>();

//OPTIONAL: Load all EXTERNAL DLLs needed by this process
AppDomain.CurrentDomain.AssemblyResolve += AssemblyHelpers.OnAssemblyResolve;
AssemblyHelpers.LoadAssemblies(config.AssemblyFolder, config.AssemblyListPath);

//Create Worker
for(var i=1; i <= config.Workers; i++)
var builder = new ContainerBuilder();
builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
RegisterAssembly.RegisterTypes(builder, config.StorageMode, config.DBConnectionString, config.UseCache, config.CacheConfigurationString, config.EncryptionKey, config.DBAuthKey);
var container = builder.Build();
//Use lifetime scope to avoid memory leak http://docs.autofac.org/en/latest/resolve/
using (var scope = container.BeginLifetimeScope())
{
var worker = new Worker(config, container, i);
workerList.Add(worker);
var jobDAL = scope.Resolve<IJobDAL>();
for (var i = 1; i <= config.Workers; i++)
{
var worker = new Worker(config, jobDAL, i);
workerList.Add(worker);
}
}
}

#endregion

#region Server Run and Manage jobs

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Shift/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.7.9")]
[assembly: AssemblyVersion("1.0.8.0")]
4 changes: 2 additions & 2 deletions Shift/Shift.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<DocumentationFile>bin\Release\Shift.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=4.6.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.4.6.0\lib\net45\Autofac.dll</HintPath>
<Reference Include="Autofac, Version=4.6.1.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.4.6.1\lib\net45\Autofac.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapper, Version=1.50.2.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
4 changes: 2 additions & 2 deletions Shift/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class Worker
private int? autoDeletePeriod;
private IList<JobStatus?> autoDeleteStatus;

public Worker(ServerConfig config, IContainer container, int workerID)
public Worker(ServerConfig config, IJobDAL jobDAL, int workerID)
{
taskList = new Dictionary<string, TaskInfo>();
jobDAL = container.Resolve<IJobDAL>();
this.jobDAL = jobDAL;
this.workerID = workerID;

this.workerProcessID = config.ProcessID + "-" + workerID;
Expand Down
2 changes: 1 addition & 1 deletion Shift/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="4.6.0" targetFramework="net461" />
<package id="Autofac" version="4.6.1" targetFramework="net461" />
<package id="Dapper" version="1.50.2" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
</packages>

0 comments on commit 6b89b0a

Please sign in to comment.