Skip to content

Commit

Permalink
Wrong scheduler factory was used, fixes #26
Browse files Browse the repository at this point in the history
Decrease scope tracking logging level.
  • Loading branch information
cd21h committed Jul 16, 2016
1 parent 384dc03 commit 02bca91
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 76 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Contributing#

##Dependencies upgrade##
When NUnit.ConsoleRunner is upgraded make sure build.proj/Test target has correct path to the nunit.console.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2.1.0-beta-{build}
version: 2.1.1.{build}
clone_depth: 3
clone_folder: c:\work\autofac.extras.quartz
pull_requests:
Expand Down
4 changes: 2 additions & 2 deletions build.proj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="14.0" DefaultTargets="BuildAll" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<BuildTarget Condition="'$(BuildTarget)' == ''">Rebuild</BuildTarget>
Expand Down Expand Up @@ -46,7 +46,7 @@

<Target Name="Test" DependsOnTargets="BuildAll">
<Exec WorkingDirectory="$(SrcDir)\Tests\bin\$(BuildConfig)"
Command="$(PkgDir)\Nunit.ConsoleRunner.3.2.0\tools\nunit3-console.exe Autofac.Extras.Quartz.Tests.dll /stoponerror --dispose-runners --noheader"
Command="$(PkgDir)\Nunit.ConsoleRunner.3.4.1\tools\nunit3-console.exe Autofac.Extras.Quartz.Tests.dll /stoponerror --dispose-runners --noheader"
/>
</Target>

Expand Down
2 changes: 2 additions & 0 deletions src/Autofac.Extras.Quartz.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantArgumentNameForLiteralExpression/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Full/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Full"&gt;&lt;AspOptimizeRegisterDirectives&gt;True&lt;/AspOptimizeRegisterDirectives&gt;&lt;HtmlReformatCode&gt;True&lt;/HtmlReformatCode&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_TO_IMPLICIT&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_EXCEPT_PRIMITIVE_TYPES&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_IMPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSharpFormatDocComments&gt;True&lt;/CSharpFormatDocComments&gt;&lt;CSReorderTypeMembers&gt;True&lt;/CSReorderTypeMembers&gt;&lt;VBOptimizeImports&gt;True&lt;/VBOptimizeImports&gt;&lt;VBShortenReferences&gt;True&lt;/VBShortenReferences&gt;&lt;VBReformatCode&gt;True&lt;/VBReformatCode&gt;&lt;VBFormatDocComments&gt;True&lt;/VBFormatDocComments&gt;&lt;JsInsertSemicolon&gt;True&lt;/JsInsertSemicolon&gt;&lt;JsReformatCode&gt;True&lt;/JsReformatCode&gt;&lt;JsFormatDocComments&gt;True&lt;/JsFormatDocComments&gt;&lt;CssAlphabetizeProperties&gt;True&lt;/CssAlphabetizeProperties&gt;&lt;CssReformatCode&gt;True&lt;/CssReformatCode&gt;&lt;XMLReformatCode&gt;True&lt;/XMLReformatCode&gt;&lt;CSUpdateFileHeader&gt;True&lt;/CSUpdateFileHeader&gt;&lt;/Profile&gt;</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ALIGN_MULTILINE_BINARY_EXPRESSIONS_CHAIN/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/CASE_BLOCK_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/INITIALIZER_BRACES/@EntryValue">END_OF_LINE</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderRegionName/@EntryValue">copyright</s:String>
Expand Down
4 changes: 2 additions & 2 deletions src/Autofac.Extras.Quartz/Autofac.Extras.Quartz.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
<HintPath>..\packages\Common.Logging.Core.3.3.1\lib\net40\Common.Logging.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="JetBrains.Annotations, Version=10.0.0.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.10.0.0\lib\net20\JetBrains.Annotations.dll</HintPath>
<Reference Include="JetBrains.Annotations, Version=10.1.5.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.10.1.5\lib\net\JetBrains.Annotations.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Quartz">
Expand Down
3 changes: 2 additions & 1 deletion src/Autofac.Extras.Quartz/Autofac.Extras.Quartz.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>
Optional dependencies support.
Decrease scope tracking log level to Trace.
Upgrade depdencies.
</releaseNotes>
<copyright>Copyright 2014-2016</copyright>
<tags>alphacloud quartz autofac</tags>
Expand Down
22 changes: 13 additions & 9 deletions src/Autofac.Extras.Quartz/AutofacJobFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Autofac Quartz integration
// https://github.com/alphacloud/Autofac.Extras.Quartz
// Licensed under MIT license.
// Copyright (c) 2014-2015 Alphacloud.Net
// Copyright (c) 2014-2016 Alphacloud.Net

#endregion

