Skip to content

Commit d3291aa

Browse files
author
Joel Mitchell
committed
Added async recurring task support
1 parent d3843fc commit d3291aa

File tree

5 files changed

+107
-20
lines changed

5 files changed

+107
-20
lines changed

src/Cofoundry.Plugins.BackgroundTasks.Hangfire/Cofoundry.Plugins.BackgroundTasks.Hangfire.csproj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@
4545
<Private>True</Private>
4646
</Reference>
4747
<Reference Include="Cofoundry.Core, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
48-
<HintPath>..\packages\Cofoundry.Core.0.1.0-beta0005\lib\net461\Cofoundry.Core.dll</HintPath>
48+
<HintPath>..\packages\Cofoundry.Core.0.1.0-beta0022\lib\net461\Cofoundry.Core.dll</HintPath>
4949
<Private>True</Private>
5050
</Reference>
5151
<Reference Include="Cofoundry.Domain, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
52-
<HintPath>..\packages\Cofoundry.Domain.0.1.0-beta0005\lib\net461\Cofoundry.Domain.dll</HintPath>
52+
<HintPath>..\packages\Cofoundry.Domain.0.1.0-beta0022\lib\net461\Cofoundry.Domain.dll</HintPath>
5353
<Private>True</Private>
5454
</Reference>
5555
<Reference Include="Cofoundry.Web, Version=0.1.0.0, Culture=neutral, processorArchitecture=MSIL">
56-
<HintPath>..\packages\Cofoundry.Web.0.1.0-beta0005\lib\net461\Cofoundry.Web.dll</HintPath>
56+
<HintPath>..\packages\Cofoundry.Web.0.1.0-beta0022\lib\net461\Cofoundry.Web.dll</HintPath>
5757
<Private>True</Private>
5858
</Reference>
5959
<Reference Include="Conditions, Version=2.1.0.24, Culture=neutral, processorArchitecture=MSIL">
@@ -68,12 +68,12 @@
6868
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
6969
<Private>True</Private>
7070
</Reference>
71-
<Reference Include="Hangfire.Core, Version=1.6.4.0, Culture=neutral, processorArchitecture=MSIL">
72-
<HintPath>..\packages\Hangfire.Core.1.6.4\lib\net45\Hangfire.Core.dll</HintPath>
71+
<Reference Include="Hangfire.Core, Version=1.6.5.0, Culture=neutral, processorArchitecture=MSIL">
72+
<HintPath>..\packages\Hangfire.Core.1.6.5\lib\net45\Hangfire.Core.dll</HintPath>
7373
<Private>True</Private>
7474
</Reference>
75-
<Reference Include="Hangfire.SqlServer, Version=1.6.4.0, Culture=neutral, processorArchitecture=MSIL">
76-
<HintPath>..\packages\Hangfire.SqlServer.1.6.4\lib\net45\Hangfire.SqlServer.dll</HintPath>
75+
<Reference Include="Hangfire.SqlServer, Version=1.6.5.0, Culture=neutral, processorArchitecture=MSIL">
76+
<HintPath>..\packages\Hangfire.SqlServer.1.6.5\lib\net45\Hangfire.SqlServer.dll</HintPath>
7777
<Private>True</Private>
7878
</Reference>
7979
<Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
@@ -189,6 +189,7 @@
189189
<Compile Include="Bootstrap\HangfireBackgroundTasksDependencyRegistration.cs" />
190190
<Compile Include="Dashboard\HangfireDashboardAuthorizationFilter.cs" />
191191
<Compile Include="Framework\ContainerJobActivator.cs" />
192+
<Compile Include="Framework\InjectableAsyncTaskWrapper.cs" />
192193
<Compile Include="Startup\IHangfireBackgroundTaskInitializer.cs" />
193194
<Compile Include="Framework\HangfireBackgroundTaskScheduler.cs" />
194195
<Compile Include="Startup\HangfireBackgroundTaskInitializer.cs" />

