【码农世界】基于IIS进程部署定时作业任务,作业组件-Quartz.NET 3.x

  • 管理员 发布于 2019-12-26
  • 栏目:技术宅男
  • 来源:本站
  • 人围观
  • 个不明物体

本文将介绍两个点的应用:

1、作业组件的简单-Quartz.NET 3.x

2、基于IIS进程部署定时作业任务


下面开始:

1、作业组件的简单-Quartz.NET 3.x

1.1 首先在项目工程上引用Quartz组件(NuGet上下载就成)

image.png

1.2 我这边把这个组件做成了一个公用的功能(封装成一个类),以便不同业务能够调用

/// <summary>  
    /// QuartzHelper的摘要说明。   
    /// </summary>   
    public class QuartzHelper
    {
        public static async Task SimpleRun<T>(int Minutes) where T:IJob
        {
            try
            {
                string groupname = "G_Group";
                string jogname = "J_Job";
                string triggername = "T_Trigger";

                // 抓住从工厂调度器实例
                NameValueCollection props = new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" }
                };
                StdSchedulerFactory factory = new StdSchedulerFactory(props);
                IScheduler scheduler = await factory.GetScheduler();

                // 开始
                await scheduler.Start();

                // 定义作业与我们的HelloJob类
                IJobDetail job = JobBuilder.Create<T>()
                    .WithIdentity(jogname, groupname)
                    .Build();

                // 现在触发工作运行,然后重复每1秒
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity(triggername, groupname)
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInMinutes(Minutes)            //在这里配置执行延时
                        .RepeatForever()
                        )
                    .Build();

                // 告诉Quartz用定义的触发器执行作业
                await scheduler.ScheduleJob(job, trigger);

                // 睡眠时显示发生了什么
                //await Task.Delay(TimeSpan.FromSeconds(5));
                // 关闭本次任务
                //await scheduler.Shutdown();           

                //如果解除await Task.Delay(TimeSpan.FromSeconds(5))和await scheduler.Shutdown()的注释,
                //5秒后输出"Press any key to close the application",
                //scheduler里注册的任务也会停止。


            }
            catch (SchedulerException se)
            {
                LogHelper.WriteErrorLog("QuartzHelper_Task SimpleRun:内部异常", se);
            }
        }
    }

1.3 具体的业务处理类(继承的Execute必须为async,但至于里的方法要不要async可按照业务业定)

image.png

1.4 调用,这边作业是12小时执行一次

QuartzHelper.SimpleRun<Hanwq.CMS.Web.Plugin.Gather.Common.AutoGather>(Utils.ObjToInt(Utils.GetAppSetting("auto_gather_times"), 720)).GetAwaiter().GetResult();

2.基于IIS进程部署定时作业任务

2.1 部署调用,在项目里创建Global.asax,然后调用即可,12小时执行一次

protected void Application_Start(object sender, EventArgs e)
        {
            LogHelper.WriteWarnLog("IIS应用程序池启动...");
            if (Utils.GetAppSetting("is_auto_gather") == "1")
            {
                QuartzHelper.SimpleRun<Hanwq.CMS.Web.Plugin.Gather.Common.AutoGather>(Utils.ObjToInt(Utils.GetAppSetting("auto_gather_times"), 720)).GetAwaiter().GetResult();
            }
        }

2.2 部署IIS进程上的蛋疼点:若网站无访问,到一定时间后,IIS线程池会被回收掉,那时前面部署的作业也将被干掉,无法执行。。

这时可以这样玩:

image.png

image.png

就是在IIS回收时,请求网站中的一个网页,强制让IIS再启动起来。。这只是一个没办法中的办法,谁让我是个穷B,没有服务器,只能用用虚拟主机。。


为了降低IIS被回收的概率,我把要执行的业务功能写在了那个网页当中,再把作业时间短(小于IIS自动回收时间),每次Quartz执行作业就调用下网页,让IIS觉得网站是有人访问的,我真TMD太聪明了。。。




原创贴,转载请带上页面地址。






欢迎踩踏本文章

来说点啥吧

看人家说的