增加定时任务,

This commit is contained in:
chfei committed 2026-09-30 15:46:20 +08:00
1 parent be1fc11e07
commit be08510a84
10 files changed
+171 -8

No files matched your search

@@ -2,19 +2,26 @@
using Autofac.Core;
using Autofac.Integration.WebApi;
using FreeSql;
using Hangfire;
using Hangfire.Common;
using Hangfire.InMemory;
using MAX.Core.DependencyInjection;
using MAX.Core.Helpers;
using MAX.Core.Interfaces;
using MAX.Core.Logger;
using MAX.Framework.Logger;
using Org.BouncyCastle.Utilities.Collections;
using Store.Framework.Commons;
using Store.Framework.Jobs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Dependencies;
namespace Store.Framework.DependencyInjection;
@@ -24,7 +31,7 @@ public class ContainerConfig
public static IContainer BuildContainer()
{
var builder = new ContainerBuilder();
var appAssembly = StoreAppTypeFinder.Current.GetAssemblies().ToArray();
var appAssemblys = StoreAppTypeFinder.Current.GetAssemblies().ToArray();
var connection = ConfigurationManager.ConnectionStrings["ConnKJD"];
if (connection == null || string.IsNullOrWhiteSpace(connection.ConnectionString))
@@ -45,7 +52,7 @@ public class ContainerConfig
// 注册 Web API 所有 Controller
builder.RegisterApiControllers(appAssembly);
builder.RegisterApiControllers(appAssemblys);
builder.RegisterInstance(NLogger.Instance).As<ILoggerFacade>().SingleInstance();
@@ -54,7 +61,7 @@ public class ContainerConfig
#region 注册所有IOC接口
Type typeSingleton = typeof(ISingletonDependency);
builder.RegisterAssemblyTypes(appAssembly)
builder.RegisterAssemblyTypes(appAssemblys)
.Where(a =>
typeSingleton.IsAssignableFrom(a)
&& a.IsClass
@@ -65,7 +72,7 @@ public class ContainerConfig
.SingleInstance();
Type typeTransient = typeof(ITransientDependency);
builder.RegisterAssemblyTypes(appAssembly)
builder.RegisterAssemblyTypes(appAssemblys)
.Where(a =>
typeTransient.IsAssignableFrom(a)
&& a.IsClass
@@ -76,7 +83,7 @@ public class ContainerConfig
.InstancePerDependency();
Type typeScoped = typeof(IScopedDependency);
builder.RegisterAssemblyTypes(appAssembly)
builder.RegisterAssemblyTypes(appAssemblys)
.Where(a => typeScoped.IsAssignableFrom(a) && a.IsClass && !a.IsAbstract)
.AsImplementedInterfaces()
.AsSelf()
@@ -84,7 +91,13 @@ public class ContainerConfig
#endregion
IServiceRegistrar registrar = new ServiceRegistrarProxy(builder);
IEnumerable<IStartup> startupList = ReflectionHelper.GetInstancesByInterface<IStartup>(appAssembly);
BuildHangfire(appAssemblys);
IEnumerable<IStartup> startupList = ReflectionHelper.GetInstancesByInterface<IStartup>(appAssemblys);
foreach (IStartup item in startupList.OrderByDescending(a => a.Priority))
{
Debug.WriteLine($"Startup : {item.GetType()}");
@@ -96,4 +109,40 @@ public class ContainerConfig
return app;
}
public static void BuildHangfire(IDependencyResolver app)
{
Hangfire.GlobalConfiguration.Configuration
.UseInMemoryStorage()
.UseActivator(new ContainerJobActivator(app));
var server = new Hangfire.BackgroundJobServer();
var appAssemblys = StoreAppTypeFinder.Current.GetAssemblies().ToArray();
var runAsync = typeof(IScheduledJobs).GetMethod(nameof(IScheduledJobs.RunAsync));
var jobTypes = ReflectionHelper.FindTypes<IScheduledJobs>(appAssemblys)
.Where(t => typeof(IScheduledJobs).IsAssignableFrom(t)
&& !t.IsInterface
&& !t.IsAbstract)
.Select(t => new { Type = t, Attr = t.GetCustomAttribute<JobAttribute>() })
.Where(x => x.Attr != null);
var manager = new RecurringJobManager();
foreach (var x in jobTypes)
{
var job = new Job(x.Type, runAsync);
manager.AddOrUpdate(
x.Attr.Name,
job,
x.Attr.Cron
);
}
}
private static void BuildHangfire(Assembly[] assemblies)
{
//Hangfire.RecurringJob.AddOrUpdate("synclogistics_balance_check", ()=>Debug.WriteLine("1"), "* * * * * *");
}
}
@@ -0,0 +1,15 @@
using Hangfire;
using System.ComponentModel;
using System.Web.Http.Dependencies;
namespace Store.Framework.Jobs;
public class ContainerJobActivator : JobActivator
{
private readonly IDependencyResolver _provider;
public ContainerJobActivator(IDependencyResolver provider) => _provider = provider;
public override object ActivateJob(Type jobType)
=> _provider.GetService(jobType);
}
+12
View File
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Store.Framework.Jobs;
public interface IScheduledJobs
{
Task RunAsync();
}
+20
View File
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Store.Framework.Jobs;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class JobAttribute : Attribute
{
public string Name { get; }
public string Cron { get; }
public JobAttribute(string name, string cron)
{
Name = name;
Cron = cron;
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ namespace Store.Framework;
internal class Startup : IStartup
{
public int Priority => IntPriority.Normal;
public int Priority => IntPriority.High;
public void Initialize()
{
+2
View File
@@ -10,6 +10,8 @@
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="FreeSql.Provider.MySql" Version="3.5.311" />
<PackageReference Include="FreeSql.Repository" Version="3.5.311" />
<PackageReference Include="Hangfire" Version="1.8.25" />
<PackageReference Include="Hangfire.InMemory" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="NLog" Version="6.2.1" />
</ItemGroup>