Expand Down Expand Up @@ -86,9 +86,12 @@ public void Dispose()
/// <returns>
/// the newly instantiated Job
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="bundle"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is <see langword="null" />.</exception>
/// <exception cref="SchedulerConfigException">Error resolving exception. Original exception will be stored in <see cref="Exception.InnerException"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="bundle" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException"><paramref name="scheduler" /> is <see langword="null" />.</exception>
/// <exception cref="SchedulerConfigException">
/// Error resolving exception. Original exception will be stored in
/// <see cref="Exception.InnerException" />.
/// </exception>
[NotNull]
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
Expand All @@ -106,9 +109,9 @@ public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
var jobTrackingInfo = new JobTrackingInfo(nestedScope);
RunningJobs[newJob] = jobTrackingInfo;

if (s_log.IsDebugEnabled)
if (s_log.IsTraceEnabled)
{
s_log.DebugFormat(CultureInfo.InvariantCulture, "Scope 0x{0:x} associated with Job 0x{1:x}",
s_log.TraceFormat(CultureInfo.InvariantCulture, "Scope 0x{0:x} associated with Job 0x{1:x}",
jobTrackingInfo.Scope.GetHashCode(), newJob.GetHashCode());
}

Expand Down Expand Up @@ -139,6 +142,7 @@ public void ReturnJob([CanBeNull] IJob job)
if (!RunningJobs.TryRemove(job, out trackingInfo))
{
s_log.WarnFormat("Tracking info for job 0x{0:x} not found", job.GetHashCode());
// ReSharper disable once SuspiciousTypeConversion.Global
var disposableJob = job as IDisposable;
disposableJob?.Dispose();
}
Expand All @@ -150,16 +154,16 @@ public void ReturnJob([CanBeNull] IJob job)

static void DisposeScope(IJob job, ILifetimeScope lifetimeScope)
{
if (s_log.IsDebugEnabled)
if (s_log.IsTraceEnabled)
{
s_log.DebugFormat("Disposing Scope 0x{0:x} for Job 0x{1:x}",
s_log.TraceFormat("Disposing Scope 0x{0:x} for Job 0x{1:x}",
lifetimeScope?.GetHashCode() ?? 0,
job?.GetHashCode() ?? 0);
}
lifetimeScope?.Dispose();
}

#region Job data
#region Job data

internal sealed class JobTrackingInfo
{
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac.Extras.Quartz/AutofacSchedulerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Autofac Quartz integration
// https://github.com/alphacloud/Autofac.Extras.Quartz
// Licensed under MIT license.
// Copyright (c) 2014-2015 Alphacloud.Net
// Copyright (c) 2014-2016 Alphacloud.Net

#endregion

Expand Down
13 changes: 7 additions & 6 deletions src/Autofac.Extras.Quartz/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// Autofac Quartz integration
// https://github.com/alphacloud/Autofac.Extras.Quartz
// Licensed under MIT license.
// Copyright (c) 2014-2015 Alphacloud.Net
// Copyright (c) 2014-2016 Alphacloud.Net

#endregion

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.

Expand All @@ -24,17 +24,18 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.

[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM

[assembly: Guid("b7307028-45b4-495c-8c38-8a60dbef8aca")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.1.1.0")]
[assembly: AssemblyFileVersion("2.1.1.0")]
[assembly: AssemblyInformationalVersion("2.1.1.0")]
[assembly: InternalsVisibleTo("Autofac.Extras.Quartz.Tests, PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001003b6b2a7202e9a5"
+ "c9b62e49f9f05bebbfd23fc6f08f0ec853d0857fc6cd3ca06d524c96651f82c6cddec44b9c0a20"
Expand Down
5 changes: 2 additions & 3 deletions src/Autofac.Extras.Quartz/QuartzAutofacFactoryModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Autofac Quartz integration
// https://github.com/alphacloud/Autofac.Extras.Quartz
// Licensed under MIT license.
// Copyright (c) 2014-2015 Alphacloud.Net
// Copyright (c) 2014-2016 Alphacloud.Net

#endregion

Expand Down Expand Up @@ -76,8 +76,7 @@ protected override void Load([NotNull] ContainerBuilder builder)
.As<IJobFactory>()
.SingleInstance();

builder.Register<ISchedulerFactory>(c =>
{
builder.Register<ISchedulerFactory>(c => {
var cfgProvider = ConfigurationProvider;

var autofacSchedulerFactory = cfgProvider != null
Expand Down
3 changes: 2 additions & 1 deletion src/Autofac.Extras.Quartz/QuartzAutofacJobsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Autofac.Extras.Quartz
{
using System;
using System.Reflection;
using Core;
using global::Quartz;
using JetBrains.Annotations;
using Module = Autofac.Module;
Expand Down Expand Up @@ -65,7 +66,7 @@ public QuartzAutofacJobsModule([NotNull] params Assembly[] assembliesToScan)
protected override void Load(ContainerBuilder builder)
{
var registrationBuilder = builder.RegisterAssemblyTypes(_assembliesToScan)
.Where(type => !type.IsAbstract && typeof (IJob).IsAssignableFrom(type))
.Where(type => !type.IsAbstract && typeof(IJob).IsAssignableFrom(type))
.AsSelf().InstancePerLifetimeScope();

if (AutoWireProperties)
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac.Extras.Quartz/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<package id="Autofac" version="3.5.2" targetFramework="net40" />
<package id="Common.Logging" version="3.3.1" targetFramework="net40" />
<package id="Common.Logging.Core" version="3.3.1" targetFramework="net40" />
<package id="JetBrains.Annotations" version="10.0.0" targetFramework="net40" developmentDependency="true" />
<package id="JetBrains.Annotations" version="10.1.5" targetFramework="net40" developmentDependency="true" />
<package id="Quartz" version="2.3.3" targetFramework="net40" />
</packages>
13 changes: 7 additions & 6 deletions src/Samples/SimpleService/App.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
Expand All @@ -8,7 +9,7 @@

</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<common>
<logging>
Expand All @@ -32,6 +33,10 @@
<level value="WARN" />
<foreColor value="Yellow, HighIntensity" />
</mapping>
<mapping>
<level value="TRACE" />
<foreColor value="Green" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
Expand All @@ -41,7 +46,7 @@
</layout>
</appender>
<root>
<level value="DEBUG" />
<level value="TRACE" />
<appender-ref ref="DebugAppender" />
</root>
</log4net>
Expand All @@ -56,10 +61,6 @@
<assemblyIdentity name="Topshelf" publicKeyToken="b800c4cfcdeea87b" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.3.154.0" newVersion="3.3.154.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Quartz" publicKeyToken="f6b8c98a402cc8a4" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.3.3.0" newVersion="2.3.3.0" />
Expand Down
46 changes: 26 additions & 20 deletions src/Samples/SimpleService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,54 @@
// Autofac Quartz integration
// https://github.com/alphacloud/Autofac.Extras.Quartz
// Licensed under MIT license.
// Copyright (c) 2015 Alphacloud.Net
// Copyright (c) 2014-2016 Alphacloud.Net

#endregion

namespace SimpleService
{
using System;
using System.Collections.Specialized;
using AppServices;
using Autofac;
using Autofac.Extras.Quartz;
using Common.Logging;
using Jobs;
using Quartz;
using Quartz.Spi;
using Topshelf;
using Topshelf.Autofac;
using Topshelf.Quartz;
using Topshelf.ServiceConfigurators;

internal static class Program
{
private static readonly ILog s_log = LogManager.GetLogger(typeof (Program));
private static IContainer _container;
static readonly ILog s_log = LogManager.GetLogger(typeof(Program));
static IContainer _container;

private static int Main(string[] args)
static int Main(string[] args)
{
Console.WriteLine("This sample demostrates how to integrate Quartz, TopShelf and Autofac.");
s_log.Info("Starting...");
try
{
_container = ConfigureContainer(new ContainerBuilder()).Build();

HostFactory.Run(conf =>
{
ScheduleJobServiceConfiguratorExtensions.SchedulerFactory = () => _container.Resolve<IScheduler>();

HostFactory.Run(conf => {
conf.SetServiceName("AutofacExtras.Quartz.Sample");
conf.SetDisplayName("Quartz.Net integration for autofac");
conf.SetDisplayName("Quartz.Net integration for Autofac");
conf.UseLog4Net();
conf.UseAutofacContainer(_container);

conf.Service<ServiceCore>(svc =>
{
conf.Service<ServiceCore>(svc => {
svc.ConstructUsingAutofacContainer();
svc.WhenStarted(o => o.Start());
svc.WhenStopped(o =>
{
svc.WhenStopped(o => {
o.Stop();
_container.Dispose();
});
ConfigureBackgroundJobs(svc);
ConfigureScheduler(svc);
});
});

Expand All @@ -68,12 +67,10 @@ private static int Main(string[] args)
}
}

private static void ConfigureBackgroundJobs(ServiceConfigurator<ServiceCore> svc)
static void ConfigureScheduler(ServiceConfigurator<ServiceCore> svc)
{
svc.UsingQuartzJobFactory(() => _container.Resolve<IJobFactory>());

svc.ScheduleQuartzJob(q =>
{
svc.ScheduleQuartzJob(q => {
s_log.Trace("Configuring jobs");
q.WithJob(JobBuilder.Create<HeartbeatJob>()
.WithIdentity("Heartbeat", "Maintenance")
.Build);
Expand All @@ -84,8 +81,17 @@ private static void ConfigureBackgroundJobs(ServiceConfigurator<ServiceCore> svc

internal static ContainerBuilder ConfigureContainer(ContainerBuilder cb)
{
cb.RegisterModule(new QuartzAutofacFactoryModule());
cb.RegisterModule(new QuartzAutofacJobsModule(typeof (HeartbeatJob).Assembly));
// configure and register Quartz
var schedulerConfig = new NameValueCollection {
{"quartz.threadPool.threadCount", "3"},
{"quartz.threadPool.threadNamePrefix", "SchedulerWorker"},
{"quartz.scheduler.threadName", "Scheduler"}
};

cb.RegisterModule(new QuartzAutofacFactoryModule {
ConfigurationProvider = c => schedulerConfig
});
cb.RegisterModule(new QuartzAutofacJobsModule(typeof(HeartbeatJob).Assembly));

RegisterComponents(cb);
return cb;
Expand Down
Loading

0 comments on commit 02bca91

Please sign in to comment.