Quartz.Net触发器同时触发多次

时间:2018-01-10 14:40:29

标签: c# iis quartz-scheduler quartz.net

我目前正在开发一个应用程序,我使用Quartz Scheduler调度作业以发送包含一些日志数据的电子邮件。

我目前在主机服务器上有9个触发器进行测试,它们从08:00开始,然后每小时一次,直到周一至周五16:00。

但是我的触发器不止一次触发有问题。

我尝试了一些不同的东西让它停下来,但我似乎无法做到。

我尝试过添加

[DisallowConcurrentExecution]

在scheduls调用的Job之上。 我尝试重置主机服务器上的IIS,尝试重置它。在我发布下一版本的代码之前。

但是当它们被触发时,确切地说。目前他们开火4次。我重新启动IIS后。他们首先开了一次,然后两次,然后他们开了四次并保持这个数量。

我设置的方式是使用 JobSchdul 类中的开始方法。

然后我在应用程序启动时将我需要的触发器和作业放在那里。

Global.asax中

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        var httpRunPath = HttpRuntime.AppDomainAppPath;
        var serverMapPath = Server.MapPath(".");

        JobScheduler scheduler = new JobScheduler();
        scheduler.Start(serverMapPath, httpRunPath);
    }

请注意,我有9个触发器的原因是出于测试目的。它们在主机服务器上运行,而我尝试在我的开发机器上修复问题。

JobScheduler.cs

public void Start(string serverPath, string httpPath)
    {

        try
        {
            var toDate = DateTime.Now.ToString();
            var fromDate = DateTime.Now.AddHours(-7).ToString();
            var to = "clm@thansen.dk";
            var serverMapPath = serverPath;
            var httpRunPath = httpPath;
            var workingDays = new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };

            scheduler.Start();

            IJobDetail emailerJob = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger = TriggerBuilder.Create()
                .WithIdentity("trigger", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(8, 0, workingDays))
                .Build();

            // Extra scheduls

            // 1
            IJobDetail emailerJob1 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job1", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger1 = TriggerBuilder.Create()
                .WithIdentity("trigger1", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(9, 0, workingDays))
                .Build();

            // 2
            IJobDetail emailerJob2 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job2", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger2 = TriggerBuilder.Create()
                .WithIdentity("trigger2", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(10, 0, workingDays))
                .Build();

            // 3
            IJobDetail emailerJob3 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job3", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger3 = TriggerBuilder.Create()
                .WithIdentity("trigger3", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(11, 0, workingDays))
                .Build();

            // 4
            IJobDetail emailerJob4 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job4", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger4 = TriggerBuilder.Create()
                .WithIdentity("trigger4", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(12, 0, workingDays))
                .Build();

            // 5
            IJobDetail emailerJob5 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job5", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger5 = TriggerBuilder.Create()
                .WithIdentity("trigger5", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(13, 0, workingDays))
                .Build();

            // 6
            IJobDetail emailerJob6 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job6", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger6 = TriggerBuilder.Create()
                .WithIdentity("trigger6", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(14, 0, workingDays))
                .Build();

            // 7
            IJobDetail emailerJob7 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job7", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger7 = TriggerBuilder.Create()
                .WithIdentity("trigger7", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(15, 0, workingDays))
                .Build();

            IJobDetail emailerJob8 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job8", "Information")
                .WithDescription("Starts on application start")
                .Build();

            ITrigger emailerTrigger8 = TriggerBuilder.Create()
                .WithIdentity("trigger8", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(15, 30, workingDays))
                .Build();

            IJobDetail emailerJob9 = JobBuilder.Create<BrokenIMGMailJob>()
                .WithIdentity("job9", "Information")
                .WithDescription("Extra job test")
                .Build();

            ITrigger emailerTrigger9 = TriggerBuilder.Create()
                .WithIdentity("trigger9", "Information")
                .UsingJobData("toDate", toDate)
                .UsingJobData("fromDate", fromDate)
                .UsingJobData("to", to)
                .UsingJobData("serverMapPath", serverMapPath)
                .UsingJobData("httpRunPath", httpRunPath)
                .StartNow()
                .WithSchedule(CronScheduleBuilder.AtHourAndMinuteOnGivenDaysOfWeek(15, 40, workingDays))
                .Build();

            scheduler.ScheduleJob(emailerJob, emailerTrigger);
            scheduler.ScheduleJob(emailerJob1, emailerTrigger1);
            scheduler.ScheduleJob(emailerJob2, emailerTrigger2);
            scheduler.ScheduleJob(emailerJob3, emailerTrigger3);
            scheduler.ScheduleJob(emailerJob4, emailerTrigger4);
            scheduler.ScheduleJob(emailerJob5, emailerTrigger5);
            scheduler.ScheduleJob(emailerJob6, emailerTrigger6);
            scheduler.ScheduleJob(emailerJob7, emailerTrigger7);
            scheduler.ScheduleJob(emailerJob8, emailerTrigger8);
            scheduler.ScheduleJob(emailerJob9, emailerTrigger9);

        }
        catch (Exception ex)
        {
            logging.WriteLog(1, "JobScheduler", "Start", "Start trigger", $"EXCEPTION MESSAGE: {ex.Message} | EXCEPTION INNER: {ex.InnerException}", LogType.Exception, "");
        }
    }