src/Cofoundry.Plugins.BackgroundTasks.Hangfire/Cofoundry.Plugins.BackgroundTasks.Hangfire.nuspec

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
<dependencies>
1616
<dependency id="Antlr" version="3.5.0.2" />
1717
<dependency id="AutoMapper" version="5.1.1" />
18-
<dependency id="Cofoundry.Core" version="0.1.0-beta0005" />
19-
<dependency id="Cofoundry.Domain" version="0.1.0-beta0005" />
20-
<dependency id="Cofoundry.Web" version="0.1.0-beta0005" />
18+
<dependency id="Cofoundry.Core" version="0.1.0-beta0022" />
19+
<dependency id="Cofoundry.Domain" version="0.1.0-beta0022" />
20+
<dependency id="Cofoundry.Web" version="0.1.0-beta0022" />
2121
<dependency id="CommonServiceLocator" version="1.3" />
2222
<dependency id="Conditions" version="2.1.0" />
2323
<dependency id="EntityFramework" version="6.1.3" />
24-
<dependency id="Hangfire" version="1.6.4" />
25-
<dependency id="Hangfire.Core" version="1.6.4" />
26-
<dependency id="Hangfire.SqlServer" version="1.6.4" />
24+
<dependency id="Hangfire" version="1.6.5" />
25+
<dependency id="Hangfire.Core" version="1.6.5" />
26+
<dependency id="Hangfire.SqlServer" version="1.6.5" />
2727
<dependency id="HtmlAgilityPack" version="1.4.9.5" />
2828
<dependency id="Microsoft.AspNet.Mvc" version="5.2.3" />
2929
<dependency id="Microsoft.AspNet.OData" version="5.9.1" />

src/Cofoundry.Plugins.BackgroundTasks.Hangfire/Framework/HangfireBackgroundTaskScheduler.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int hours = 0, int
2626
RegisterRecurringTask<TTask>(cronExpression);
2727

2828
return this;
29-
3029
}
3130

3231
public IBackgroundTaskScheduler RegisterRecurringTask<TTask>(int minutes) where TTask : IRecurringBackgroundTask
@@ -44,6 +43,37 @@ public IBackgroundTaskScheduler DeregisterRecurringTask<TTask>() where TTask : I
4443
return this;
4544
}
4645

46+
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int days, int atHour = 0, int atMinute = 0) where TTask : IAsyncRecurringBackgroundTask
47+
{
48+
string cronExpression = string.Format("{2} {1} */{0} * *", days, atHour, atMinute);
49+
RegisterAsyncRecurringTask<TTask>(cronExpression);
50+
51+
return this;
52+
}
53+
54+
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int hours = 0, int minute = 0) where TTask : IAsyncRecurringBackgroundTask
55+
{
56+
string cronExpression = string.Format("{1} */{0} * * *", hours, minute);
57+
RegisterAsyncRecurringTask<TTask>(cronExpression);
58+
59+
return this;
60+
}
61+
62+
public IBackgroundTaskScheduler RegisterAsyncRecurringTask<TTask>(int minutes) where TTask : IAsyncRecurringBackgroundTask
63+
{
64+
string cronExpression = string.Format("*/{0} * * * *", minutes);
65+
RegisterAsyncRecurringTask<TTask>(cronExpression);
66+
67+
return this;
68+
}
69+
70+
public IBackgroundTaskScheduler DeregisterAsyncRecurringTask<TTask>() where TTask : IAsyncRecurringBackgroundTask
71+
{
72+
RecurringJob.RemoveIfExists(GetJobId<TTask>());
73+
74+
return this;
75+
}
76+
4777
#endregion
4878

4979
#region private methods
@@ -58,6 +88,11 @@ private void RegisterRecurringTask<TTask>(string cronExpression) where TTask : I
5888
RecurringJob.AddOrUpdate<InjectableTaskWrapper<TTask>>(GetJobId<TTask>(), t => t.Execute(), cronExpression);
5989
}
6090

