Skip to content

Commit ade3b3e

Browse files
Nongzhshthiennn
authored andcommitted
Implemented real-time notification with signalR and Hangfire. (simplcommerce#675)
* Refactor user settings. * Add hangfirejobs module * Implemented real-time notification with signalR. * Allow background jobs queued at a giving time * Add Delete-BIN-OBJ-Folders.bat
1 parent 396e280 commit ade3b3e

File tree

107 files changed

+12196
-555
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+12196
-555
lines changed

Delete-BIN-OBJ-Folders.bat

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@ECHO off
2+
cls
3+
4+
ECHO Deleting all BIN and OBJ folders...
5+
ECHO.
6+
7+
FOR /d /r . %%d in (bin,obj) DO (
8+
IF EXIST "%%d" (
9+
ECHO %%d | FIND /I "\node_modules\" > Nul && (
10+
ECHO.Skipping: %%d
11+
) || (
12+
ECHO.Deleting: %%d
13+
rd /s/q "%%d"
14+
)
15+
)
16+
)
17+
18+
ECHO.
19+
ECHO.BIN and OBJ folders have been successfully deleted. Press any key to exit.
20+
pause > nul

SimplCommerce.sln

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.WishLi
117117
EndProject
118118
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Comments", "src\Modules\SimplCommerce.Module.Comments\SimplCommerce.Module.Comments.csproj", "{4C78BFFD-3AD6-4C39-A44D-F7FC55A2B3A6}"
119119
EndProject
120-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.PaymentBraintree", "src\Modules\SimplCommerce.Module.PaymentBraintree\SimplCommerce.Module.PaymentBraintree.csproj", "{3BF9411D-4794-404D-9ADE-8DABD50853D1}"
120+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.HangfireJobs", "src\Modules\SimplCommerce.Module.HangfireJobs\SimplCommerce.Module.HangfireJobs.csproj", "{73B6C339-0C11-46DB-AFC5-2599E4476224}"
121+
EndProject
122+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Notifications", "src\Modules\SimplCommerce.Module.Notifications\SimplCommerce.Module.Notifications.csproj", "{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}"
123+
EndProject
124+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.PaymentBraintree", "src\Modules\SimplCommerce.Module.PaymentBraintree\SimplCommerce.Module.PaymentBraintree.csproj", "{69EDEA3F-D895-4A16-9742-3F6660A12D51}"
121125
EndProject
122126
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Inventory.Tests", "test\SimplCommerce.Module.Inventory.Tests\SimplCommerce.Module.Inventory.Tests.csproj", "{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}"
123127
EndProject
128+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplCommerce.Module.SignalR", "src\Modules\SimplCommerce.Module.SignalR\SimplCommerce.Module.SignalR.csproj", "{6A59FF75-297F-4119-8E15-6B82C12188D9}"
129+
EndProject
124130
Global
125131
GlobalSection(SolutionConfigurationPlatforms) = preSolution
126132
Debug|Any CPU = Debug|Any CPU
@@ -623,18 +629,42 @@ Global
623629
{4C78BFFD-3AD6-4C39-A44D-F7FC55A2B3A6}.Release|x64.Build.0 = Release|Any CPU
624630
{4C78BFFD-3AD6-4C39-A44D-F7FC55A2B3A6}.Release|x86.ActiveCfg = Release|Any CPU
625631
{4C78BFFD-3AD6-4C39-A44D-F7FC55A2B3A6}.Release|x86.Build.0 = Release|Any CPU
626-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
627-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
628-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Debug|x64.ActiveCfg = Debug|Any CPU
629-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Debug|x64.Build.0 = Debug|Any CPU
630-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Debug|x86.ActiveCfg = Debug|Any CPU
631-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Debug|x86.Build.0 = Debug|Any CPU
632-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
633-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|Any CPU.Build.0 = Release|Any CPU
634-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|x64.ActiveCfg = Release|Any CPU
635-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|x64.Build.0 = Release|Any CPU
636-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|x86.ActiveCfg = Release|Any CPU
637-
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|x86.Build.0 = Release|Any CPU
632+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
633+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Debug|Any CPU.Build.0 = Debug|Any CPU
634+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Debug|x64.ActiveCfg = Debug|Any CPU
635+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Debug|x64.Build.0 = Debug|Any CPU
636+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Debug|x86.ActiveCfg = Debug|Any CPU
637+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Debug|x86.Build.0 = Debug|Any CPU
638+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Release|Any CPU.ActiveCfg = Release|Any CPU
639+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Release|Any CPU.Build.0 = Release|Any CPU
640+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Release|x64.ActiveCfg = Release|Any CPU
641+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Release|x64.Build.0 = Release|Any CPU
642+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Release|x86.ActiveCfg = Release|Any CPU
643+
{73B6C339-0C11-46DB-AFC5-2599E4476224}.Release|x86.Build.0 = Release|Any CPU
644+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
645+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Debug|Any CPU.Build.0 = Debug|Any CPU
646+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Debug|x64.ActiveCfg = Debug|Any CPU
647+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Debug|x64.Build.0 = Debug|Any CPU
648+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Debug|x86.ActiveCfg = Debug|Any CPU
649+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Debug|x86.Build.0 = Debug|Any CPU
650+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Release|Any CPU.ActiveCfg = Release|Any CPU
651+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Release|Any CPU.Build.0 = Release|Any CPU
652+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Release|x64.ActiveCfg = Release|Any CPU
653+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Release|x64.Build.0 = Release|Any CPU
654+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Release|x86.ActiveCfg = Release|Any CPU
655+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001}.Release|x86.Build.0 = Release|Any CPU
656+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
657+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Debug|Any CPU.Build.0 = Debug|Any CPU
658+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Debug|x64.ActiveCfg = Debug|Any CPU
659+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Debug|x64.Build.0 = Debug|Any CPU
660+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Debug|x86.ActiveCfg = Debug|Any CPU
661+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Debug|x86.Build.0 = Debug|Any CPU
662+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Release|Any CPU.ActiveCfg = Release|Any CPU
663+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Release|Any CPU.Build.0 = Release|Any CPU
664+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Release|x64.ActiveCfg = Release|Any CPU
665+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Release|x64.Build.0 = Release|Any CPU
666+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Release|x86.ActiveCfg = Release|Any CPU
667+
{69EDEA3F-D895-4A16-9742-3F6660A12D51}.Release|x86.Build.0 = Release|Any CPU
638668
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
639669
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
640670
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -647,6 +677,18 @@ Global
647677
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|x64.Build.0 = Release|Any CPU
648678
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|x86.ActiveCfg = Release|Any CPU
649679
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|x86.Build.0 = Release|Any CPU
680+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
681+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
682+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Debug|x64.ActiveCfg = Debug|Any CPU
683+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Debug|x64.Build.0 = Debug|Any CPU
684+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Debug|x86.ActiveCfg = Debug|Any CPU
685+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Debug|x86.Build.0 = Debug|Any CPU
686+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
687+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Release|Any CPU.Build.0 = Release|Any CPU
688+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Release|x64.ActiveCfg = Release|Any CPU
689+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Release|x64.Build.0 = Release|Any CPU
690+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Release|x86.ActiveCfg = Release|Any CPU
691+
{6A59FF75-297F-4119-8E15-6B82C12188D9}.Release|x86.Build.0 = Release|Any CPU
650692
EndGlobalSection
651693
GlobalSection(SolutionProperties) = preSolution
652694
HideSolutionNode = FALSE
@@ -695,8 +737,11 @@ Global
695737
{EB2D720D-E784-4A21-A638-ACD462167FDF} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
696738
{67B14D0D-0BA7-4E6C-8F8E-151C92732D6D} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
697739
{4C78BFFD-3AD6-4C39-A44D-F7FC55A2B3A6} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
698-
{3BF9411D-4794-404D-9ADE-8DABD50853D1} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
740+
{73B6C339-0C11-46DB-AFC5-2599E4476224} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
741+
{5918CAEE-7E1A-4BAD-8655-4C53FEA3D001} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
742+
{69EDEA3F-D895-4A16-9742-3F6660A12D51} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
699743
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D}
744+
{6A59FF75-297F-4119-8E15-6B82C12188D9} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
700745
EndGlobalSection
701746
GlobalSection(ExtensibilityGlobals) = postSolution
702747
SolutionGuid = {B9D0D8F0-1AB9-44DD-839F-ED8CEE7DDB10}