JobClass.cs - 其中包含我的其他工作类

 [DisallowConcurrentExecution]
public class BrokenIMGMailJob : IJob
{
    LoggingRepository logging = new LoggingRepository();
    Email _email = new Email();

    public async Task Execute(IJobExecutionContext context)
    {
        try
        {
            JobKey key = context.JobDetail.Key;
            JobDataMap dataMap = context.MergedJobDataMap;


            var toDate = Convert.ToDateTime(dataMap.GetString("toDate"));
            var fromDate = Convert.ToDateTime(dataMap.GetString("fromDate"));
            var tmpTo = dataMap.GetString("to");
            string[] toArr;
            string to;
            string httpPath;

            // Gets the path for where the site is being run
            var serverMapPath = dataMap.GetString("serverMapPath");
            var httpRunPath = dataMap.GetString("httpRunPath");

            if (string.IsNullOrEmpty(serverMapPath))
            {
                httpPath = httpRunPath;
            }
            else
            {
                httpPath = serverMapPath;
            }

            // Regex:
            // (?<=/ ).* : Gets the last parts of the line
            //.+?(?= /) : Gets everything from the start of the line till  " / "
            var regex = new Regex(".+?(?= /)|(?<=/ ).*");

            // List for either not found images or bad images
            var notFoundList = new List<string>();
            var badList = new List<string>();

            // 1) Retreive 7 hours worth of Images
            var logs = logging.getDaysErrorLogs(3, fromDate, toDate); // fromDate: DateTime.Now.AddHours(-7) toDate:DateTime.Now

            // 2) Sort their names and take only the image name "xxxx1.jpg/JPG"
            foreach (var log in logs)
            {
                //(ShowImage|ProcessImage| / )
                var matches = regex.Matches(log.ActionName);
                var fileName = matches[1].ToString();

                if (log.Message == "Image not found")
                {
                    notFoundList.Add(fileName);
                }
                if (log.Message == "Bad image")
                {
                    badList.Add(fileName);
                }
            }

            // 3) Count how many of each instance exists
            var notFoundGroup = notFoundList.GroupBy(x => x).OrderByDescending(x => x.Count());
            var badGroup = badList.GroupBy(x => x).OrderByDescending(x => x.Count());
            // 4) Set up mail with the information nicely displayed
            var msg = $"<div style='font-family:Arial, Helvetica, sans-serif; font-size:small'>";
            msg += $"Current URL: {httpPath}";
            msg += $"<h2>Beskadiget billeder</h2> ";
            msg += $"Antal forekomster: {badList.Count()}<br/>";
            msg += $"Forekomster i tidsrummet <b>{fromDate.ToString("dd MMM HH:mm:ss")}</b> til <b>{toDate.ToString("dd MMM HH:mm:ss")}</b><br/>";
            msg += $"<b>Filnavn - Antal forkomster</b><br/>";
            msg += $"-------------------------------------<br/>";
            foreach (var file in notFoundGroup)
            {
                msg += $"{file.Key} - ({file.Count()})<br/>";
            }
            msg += $"-------------------------------------<br/>";

            msg += $"<br/><h2>Ikke fundet billeder</h2>";
            msg += $"Antal forekomster: {notFoundList.Count()}<br/>";
            msg += $"Forekomster i tidsrummet <b>{fromDate.ToString("dd MMM HH:mm:ss")}</b> til <b>{toDate.ToString("dd MMM HH:mm:ss")}</b><br/>";
            msg += $"<b>Filnavn - Antal forkomster</b><br/>";
            msg += $"-------------------------------------<br/>";
            foreach (var file in badGroup)
            {
                msg += $"{file.Key} - ({file.Count()})<br/>";
            }
            msg += $"-------------------------------------<br/>";
            msg += $"<p>Denne besked blev sent, {DateTime.Now.ToString("U")}</p>";
            msg += $"</div>";

            if (tmpTo.Contains(";"))
            {
                toArr = Regex.Split(tmpTo, ";");
                _email.SendEmail(toArr, "Bad and Not found Images", msg, true);
            }
            else
            {
                to = tmpTo;
                _email.SendEmail(to, "Bad and Not found Images", msg, true);
            }

        }
        catch (Exception ex)
        {

            logging.WriteLog(1, "JobClass", "BrokenIMGMailJob","Send broken images mail", $"EXCEPTION MESSAGE: {ex.Message} | EXCEPTION INNER: {ex.InnerException}", LogType.Exception, "");
        }

    }
}

0 个答案:

没有答案