Fluentscheduler is used to develop repetitive tasks that can be executed regularly

Recently, there is a need to develop a program to regularly detect whether some computers are online, and if they are online, they can connect successfully and then perform some other operations.

For this kind of timing to execute a certain task, at first I wanted to use the previously developed Windows service. But this kind of service deployment is troublesome, and it is easy to make mistakes and hang up. Finally, after querying, I decided to use the relatively simple and easy-to-use fluentscheduler.

The solution is to encapsulate the core code as a DLL, create a new application and deploy it on IIS, and use Global.

The most important thing is that the service will be recycled by IIS, so when Application_End ends the program, you must visit yourself to ensure that it will not be recycled by IIS, so as to ensure 24-hour uninterrupted service without interruption, remember!

 

IIS preload

After the application pool is recycled, if no one visits the website, w3wp will not start, which means that our scheduled tasks will not start, so we need to simulate visiting the website after the application pool is recycled, we This problem can be solved by writing a timing program to visit the website every second, but it is not necessary to write an additional program to solve this problem, because Microsoft has provided a function of website preloading, every time the application When the pool is recycled, the system will start a process to simulate visiting the website again

The specific settings are as follows:

global.asax

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace RetrunPassword
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            RepeatbyTimer.StartUp();//启动定时更新归还密码失败的电脑

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
               
        }
    }
}

Timing task class and method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentScheduler;
using System.Configuration;
using System.Diagnostics;

namespace RetrunPassword
{
    public class RepeatbyTimer
    {    /// <summary>
         /// 启动定时任务
         /// </summary>
        public static void StartUp()
        {
            JobManager.Initialize(new ApiJobFactory());
        }

        /// <summary>
        /// 停止定时任务
        /// </summary>
        public static void Stop()
        {
            JobManager.Stop();
        }
    }
    /// <summary>
    /// 待处理的作业工厂,在构造函数中设置好各个Job的执行计划。
    /// 参考【https://github.com/fluentscheduler/FluentScheduler】
    /// </summary>
    public class ApiJobFactory : Registry
    {
        public ApiJobFactory()
        {
            // 立即执行每两秒一次的计划任务。(指定一个时间间隔运行,根据自己需求,可以是秒、分、时、天、月、年等。)
            Schedule<Demo>().ToRunNow().AndEvery(1).Minutes();

             延迟一个指定时间间隔执行一次计划任务。(当然,这个间隔依然可以是秒、分、时、天、月、年等。)
            //Schedule<Demo>().ToRunOnceIn(5).Minutes();

             在一个指定时间执行计划任务(最常用。这里是在每天的下午 1:10 分执行)
            //Schedule(() => Trace.WriteLine("It's 1:10 PM now.")).ToRunEvery(1).Days().At(13, 10);

             立即执行一个在每月的星期一 3:00 的计划任务(可以看出来这个一个比较复杂点的时间,它意思是它也能做到!)
            //Schedule<Demo>().ToRunNow().AndEvery(1).Months().OnTheFirst(DayOfWeek.Monday).At(3, 0);

             在同一个计划中执行两个(多个)任务
            //Schedule<Demo>().AndThen<Demo>().ToRunNow().AndEvery(5).Minutes();
        }
    }

    public class Demo : IJob
    {
        public static string connectionString = ConfigurationManager.ConnectionStrings["BPMCEDATAConnectionString"].ConnectionString;
        void IJob.Execute()
        {

            Trace.WriteLine("开始定时任务了,现在时间是:" + DateTime.Now);
            DataClasses1DataContext dc = new DataClasses1DataContext();

            var rcd = from ITAccountLists_M in dc.ITAccountLists_M where ITAccountLists_M.Flag == 1 select ITAccountLists_M.CompuAddress;
            foreach (var r in rcd)
            {

                RemotePasswordMangement.ChangePassword.ReturnPass(r.ToString(), connectionString);
                // ReturnPass(r.ToString());
            }
        }
    }
}

 

Guess you like

Origin blog.csdn.net/suixufeng/article/details/82682950