定时任务配置

This commit is contained in:
tongfei
2023-10-27 10:20:31 +08:00
parent 8221eae779
commit f704cdd606
7 changed files with 398 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Serialization;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
@@ -54,6 +55,7 @@ namespace WMS.Web.Repositories.DependencyInjection
this.AddOther();
this.AddConfigOptions();
this.AddServiceRepositories();
this.AddQuartzService();
}
/// <summary>
/// 其它功能注入AutoMapper等其它
@@ -174,6 +176,50 @@ namespace WMS.Web.Repositories.DependencyInjection
Services.Configure<ErpOptions>(Configuration.GetSection("ErpOptions"));
}
/// <summary>
/// Quartz定时任务-可分布式集群
/// </summary>
private void AddQuartzService()
{
var options = Configuration.GetSection("QuartzJobOptions").Get<QuartzJobOptions>();
//Services.AddTransient<InventoryQuartzJob>();//添加注入定时服务
//Services.AddTransient<OrderContractQuartzJob>();//添加注入定时服务
Services.AddQuartz(q =>
{
q.UsePersistentStore(x =>
{
//存储类型
x.Properties[options.JobStoreTypeKey] = options.JobStoreTypeValue;
//数据库驱动类型-这里是mysql
x.Properties[options.JobStoreDriverDelegateTypeKey] = options.JobStoreDriverDelegateTypeValue;
//表名前缀
x.Properties[options.JobStoreTablePrefixKey] = options.JobStoreTablePrefixValue;
//数据源名称
x.Properties[options.JobStoreDataSourceKey] = options.JobStoreDataSourceValue;
//连接字符串
x.Properties[options.JobStoreConnectionStringKey] = options.JobStoreConnectionStringValue;
//sqlserver版本
x.Properties[options.JobStoreProviderKey] = options.JobStoreProviderValue;
//是否启用集群:是
x.Properties[options.JobStoreClusteredKey] = options.JobStoreClusteredValue;
//集群节点都必须有一个唯一ID
x.Properties[options.JobStoreInstanceIdKey] = options.JobStoreInstanceIdValue;
x.UseProperties = true;
x.UseClustering();
x.UseJsonSerializer();
});
//用于注入
q.UseMicrosoftDependencyInjectionJobFactory();
});
//.net core核心托管-添加Quartz服务器
Services.AddQuartzServer(options =>
{
//关闭时,我们希望作业正常完成
options.WaitForJobsToComplete = false;
});
}
/// <summary>
/// 注入服务层
/// </summary>