增加定时任务,

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

+3 -1
View File
@@ -33,8 +33,10 @@ namespace KJD_Web
// 让 Web API 用 Autofac 解析 Controller
GlobalConfiguration.Configuration.DependencyResolver =
new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configure(WebApiConfig.Register);
ContainerConfig.BuildHangfire(GlobalConfiguration.Configuration.DependencyResolver);
GlobalConfiguration.Configure(WebApiConfig.Register);
}
+4
View File
@@ -26,6 +26,10 @@
<add name="WxFindOrderLimitVersion" connectionString="172" />
<add name="WxFindOrderLowVersion" connectionString="99" />
</connectionStrings>
<appSettings>
<!-- 禁用 OWIN 启动类的自动发现,避免与现有项目结构冲突 -->
<add key="owin:AutomaticAppStartup" value="false" />
</appSettings>
<!--
有关 web.config 更改的说明,请参见 http://go.microsoft.com/fwlink/?LinkId=235367。
@@ -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>
@@ -0,0 +1,31 @@
using MAX.Core.DependencyInjection;
using MAX.Core.Helpers;
using Store.Framework.Jobs;
using Store.VirtualLogistics.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Store.SyncLogistics.Jobs;
[Job("synclogistics_balance_check", "*/10 * * * *")]
internal class BalanceCheckJob : IScheduledJobs, ITransientDependency
{
private readonly VirtualLogisticsService _virtualLogisticsService;
public BalanceCheckJob(VirtualLogisticsService virtualLogisticsService)
{
_virtualLogisticsService = virtualLogisticsService;
}
public Task RunAsync()
{
var money = _virtualLogisticsService.GetUserMoney();
if (money < 50)
{
HttpHelper.Get($"https://iyuu.cn/IYUU71425T5f235fa5f5afb2087416750622ed4d241c89da44.send?text=余额不足:{money}");
}
return Task.CompletedTask;
}
}
+28
View File
@@ -0,0 +1,28 @@
using Autofac;
using Hangfire;
using MAX.Core.DependencyInjection;
using MAX.Core.Interfaces;
using Store.SyncLogistics.Jobs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Store.SyncLogistics;
internal class Startup : IStartup
{
public int Priority => IntPriority.Normal;
public void Initialize()
{
}
public void Register(IServiceRegistrar registrar)
{
}
}