91+
private void RegisterAsyncRecurringTask<TTask>(string cronExpression) where TTask : IAsyncRecurringBackgroundTask
92+
{
93+
RecurringJob.AddOrUpdate<InjectableAsyncTaskWrapper<TTask>>(GetJobId<TTask>(), t => t.ExecuteAsync(), cronExpression);
94+
}
95+
6196
#endregion
6297
}
6398
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+

2+
using Hangfire;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Cofoundry.Core.BackgroundTasks;
8+
using Cofoundry.Core.DependencyInjection;
9+
using Cofoundry.Core.ErrorLogging;
10+
11+
namespace Cofoundry.Plugins.BackgroundTasks.Hangfire
12+
{
13+
/// <summary>
14+
/// Used to wrap task execution in a dispoable DI child context since
15+
/// hangfire does not yet support child contexts.
16+
/// </summary>
17+
/// <typeparam name="TTask">Type fo task to execute</typeparam>
18+
public class InjectableAsyncTaskWrapper<TTask> where TTask : IAsyncBackgroundTask
19+
{
20+
private readonly IResolutionContext _resolutionContext;
21+
22+
public InjectableAsyncTaskWrapper(
23+
IResolutionContext resolutionContext
24+
)
25+
{
26+
_resolutionContext = resolutionContext;
27+
}
28+
29+
[AutomaticRetry(Attempts = 0)]
30+
public async Task ExecuteAsync()
31+
{
32+
using (var childContext = _resolutionContext.CreateChildContext())
33+
{
34+
try
35+
{
36+
var task = childContext.Resolve<TTask>();
37+
await task.ExecuteAsync();
38+
}
39+
catch (Exception ex)
40+
{
41+
// I couldn't get hangfire error logging to work so I'm just catching any exceptions here
42+
// and sending them to the error logger.
43+
var errorLoggingService = childContext.Resolve<IErrorLoggingService>();
44+
errorLoggingService.Log(ex);
45+
46+
throw;
47+
}
48+
}
49+
}
50+
}
51+
}

src/Cofoundry.Plugins.BackgroundTasks.Hangfire/packages.config

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
<packages>
33
<package id="Antlr" version="3.5.0.2" targetFramework="net461" />
44
<package id="AutoMapper" version="5.1.1" targetFramework="net461" />
5-
<package id="Cofoundry.Core" version="0.1.0-beta0005" targetFramework="net461" />
6-
<package id="Cofoundry.Domain" version="0.1.0-beta0005" targetFramework="net461" />
7-
<package id="Cofoundry.Web" version="0.1.0-beta0005" targetFramework="net461" />
5+
<package id="Cofoundry.Core" version="0.1.0-beta0022" targetFramework="net461" />
6+
<package id="Cofoundry.Domain" version="0.1.0-beta0022" targetFramework="net461" />
7+
<package id="Cofoundry.Web" version="0.1.0-beta0022" targetFramework="net461" />
88
<package id="CommonServiceLocator" version="1.3" targetFramework="net461" />
99
<package id="Conditions" version="2.1.0" targetFramework="net461" />
1010
<package id="EntityFramework" version="6.1.3" targetFramework="net461" />
11-
<package id="Hangfire" version="1.6.4" targetFramework="net461" />
12-
<package id="Hangfire.Core" version="1.6.4" targetFramework="net461" />
13-
<package id="Hangfire.SqlServer" version="1.6.4" targetFramework="net461" />
11+
<package id="Hangfire" version="1.6.5" targetFramework="net461" />
12+
<package id="Hangfire.Core" version="1.6.5" targetFramework="net461" />
13+
<package id="Hangfire.SqlServer" version="1.6.5" targetFramework="net461" />
1414
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net461" />
1515
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
1616
<package id="Microsoft.AspNet.OData" version="5.9.1" targetFramework="net461" />

0 commit comments

Comments
 (0)