feat(新增): 执行动态任务不在
This commit is contained in:
parent
55e3509d1f
commit
4f429d09a4
|
@ -17,7 +17,7 @@ public class JobService : IJobService
|
||||||
// var scheduler = schedulerFactory.GetScheduler();
|
// var scheduler = schedulerFactory.GetScheduler();
|
||||||
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
||||||
|
|
||||||
var jobDetail = JobBuilder.Create<SimJob>()
|
var jobDetail = JobBuilder.Create<SimpleJob>()
|
||||||
.UsingJobData("username", "用户名")
|
.UsingJobData("username", "用户名")
|
||||||
.UsingJobData("password", "密码")
|
.UsingJobData("password", "密码")
|
||||||
.WithIdentity("simpleJob", "简单的JOB")
|
.WithIdentity("simpleJob", "简单的JOB")
|
||||||
|
@ -46,7 +46,7 @@ public class JobService : IJobService
|
||||||
var stdSchedulerFactory = new StdSchedulerFactory();
|
var stdSchedulerFactory = new StdSchedulerFactory();
|
||||||
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
||||||
|
|
||||||
var jobDetail = JobBuilder.Create<SimJob>()
|
var jobDetail = JobBuilder.Create<SimpleJob>()
|
||||||
.UsingJobData("username", "用户名")
|
.UsingJobData("username", "用户名")
|
||||||
.UsingJobData("password", "密码")
|
.UsingJobData("password", "密码")
|
||||||
// TODO 如果不设置持久的会报错;
|
// TODO 如果不设置持久的会报错;
|
||||||
|
@ -81,7 +81,7 @@ public class JobService : IJobService
|
||||||
var stdSchedulerFactory = new StdSchedulerFactory();
|
var stdSchedulerFactory = new StdSchedulerFactory();
|
||||||
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
||||||
|
|
||||||
var jobDetail = JobBuilder.Create<SimJob>()
|
var jobDetail = JobBuilder.Create<SimpleJob>()
|
||||||
.UsingJobData("username", "用户名")
|
.UsingJobData("username", "用户名")
|
||||||
.UsingJobData("password", "密码")
|
.UsingJobData("password", "密码")
|
||||||
.StoreDurably() // 设置作业为持久的
|
.StoreDurably() // 设置作业为持久的
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Quartz;
|
||||||
|
using Quartz.Impl;
|
||||||
|
|
||||||
|
namespace Bunny.Service.Job.JobService;
|
||||||
|
|
||||||
|
public static class CronJobService
|
||||||
|
{
|
||||||
|
public static void AddCronJobService(this WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
var stdSchedulerFactory = new StdSchedulerFactory();
|
||||||
|
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
||||||
|
|
||||||
|
var jobDetail = JobBuilder.Create<SimpleJob>()
|
||||||
|
.UsingJobData("username", "用户名")
|
||||||
|
.UsingJobData("password", "密码")
|
||||||
|
.StoreDurably() // 设置作业为持久的
|
||||||
|
.WithIdentity("simpleJob", "简单的JOB")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
scheduler.AddJob(jobDetail, true).GetAwaiter().GetResult();
|
||||||
|
|
||||||
|
var trigger = TriggerBuilder.Create()
|
||||||
|
.ForJob(jobDetail)
|
||||||
|
.UsingJobData("trigger", "trigger值")
|
||||||
|
.WithIdentity("testTrigger", "测试发出器")
|
||||||
|
.StartNow()
|
||||||
|
.WithCronSchedule("0/1 * * * * ?")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
scheduler.ScheduleJob(trigger).GetAwaiter().GetResult();
|
||||||
|
scheduler.Start().GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Quartz;
|
||||||
|
using Quartz.Impl;
|
||||||
|
|
||||||
|
namespace Bunny.Service.Job.JobService;
|
||||||
|
|
||||||
|
public static class SimpleJobService
|
||||||
|
{
|
||||||
|
public static void AddCreateSimpleJobService(this WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
var schedulerFactory = new StdSchedulerFactory();
|
||||||
|
// TODO 如果使用异步,必须使用await 和 async
|
||||||
|
// var scheduler = schedulerFactory.GetScheduler();
|
||||||
|
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
||||||
|
|
||||||
|
var jobDetail = JobBuilder.Create<SimpleJob>()
|
||||||
|
.UsingJobData("username", "用户名")
|
||||||
|
.UsingJobData("password", "密码")
|
||||||
|
.WithIdentity("simpleJob", "简单的JOB")
|
||||||
|
.Build();
|
||||||
|
var trigger = TriggerBuilder.Create()
|
||||||
|
.WithIdentity("testTrigger", "测试发出器")
|
||||||
|
.StartNow()
|
||||||
|
// 每隔5秒执行一次,一共重复10次
|
||||||
|
.WithSimpleSchedule(x => x.WithIntervalInSeconds(1).WithRepeatCount(10))
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// 使用同步方法
|
||||||
|
scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
|
||||||
|
scheduler.Start().GetAwaiter().GetResult();
|
||||||
|
|
||||||
|
// TODO 使用异步
|
||||||
|
// await _scheduler.ScheduleJob(jobDetail, trigger);
|
||||||
|
// await _scheduler.Start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace Bunny.Service.Job;
|
namespace Bunny.Service.Job;
|
||||||
|
|
||||||
public class SimJob : IJob
|
public class SimpleJob : IJob
|
||||||
{
|
{
|
||||||
public Task Execute(IJobExecutionContext context)
|
public Task Execute(IJobExecutionContext context)
|
||||||
{
|
{
|
|
@ -11,40 +11,22 @@ public class BaseConfig(WebApplicationBuilder builder)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
// 注入服务依赖、Host服务、插件等
|
|
||||||
AddApplicationService();
|
|
||||||
// 使用扩展,第三方如Redis、Minio、SQLSugar
|
|
||||||
UseExtend();
|
|
||||||
// 配置跨域
|
// 配置跨域
|
||||||
UseCors();
|
UseCors();
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 注入控制器、服务依赖、Host服务等
|
|
||||||
/// </summary>
|
|
||||||
private void AddApplicationService()
|
|
||||||
{
|
|
||||||
// 配置日志相关
|
// 配置日志相关
|
||||||
// builder.Logging.AddLog4Net("Config/log4net.config");
|
// builder.Logging.AddLog4Net("Config/log4net.config");
|
||||||
// 添加Service服务
|
|
||||||
builder.AddApplicationServices();
|
|
||||||
// 添加后台服务
|
|
||||||
builder.AddApplicationBackendServices();
|
|
||||||
// 添加使用自定义配置文件
|
// 添加使用自定义配置文件
|
||||||
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
||||||
// 添加 SignalR
|
// 添加 SignalR
|
||||||
builder.Services.AddSignalR();
|
builder.Services.AddSignalR();
|
||||||
|
// 添加Service服务
|
||||||
|
builder.AddApplicationServices();
|
||||||
|
// 添加后台服务
|
||||||
|
builder.AddApplicationBackendServices();
|
||||||
// 添加验证码
|
// 添加验证码
|
||||||
builder.AddCaptcha();
|
builder.AddCaptcha();
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 扩展服务
|
|
||||||
/// </summary>
|
|
||||||
private void UseExtend()
|
|
||||||
{
|
|
||||||
// 设置过滤器
|
// 设置过滤器
|
||||||
builder.FilterConfigInitialize();
|
builder.AddFilterConfigInitialize();
|
||||||
// 初始化Redis
|
// 初始化Redis
|
||||||
builder.AddRedisContext();
|
builder.AddRedisContext();
|
||||||
// 初始化Minio
|
// 初始化Minio
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
using Bunny.Common.Filter;
|
|
||||||
using Bunny.Service.Filter;
|
|
||||||
|
|
||||||
namespace Bunny.WebApi.Config;
|
|
||||||
|
|
||||||
public static class FilterConfig
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 配置过滤器
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="builder"></param>
|
|
||||||
public static void FilterConfigInitialize(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
// 添加自定义过滤器---全局异常捕获
|
|
||||||
builder.Services.AddControllers(option =>
|
|
||||||
{
|
|
||||||
option.Filters.Add<GlobalExceptionFilter>();
|
|
||||||
option.Filters.Add<AuthorizationFilter>();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
using Lazy.Captcha.Core;
|
|
||||||
using Lazy.Captcha.Core.Generator;
|
|
||||||
using SkiaSharp;
|
|
||||||
|
|
||||||
namespace Bunny.WebApi.Config;
|
|
||||||
|
|
||||||
public static class PluginConfig
|
|
||||||
{
|
|
||||||
public static void AddCaptcha(this WebApplicationBuilder builder)
|
|
||||||
{
|
|
||||||
var service = builder.Services;
|
|
||||||
// 使用图形验证码
|
|
||||||
service.AddDistributedMemoryCache();
|
|
||||||
// 验证码相关配置内容
|
|
||||||
service.AddCaptcha(options =>
|
|
||||||
{
|
|
||||||
options.CaptchaType = CaptchaType.DEFAULT; // 验证码类型
|
|
||||||
options.CodeLength = 4; // 验证码长度
|
|
||||||
options.ExpirySeconds = 60; // 过期时间(单位/秒)
|
|
||||||
options.IgnoreCase = true; // 比较忽略大小写
|
|
||||||
options.ImageOption.Animation = true; // 是否启用动画
|
|
||||||
options.ImageOption.Width = 130; // 验证码宽度
|
|
||||||
options.ImageOption.Height = 48; // 验证码高度
|
|
||||||
options.ImageOption.BackgroundColor = SKColors.White;
|
|
||||||
options.ImageOption.BubbleCount = 6; // 气泡数量
|
|
||||||
options.ImageOption.BubbleMinRadius = 2; // 气泡最小半径
|
|
||||||
options.ImageOption.BubbleMaxRadius = 6; // 气泡最大半径
|
|
||||||
options.ImageOption.BubbleThickness = 2; // 气泡边沿厚度
|
|
||||||
options.ImageOption.InterferenceLineCount = 2; // 干扰线数量
|
|
||||||
options.ImageOption.FontSize = 36; // 字体大小
|
|
||||||
options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; // 字体,中文使用kaiti,其他字符可根据喜好设置
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,12 @@
|
||||||
using Bunny.Common.Connect;
|
using Bunny.Common.Connect;
|
||||||
|
using Bunny.Common.Filter;
|
||||||
|
using Bunny.Service.Filter;
|
||||||
using Bunny.Service.IService;
|
using Bunny.Service.IService;
|
||||||
using Bunny.Service.IService.Service;
|
using Bunny.Service.IService.Service;
|
||||||
|
using Bunny.Service.Job.JobService;
|
||||||
|
using Lazy.Captcha.Core;
|
||||||
|
using Lazy.Captcha.Core.Generator;
|
||||||
|
using SkiaSharp;
|
||||||
|
|
||||||
namespace Bunny.WebApi.Config;
|
namespace Bunny.WebApi.Config;
|
||||||
|
|
||||||
|
@ -20,11 +26,59 @@ public static class ServiceRegistration
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注入后台服务相关
|
/// 注入后台服务相关
|
||||||
|
/// 用于添加所以Job服务,之后在BaseConfig中进行调用
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="builder"></param>
|
/// <param name="builder"></param>
|
||||||
public static void AddApplicationBackendServices(this WebApplicationBuilder builder)
|
public static void AddApplicationBackendServices(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
// 测试注入后台服务
|
// 测试注入后台服务
|
||||||
// builder.Services.AddHostedService<TemplateBackgroundModule>();
|
// builder.Services.AddHostedService<TemplateBackgroundModule>().Clear();
|
||||||
|
// https://github.com/quartznet/quartznet/tree/main/database/tables
|
||||||
|
// builder.AddCreateSimpleJobService();
|
||||||
|
builder.AddCronJobService();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 配置过滤器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder"></param>
|
||||||
|
public static void AddFilterConfigInitialize(this WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
// 添加自定义过滤器---全局异常捕获
|
||||||
|
builder.Services.AddControllers(option =>
|
||||||
|
{
|
||||||
|
option.Filters.Add<GlobalExceptionFilter>();
|
||||||
|
option.Filters.Add<AuthorizationFilter>();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加验证码配置内容
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder"></param>
|
||||||
|
public static void AddCaptcha(this WebApplicationBuilder builder)
|
||||||
|
{
|
||||||
|
var service = builder.Services;
|
||||||
|
// 使用图形验证码
|
||||||
|
service.AddDistributedMemoryCache();
|
||||||
|
// 验证码相关配置内容
|
||||||
|
service.AddCaptcha(options =>
|
||||||
|
{
|
||||||
|
options.CaptchaType = CaptchaType.DEFAULT; // 验证码类型
|
||||||
|
options.CodeLength = 4; // 验证码长度
|
||||||
|
options.ExpirySeconds = 60; // 过期时间(单位/秒)
|
||||||
|
options.IgnoreCase = true; // 比较忽略大小写
|
||||||
|
options.ImageOption.Animation = true; // 是否启用动画
|
||||||
|
options.ImageOption.Width = 130; // 验证码宽度
|
||||||
|
options.ImageOption.Height = 48; // 验证码高度
|
||||||
|
options.ImageOption.BackgroundColor = SKColors.White;
|
||||||
|
options.ImageOption.BubbleCount = 6; // 气泡数量
|
||||||
|
options.ImageOption.BubbleMinRadius = 2; // 气泡最小半径
|
||||||
|
options.ImageOption.BubbleMaxRadius = 6; // 气泡最大半径
|
||||||
|
options.ImageOption.BubbleThickness = 2; // 气泡边沿厚度
|
||||||
|
options.ImageOption.InterferenceLineCount = 2; // 干扰线数量
|
||||||
|
options.ImageOption.FontSize = 36; // 字体大小
|
||||||
|
options.ImageOption.FontFamily = DefaultFontFamilys.Instance.Kaiti; // 字体,中文使用kaiti,其他字符可根据喜好设置
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
Loading…
Reference in New Issue