长话短说,今天聊一聊分布式定时任务,个人流水帐笔记:html
细心朋友稍一分析,就知道还有问题:
水平扩展后的WebApp的Quartz.net定时任务会屡次触发,
由于webapp实例使用的是默认的RAMJobStore
, 多实例在内存中都维护了Job和Trigger的副本.git
个人定时任务是同步任务,屡次执行却是没有太大问题,但对于特定业务的定时任务, 屡次执行多是致命问题。github
基于此,来看看Quartz.net 分布式定时任务的姿式web
很明显,水平扩展的多实例须要一个 独立于web实例的机制来存储Job和Trigger.sql
Quartz.NET提供ADO.NET JobStore来存储任务数据。数据库
执行脚本以后,会看到数据库中多出几个以 QRTZ_开头的表app
可采用编码形式或者 quartz.config形式添加配置负载均衡
从https://github.com/quartznet/quartznet/tree/master/database/tables 下载合适的数据库表脚本, 生成指定的表结构webapp
本次使用编码方式添加AdoJobStore配置。
首次启动会将代码中Job和Trigger持久化到sqlite,后面就直接从sqlite中加载Job和Triggerasync
using System; using System.Collections.Specialized; using System.Data; using System.Threading.Tasks; using Microsoft.Data.Sqlite; using Microsoft.Extensions.Logging; using Quartz; using Quartz.Impl; using Quartz.Impl.AdoJobStore.Common; using Quartz.Spi; namespace EqidManager { using IOCContainer = IServiceProvider; public class QuartzStartup { public IScheduler Scheduler { get; set; } private readonly ILogger _logger; private readonly IJobFactory iocJobfactory; public QuartzStartup(IOCContainer IocContainer, ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<QuartzStartup>(); iocJobfactory = new IOCJobFactory(IocContainer); DbProvider.RegisterDbMetadata("sqlite-custom", new DbMetadata() { AssemblyName = typeof(SqliteConnection).Assembly.GetName().Name, ConnectionType = typeof(SqliteConnection), CommandType = typeof(SqliteCommand), ParameterType = typeof(SqliteParameter), ParameterDbType = typeof(DbType), ParameterDbTypePropertyName = "DbType", ParameterNamePrefix = "@", ExceptionType = typeof(SqliteException), BindByName = true }); var properties = new NameValueCollection { ["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz", ["quartz.jobStore.useProperties"] = "true", ["quartz.jobStore.dataSource"] = "default", ["quartz.jobStore.tablePrefix"] = "QRTZ_", ["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz", ["quartz.dataSource.default.provider"] = "sqlite-custom", ["quartz.dataSource.default.connectionString"] = "Data Source=EqidManager.db", ["quartz.jobStore.lockHandler.type"] = "Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz", ["quartz.serializer.type"] = "binary" }; var schedulerFactory = new StdSchedulerFactory(properties); Scheduler = schedulerFactory.GetScheduler().Result; Scheduler.JobFactory = iocJobfactory; } public async Task<IScheduler> ScheduleJob() { var _eqidCounterResetJob = JobBuilder.Create<EqidCounterResetJob>() .WithIdentity("EqidCounterResetJob") .Build(); var _eqidCounterResetJobTrigger = TriggerBuilder.Create() .WithIdentity("EqidCounterResetCron") .StartNow() //天天凌晨0s .WithCronSchedule("0 0 0 * * ?") Seconds,Minutes,Hours,Day-of-Month,Month,Day-of-Week,Year(optional field) .Build(); // 这里必定要先判断是否已经从SQlite中加载了Job和Trigger if (!await Scheduler.CheckExists(new JobKey("EqidCounterResetJob")) && !await Scheduler.CheckExists(new TriggerKey("EqidCounterResetCron"))) { await Scheduler.ScheduleJob(_eqidCounterResetJob, _eqidCounterResetJobTrigger); } await Scheduler.Start(); return Scheduler; } public void EndScheduler() { if (Scheduler == null) { return; } if (Scheduler.Shutdown(waitForJobsToComplete: true).Wait(30000)) Scheduler = null; else { } _logger.LogError("Schedule job upload as application stopped"); } } }
上面是Quartz.NET 从sqlite中加载Job和Trigger的核心代码
这里要提示两点:
①. IOCJobFactory 是自定义JobFactory,目的是与ASP.NET Core原生依赖注入结合
②. 在调度任务的时候, 要先判断是否已经从sqlite加载了Job和Trigger
附赠Quartz.NET的调度UI: CrystalQuartz,方便在界面管理调度任务
① Install-Package CrystalQuartz.AspNetCore -IncludePrerelease
② Startup启用CrystalQuartz
using CrystalQuartz.AspNetCore; /* * app is IAppBuilder * scheduler is your IScheduler (local or remote) */ var quartz = app.ApplicationServices.GetRequiredService<QuartzStartup>(); var _schedule = await quartz.ScheduleJob(); app.UseCrystalQuartz(() => scheduler);
③ 在localhost:YOUR_PORT/quartz地址查看调度
以上配置只是完成从DB加载Job和Trigger, 从实际看没有解决Quartz.net在集群环境下执行重复任务的事情,
须要添加 cluster= true 属性支持负载均衡和故障转移, 抱歉,以上代码只是完成了从DB分离加载Quartz配置的步骤。
["quartz.jobStore.clustered"] = "true", ["quartz.scheduler.instanceId"] = "AUTO"
https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/advanced-enterprise-features.html
https://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontriggers.html