如何使用Quartz.Net暂停/恢复作业

时间:2016-12-24 14:40:32

标签: c# asp.net scheduled-tasks quartz-scheduler quartz.net

我的项目中有8个工作。它们从Application_Start事件开始并永远重复。其中两个人正在使用同一个班级。每7秒一个工作,另一个工作每30秒工作一次。但他们在1点使用相同的方法。从xml文件中读取日期。

如果日期早于当前日期,则会更改会话ID并为其添加20分钟。会话ID不断变化,因为它们都试图同时进入和处理。 以下是我的需要:

1 - 输入方法。 2 - 如果要更改会话,请停止当前任务以外的任务。 3 - 更改会话并保存到xml。 4 - 重新启动或恢复所有被动任务。

private DateTimeOffset g_canlimacgetir = DateTimeOffset.UtcNow.AddSeconds(0);
private DateTimeOffset g_canliorangetir = DateTimeOffset.UtcNow.AddSeconds(45);
private void CanliOranlariGetir()
{
    try
    {
        ISchedulerFactory schfack = new StdSchedulerFactory();
        IScheduler scheduler = schfack.GetScheduler();
        IJobDetail jobdetay = JobBuilder.Create<CanliOranlar>()
            .WithIdentity("canliorangetir")
            .Build();
        ITrigger trigger = TriggerBuilder.Create()
            .WithSimpleSchedule(s => s.WithIntervalInSeconds(7).RepeatForever()).StartAt(g_canliorangetir).Build();
        scheduler.ScheduleJob(jobdetay, trigger);
        scheduler.Start();
    }
}
private void CanliMaclariGetir()
{
    try
    {
        ISchedulerFactory schfack = new StdSchedulerFactory();
        IScheduler scheduler = schfack.GetScheduler();
        IJobDetail jobdetay = JobBuilder.Create<CanliMaclar>()
            .WithIdentity("canlimacgetir")
            .Build();
        ITrigger trigger = TriggerBuilder.Create()
            .WithSimpleSchedule(s => s.WithIntervalInSeconds(30).RepeatForever()).StartAt(g_canlimacgetir).Build();
        scheduler.ScheduleJob(jobdetay, trigger);
        scheduler.Start();
    }
}

private Headers GetTheToken()
    {
        Headers h = new Headers();
        HttpWebResponse response = null;
        try
        {
            const string session_site = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(session_site);
            Uri uri = new Uri("");

            request.Method = "GET";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.ContentType = "text/html; charset=utf-8";
            request.ContentLength = 0;
            Cookie trackerID = new Cookie("trackerId", ConfigurationManager.AppSettings["TrackerID"].ToString()) { Domain = uri.Host };
            DateTime tarih = DateTime.Now;
            request.Timeout = 5000;
            ServicePointManager.Expect100Continue = false;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback = new
            RemoteCertificateValidationCallback(delegate { return true; });
            response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            response.Close();
            response.Dispose();
            reader.Close();
            reader.Dispose();
            DateTime sesstimeout = DateTime.Now.AddSeconds(1199);
            string getCookieHeader = response.Headers[HttpResponseHeader.SetCookie];
            char[] ayiricilar = { '=', ',', ';' };
            string[] parcalar = getCookieHeader.Split(ayiricilar);
            int ses = 0, rv = 0, xt = 0, xct = 0;
            if (parcalar.Length > 20)
            {
                h.session_timeout = sesstimeout;
                for (int i = 0; i < parcalar.Length; i++)
                {
                    if (parcalar[i] == "ASP.NET_SessionId") { h.sessionID = parcalar[i + 1]; ses = 1; }
                    else if (parcalar[i] == "__RequestVerificationToken") { h.request_verification = parcalar[i + 1]; rv = 1; }
                    else if (parcalar[i] == "XSRF-TOKEN") { h.xsrf_token = parcalar[i + 1]; xt = 1; }
                    else if (parcalar[i] == "XSRF-COOKIE-TOKEN") { h.xsrf_cookie_token = parcalar[i + 1]; xct = 1; }
                    if (ses == 1 && rv == 1 && xt == 1 && xct == 1) i = parcalar.Length;
                }
            }
            response.Close();
            response.Dispose();

            XmlDocument doc = new XmlDocument();
            XmlReader xmlReader = new XmlTextReader(HostingEnvironment.MapPath("~/xml/values.xml"));
            doc.Load(xmlReader);
            xmlReader.Close();
            XmlNodeList InfoNode = doc.SelectNodes("SessionInfo/Info");
            InfoNode[0].Attributes["SessionExpires"].Value = h.session_timeout.ToString();
            InfoNode[0].Attributes["SessionID"].Value = h.sessionID;
            InfoNode[0].Attributes["XSRF-TOKEN"].Value = h.xsrf_token;
            InfoNode[0].Attributes["XSRF-COOKIE-TOKEN"].Value = h.xsrf_cookie_token;
            InfoNode[0].Attributes["_requestVerification"].Value = h.request_verification.ToString();
            doc.Save(HostingEnvironment.MapPath("~/xml/values.xml"));
        }
        return h;
    }

1 个答案:

答案 0 :(得分:1)

对于您可以使用的所有工作

scheduler.PauseAll();

scheduler.ResumeAll();

但是如果你想停止并开始一项特定的工作,你可以使用它:

scheduler.PauseJob(jobdetay.Key);

scheduler.ResumeJob(jobdetay.Key);