zoukankan      html  css  js  c++  java
  • 使用FluentScheduler实现定时任务管理

    之前定时任务一直用的Windows服务,前段时间发现FluentScheduler这个框架,他跟Quarz.Net,Hangfire一样都是任务调度框架,但是相对使用而言我觉得FluentScheduler更加方便简单一些.

    1.新建一个mvc项目

    2.nuget直接安装FluentScheduler

    3.新建一个类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace FluentSchedulerWeb
    {
        using FluentScheduler;
        using System.Diagnostics;
        using System.IO;
        using System.Threading;
        public class MyJob : Registry
        {
            public MyJob()
            {
                // 每2秒一次循环
                Schedule<MyJob1>().ToRunNow().AndEvery(2).Seconds();
    
                // 5秒后,只一次
                Schedule<MyOtherJob>().ToRunOnceIn(5).Seconds();
    
                //每天晚上21:15分执行
                Schedule(() => Console.WriteLine("Timed Task - Will run every day at 9:15pm: " + DateTime.Now)).ToRunEvery(1).Days().At(21, 15);
    
                // 每个月的执行
                Schedule(() =>
                {
                    Console.WriteLine("Complex Action Task Starts: " + DateTime.Now);
                    Thread.Sleep(1000);
                    Console.WriteLine("Complex Action Task Ends: " + DateTime.Now);
                }).ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);
    
                //先执行第一个Job、再执行第二个Job;完成后等5秒继续循环
                Schedule<MyFirstJob>().AndThen<MySecoundJob>().ToRunNow().AndEvery(5).Seconds();
            }
        }
    
        public class MyJob1 : IJob
        {
    
            void IJob.Execute()
            {
                Trace.WriteLine("循环每2秒执行一次;现在时间是:" + DateTime.Now);
                var str = "循环每2秒执行一次;现在时间是:" + DateTime.Now.ToString();
                System.IO.StreamWriter writer = null;
                try
                {
    
    
                    //写入日志 
                    string path = string.Empty;
                    path = @"F:ErrorLogs";
                    //不存在则创建错误日志文件夹
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path += string.Format(@"{0}.txt", DateTime.Now.ToString("yyyy-MM-dd"));
    
                    writer = !System.IO.File.Exists(path) ? System.IO.File.CreateText(path) : System.IO.File.AppendText(path); //判断文件是否存在,如果不存在则创建,存在则添加
                    writer.WriteLine(str);
                    writer.WriteLine("********************************************************************************************");
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }
        }
    
        public class MyOtherJob : IJob
        {
    
            void IJob.Execute()
            {
                Trace.WriteLine("5秒后只执行一次 ,现在时间是:" + DateTime.Now);
            }
        }
    
        public class MyFirstJob : IJob
        {
    
            void IJob.Execute()
            {
                Trace.WriteLine("执行第一个 Job ,现在时间是:" + DateTime.Now);
            }
        }
        public class MySecoundJob : IJob
        {
    
            void IJob.Execute()
            {
                Trace.WriteLine("执行第二个 Job ,现在时间是:" + DateTime.Now);
            }
        }
    
    }

    4.Global.asax中启用FluentScheduler

     protected void Application_Start()
    {
       AreaRegistration.RegisterAllAreas();
       RouteConfig.RegisterRoutes(RouteTable.Routes);
       //启用
       JobManager.Initialize(new MyJob());
    }

    这样FluentScheduler就可以使用了

    当然在使用过程中发布到iis必然面临着iis回收的问题

    下面两种方法亲测可用

    方法一.IIS预加载

    (1)修改应用程序池启动模式

     (2)开启对应网站预加载(IIS7.5没有这个)

    (3).增加配置编辑器,编写默认预加载的请求页面

    hostName是地址,initializationPage是要加载的页面

    这样预加载就搞定了

    方法二:通过代码实现iis回收之后自动访问页面来唤醒服务

    using FluentScheduler;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace FluentSchedulerWeb2
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                //启用
                JobManager.Initialize(new MyJob());
            }
    
            protected void Application_End()
            {
                WriteLog("进程即将被IIS回收"+DateTime.Now);
                WriteLog("重新访问一个页面,以唤醒服务" + DateTime.Now);
                string strURL = System.Configuration.ConfigurationManager.AppSettings["homeURL"].ToString();
                try
                {
                    System.Net.WebClient wc = new System.Net.WebClient();
                    System.IO.Stream stream = wc.OpenRead(strURL);
                    System.IO.StreamReader reader = new StreamReader(stream);
                    string html = reader.ReadToEnd();
                    if (!string.IsNullOrWhiteSpace(html))
                    {
                        WriteLog("唤醒程序成功" + DateTime.Now);
                    }
                    reader.Close();
                    reader.Dispose();
                    stream.Close();
                    stream.Dispose();
                    wc.Dispose();
                }
                catch (Exception ex)
                {
                    WriteLog("唤醒异常"+ex.Message+DateTime.Now);
                }
            }
    
            public void WriteLog(string str)
            {
                System.IO.StreamWriter writer = null;
                try
                {
                    //写入日志 
                    string path = string.Empty;
                    path = @"F:ErrorLogs";
                    //不存在则创建错误日志文件夹
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path += string.Format(@"{0}.txt", DateTime.Now.ToString("yyyy-MM-dd"));
    
                    writer = !System.IO.File.Exists(path) ? System.IO.File.CreateText(path) : System.IO.File.AppendText(path); //判断文件是否存在,如果不存在则创建,存在则添加
                    writer.WriteLine(str);
                    writer.WriteLine("********************************************************************************************");
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }
        }
    }

    这样就搞定了 

    那么,我们如何测试呢,可以更改IIS的回收时间

    在应用程序池中右键当前应用程序池,正在回收

    设置回收间隔  我设置1分钟

    这样可以再去看日志  

  • 相关阅读:
    菜鸟学自动化测试(三)—-selenium 命令
    菜鸟学自动化测试(二)—-selenium IDE 功能扩展
    用js实现自动打字动画效果
    hihoCoder #1015 : KMP算法
    hihoCoder #1014 : Trie树
    android相对布局中@id和@+id的区别(原理)
    android中动态给EditText获得焦点并弹起软键盘的方法详解
    网页版井字游戏(TicTacToe)人机对战的制作(附思路和源码)
    网页版番茄时钟的制作——Pomodoro Clock
    制作网页版简易计算器(Calculator)
  • 原文地址:https://www.cnblogs.com/LiChen19951127/p/10669178.html
Copyright © 2011-2022 走看看