src/Modules/SimplCommerce.Module.Catalog/wwwroot/admin/category/category-list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h2>{{::vm.translate.get('Categories')}}</h2>
1313
<thead>
1414
<tr>
1515
<th>{{::vm.translate.get('Name')}}</th>
16-
<th>{{::vm.translate.get('Include in menu')}}</th>
16+
<th class="text-center">{{::vm.translate.get('Include in menu')}}</th>
1717
<th>{{::vm.translate.get('Display order')}}</th>
1818
<th class="text-center">{{::vm.translate.get('Is Published')}}</th>
1919
<th>{{::vm.translate.get('Actions')}}</th>
@@ -31,4 +31,4 @@ <h2>{{::vm.translate.get('Categories')}}</h2>
3131
</td>
3232
</tr>
3333
</tbody>
34-
</table>
34+
</table>

src/Modules/SimplCommerce.Module.Core/Extensions/Settings/SettingDefinition.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using Microsoft.CodeAnalysis;
43

54
namespace SimplCommerce.Module.Core
65
{
@@ -15,12 +14,12 @@ public class SettingDefinition
1514
/// Display name of the setting.
1615
/// This can be used to show setting to the user.
1716
/// </summary>
18-
public LocalizableString DisplayName { get; set; }
17+
public string DisplayName { get; set; }
1918

2019
/// <summary>
2120
/// A brief description for this setting.
2221
/// </summary>
23-
public LocalizableString Description { get; set; }
22+
public string Description { get; set; }
2423

2524
/// <summary>
2625
/// Default value of the setting.
@@ -50,8 +49,8 @@ public class SettingDefinition
5049
public SettingDefinition(
5150
string name,
5251
string defaultValue,
53-
LocalizableString displayName = null,
54-
LocalizableString description = null,
52+
string displayName = null,
53+
string description = null,
5554
bool isVisibleToClients = false)
5655
{
5756
if (string.IsNullOrEmpty(name))

src/Modules/SimplCommerce.Module.Core/Extensions/Settings/SettingDefinitionExtensions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
using System;
1+
using Microsoft.AspNetCore.Builder;
22
using Microsoft.Extensions.DependencyInjection;
33

44
namespace SimplCommerce.Module.Core
55
{
66
public static class SettingDefinitionExtensions
77
{
8-
public static void AddSettingDefinitionItems(this IServiceProvider serviceProvider, params SettingDefinition[] defaultSettings)
8+
/// <summary>
9+
/// Can use this method add default user settings in other module.
10+
/// </summary>
11+
/// <param name="app"></param>
12+
/// <param name="defaultSettings"></param>
13+
public static void AddSettingDefinitionItems(this IApplicationBuilder app, params SettingDefinition[] defaultSettings)
914
{
10-
var settingDefinitionProvider = serviceProvider.GetService<SettingDefinitionProvider>();
15+
var settingDefinitionProvider = app.ApplicationServices.GetService<SettingDefinitionProvider>();
1116
settingDefinitionProvider.AddOrUpdate(defaultSettings);
1217
}
1318
}

src/Modules/SimplCommerce.Module.Core/ModuleInitializer.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ public void ConfigureServices(IServiceCollection serviceCollection)
2727

2828
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
2929
{
30-
app.ApplicationServices.AddSettingDefinitionItems(Demo_UserSettingItemsInThisModule());
31-
}
32-
33-
private SettingDefinition[] Demo_UserSettingItemsInThisModule()
34-
{
35-
var settings = new[]
36-
{
37-
new SettingDefinition("ReceiveNotifications", "true", isVisibleToClients: true),
38-
new SettingDefinition("Setting1", "Setting1_value", isVisibleToClients: true),
39-
new SettingDefinition("Setting2", "Setting2_value", isVisibleToClients: true),
40-
new SettingDefinition("Setting3", "Setting3_value")
41-
};
42-
43-
return settings;
4430
}
4531
}
4632
}

src/Modules/SimplCommerce.Module.EmailSenderSmtp/SimplCommerce.Module.EmailSenderSmtp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<ItemGroup>
44
<ProjectReference Include="..\SimplCommerce.Module.Core\SimplCommerce.Module.Core.csproj" />
5-
<PackageReference Include="MailKit" Version="2.1.1" />
5+
<PackageReference Include="MailKit" Version="2.1.2" />
66
</ItemGroup>
77

88
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO:Hangfire.Redis Integration
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using SimplCommerce.Infrastructure.Helpers;
3+
using SimplCommerce.Module.HangfireJobs.Services;
4+
5+
namespace SimplCommerce.Module.HangfireJobs.Extensions
6+
{
7+
/// <summary>
8+
/// Some extension methods for <see cref="IBackgroundJobManager"/>.
9+
/// </summary>
10+
public static class BackgroundJobManagerExtensions
11+
{
12+
/// <summary>
13+
/// Enqueues a job to be executed.
14+
/// </summary>
15+
/// <typeparam name="TArgs">Type of the arguments of job.</typeparam>
16+
/// <param name="backgroundJobManager">Background job manager reference</param>
17+
/// <param name="args">Job arguments.</param>
18+
/// <param name="delay">Job delay (wait duration before first try).</param>
19+
public static string Enqueue<TArgs>(this IBackgroundJobManager backgroundJobManager, TArgs args, TimeSpan? delay = null)
20+
{
21+
return AsyncHelper.RunSync(() => backgroundJobManager.EnqueueAsync(args, delay));
22+
}
23+
}
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Hangfire;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Options;
5+
using SimplCommerce.Module.HangfireJobs.Internal;
6+
7+
namespace SimplCommerce.Module.HangfireJobs.Extensions
8+
{
9+
public static class HangfireConfigureExtensions
10+
{
11+
public static void UseHangfire(this IApplicationBuilder app)
12+
{
13+
var options = app.ApplicationServices.GetService<IOptions<HangfireConfigureOptions>>()?.Value ?? new HangfireConfigureOptions();
14+
15+
if (options.EnableServer)
16+
{
17+
var _backgroundJobServerOptions = app.ApplicationServices.GetService<IOptions<BackgroundJobServerOptions>>()?.Value;
18+
app.UseHangfireServer(_backgroundJobServerOptions);
19+
}
20+
21+
if (options.Dasbhoard.Enabled)
22+
{
23+
ConfigureDashboard(app, options);
24+
}
25+
}
26+
27+
private static void ConfigureDashboard(IApplicationBuilder app, HangfireConfigureOptions options)
28+
{
29+
var dashboardOptions = app.ApplicationServices.GetService<IOptions<DashboardOptions>>()?.Value ?? new DashboardOptions();
30+
31+
if (options.Dasbhoard.EnableAuthorization)
32+
{
33+
var dashboardAuthorizationFilter = new HangfireDashboardAuthorizationFilter(options.Dasbhoard.AuthorizationCallback);
34+
35+
dashboardOptions.Authorization = new[] { dashboardAuthorizationFilter };
36+
}
37+
38+
app.UseHangfireDashboard(options: dashboardOptions);
39+
}
40+
}
41+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using Hangfire;
5+
using Hangfire.Common;
6+
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using SimplCommerce.Infrastructure.Helpers;
9+
using SimplCommerce.Module.HangfireJobs.Internal;
10+
using SimplCommerce.Module.HangfireJobs.Models;
11+
12+
namespace SimplCommerce.Module.HangfireJobs.Extensions
13+
{
14+
public static class HangfireJobsRegisterExtensions
15+
{
16+
private static readonly MethodInfo _executeMethod = typeof(ScheduledJob).GetMethod(nameof(ScheduledJob.ExecuteAsync));
17+
18+
/// <summary>
19+
/// Initialize all registered jobs
20+
/// </summary>
21+
/// <param name="app"></param>
22+
public static void InitializeHangfireJobs(this IApplicationBuilder app)
23+
{
24+
using (var scope = app.ApplicationServices.CreateScope())
25+
{
26+
var sp = scope.ServiceProvider;
27+
var backgroundJobTypes = sp.GetRequiredService<BackgroundJobCollection>();
28+
29+
var services = sp.GetRequiredService<IServiceCollection>().Where(x => x.ImplementationType != null);
30+
foreach (var item in services)
31+
{
32+
var jobType = item.ImplementationType;
33+
34+
// Register Background Jobs
35+
if (ReflectionHelper.IsAssignableToGenericType(jobType, typeof(IBackgroundJob<>)))
36+
{
37+
backgroundJobTypes.AddJob(jobType);
38+
}
39+
40+
// Register Scheduled Jobs
41+
if (typeof(ScheduledJob).IsAssignableFrom(jobType))
42+
{
43+
var job = (ScheduledJob)sp.GetService(jobType);
44+
if (job == null)
45+
{
46+
throw new Exception("The job type is not registered to DI: " + jobType);
47+
}
48+
49+
try
50+
{
51+
if (!string.IsNullOrEmpty(job.Schedule))
52+
{
53+
var _recurringJobManager = sp.GetService<IRecurringJobManager>();
54+
_recurringJobManager.AddOrUpdate(jobType.Name, new Job(jobType, _executeMethod, null, null), job.Schedule);
55+
}
56+
}
57+
catch (Exception ex)
58+
{
59+
throw new InvalidOperationException($"Unable to activate job {jobType.Name}.", ex);
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)