Quartz.net并不总是有效

时间:2017-03-06 10:50:56

标签: c# asp.net webforms quartz.net quartz.net-2.0

我正在使用Quartz.net库的简单调度程序。 我有7种不同的触发器。顺序是这样的。

第一:每120分钟一次 第二:每12小时一次 第三:每5分钟一次 第四:每3分钟一次 第五:每29秒 第六:每10秒钟 第七:每5秒钟。

你看到它有点复杂。我有这样的问题。 First Trigger需要每两个小时工作一次。但经过几次运行后,它会自行停止。它根本不起作用。其他人正在工作,他们没事。 我很抱歉我的英语不好。

    private int bultengetir = 120; // min
    private int baslayanmacsil = 29; //sec
    private int kuponsonuclandir = 5;//5 min
    private int canliorangetir = 10; //sec
    private int canlimacgetir = 7; //sec
    private int canlimacsil = 12;//hour
    private int videoeslestir = 3; //min

    private DateTimeOffset g_bultengetir = DateTimeOffset.UtcNow.AddMinutes(0);//10
    private DateTimeOffset g_baslayanmacsil = DateTimeOffset.UtcNow.AddMinutes(5);//5
    private DateTimeOffset g_canlimacgetir = DateTimeOffset.UtcNow.AddMinutes(10);//10
    private DateTimeOffset g_canliorangetir = DateTimeOffset.UtcNow.AddMinutes(11);//55
    private DateTimeOffset g_canlimacsil = DateTimeOffset.UtcNow.AddMinutes(12);//5
    private DateTimeOffset g_kuponsonuclandir = DateTimeOffset.UtcNow.AddMinutes(15);//15
    private DateTimeOffset g_videoeslestir = DateTimeOffset.UtcNow.AddMinutes(15);//7

    public void RunJobs()
    {
        CanliMaclariGetir();
        CanliOranlariGetir();
        BultenGetir();
        BaslayanMaclariSil();
        CanliMaclariSil();
        KuponlariSonuclandir();
        VideoEslestir();
    }
    private void BultenGetir()
    {
        try
        {
            ISchedulerFactory schfack = new StdSchedulerFactory();
            IScheduler scheduler = schfack.GetScheduler();

            IJobDetail jobdetay = JobBuilder.Create<BultenGetir>()
                .WithIdentity("bultengetir")
                .Build();
            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(s => s.WithIntervalInMinutes(bultengetir).RepeatForever())
                .StartAt(g_bultengetir)
                .Build();
            scheduler.ScheduleJob(jobdetay, trigger);
            scheduler.Start();
            Log log = new Log()
            {
                Name = "BultenGetir Görev Emri",
                Description = "BultenGetir Görev Emri Verildi.",
                Start = DateTime.Now,
                Finish = DateTime.Now,
                TotalMilliSecond = 0,
                Type = 6
            };
            DbWork db = new DbWork();
            db.LogEkle(log);
        }
        catch (Exception ex)
        {
            string h_mesaj = ex.Message.ToString();
            string icerik = "";
            if (ex.InnerException != null) { icerik = ex.InnerException.ToString(); }
            string h_yer = ex.StackTrace.ToString();
            dal.HataEkle("", "Gorevler Katmanı > Gorev_Zamanlayici.cs", "BultenGetir()", DateTime.Now, h_mesaj, icerik, h_yer);
        }

    }
    private void BaslayanMaclariSil()
    {
        try
        {
            ISchedulerFactory schfack = new StdSchedulerFactory();
            IScheduler scheduler = schfack.GetScheduler();

            IJobDetail jobdetay = JobBuilder.Create<BaslayanMaclariSil>()
                .WithIdentity("baslayansil")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(s => s.WithIntervalInSeconds(baslayanmacsil).RepeatForever())
                .StartAt(g_baslayanmacsil)
                .Build();
            scheduler.ScheduleJob(jobdetay, trigger);
            scheduler.Start();

            Log log = new Log()
            {
                Name = "BaslayanMaclariSil Görev Emri",
                Description = "BaslayanMaclariSil Görev Emri Verildi.",
                Start = DateTime.Now,
                Finish = DateTime.Now,
                TotalMilliSecond = 0,
                Type = 6
            };
            DbWork db = new DbWork();
            db.LogEkle(log);
        }
        catch (Exception ex)
        {
            string h_mesaj = ex.Message.ToString();
            string icerik = "";
            if (ex.InnerException != null) { icerik = ex.InnerException.ToString(); }
            string h_yer = ex.StackTrace.ToString();
            dal.HataEkle("", "Gorevler Katmanı > Gorev_Zamanlayici.cs", "BaslayanMaclariSil()", DateTime.Now, h_mesaj, icerik, h_yer);
        }
    }
    private void KuponlariSonuclandir()
    {
        try
        {
            ISchedulerFactory schfack = new StdSchedulerFactory();
            IScheduler scheduler = schfack.GetScheduler();

            IJobDetail jobdetay = JobBuilder.Create<KuponlariSonuclandir>()
                .WithIdentity("kuponsonuclandir")
                .Build();
            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(s => s.WithIntervalInMinutes(kuponsonuclandir).RepeatForever())
                .StartAt(g_kuponsonuclandir)
                .Build();
            scheduler.ScheduleJob(jobdetay, trigger);
            scheduler.Start();

            Log log = new Log()
            {
                Name = "KuponlariSonuclandir Görev Emri",
                Description = "KuponlariSonuclandir Görev Emri Verildi.",
                Start = DateTime.Now,
                Finish = DateTime.Now,
                TotalMilliSecond = 0,
                Type = 6
            };
            DbWork db = new DbWork();
            db.LogEkle(log);
        }
        catch (Exception ex)
        {
            string h_mesaj = ex.Message.ToString();
            string icerik = "";
            if (ex.InnerException != null) { icerik = ex.InnerException.ToString(); }
            string h_yer = ex.StackTrace.ToString();
            dal.HataEkle("", "Gorevler Katmanı > Gorev_Zamanlayici.cs", "KuponlariSonuclandir()", DateTime.Now, h_mesaj, icerik, h_yer);
        }
    }
    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(canliorangetir).RepeatForever()).StartAt(g_canliorangetir).Build();
            scheduler.ScheduleJob(jobdetay, trigger);
            scheduler.Start();

            Log log = new Log()
            {
                Name = "CanliOranlariGetir Görev Emri",
                Description = "CanliOranlariGetir Görev Emri Verildi.",
                Start = DateTime.Now,
                Finish = DateTime.Now,
                TotalMilliSecond = 0,
                Type = 6
            };
            DbWork db = new DbWork();
            db.LogEkle(log);
        }
        catch (Exception ex)
        {
            string h_mesaj = ex.Message.ToString();
            string icerik = "";
            if (ex.InnerException != null) { icerik = ex.InnerException.ToString(); }
            string h_yer = ex.StackTrace.ToString();
            dal.HataEkle("", "Gorevler Katmanı > Gorev_Zamanlayici.cs", "CanliOranlariGetir()", DateTime.Now, h_mesaj, icerik, h_yer);
        }
    }
    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(canlimacgetir).RepeatForever()).StartAt(g_canlimacgetir).Build();
            scheduler.ScheduleJob(jobdetay, trigger);
            scheduler.Start();
            Log log = new Log()
            {
                Name = "CanliMaclariGetir Görev Emri",
                Description = "CanliMaclariGetir Görev Emri Verildi.",
                Start = DateTime.Now,
                Finish = DateTime.Now,
                TotalMilliSecond = 0,
                Type = 6
            };
            DbWork db = new DbWork();
            db.LogEkle(log);
        }
        catch (Exception ex)
        {
            string h_mesaj = ex.Message.ToString();
            string icerik = "";
            if (ex.InnerException != null) { icerik = ex.InnerException.ToString(); }
            string h_yer = ex.StackTrace.ToString();
            dal.HataEkle("", "Gorevler Katmanı > Gorev_Zamanlayici.cs", "CanliMaclariGetir()", DateTime.Now, h_mesaj, icerik, h_yer);
        }
    }
    private void CanliMaclariSil()
    {
        try
        {
            ISchedulerFactory schfack = new StdSchedulerFactory();
            IScheduler scheduler = schfack.GetScheduler();

            IJobDetail jobdetay = JobBuilder.Create<CanliSil>()
                .WithIdentity("canlimacsil")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(s => s.WithIntervalInHours(canlimacsil).RepeatForever())
                .StartAt(g_canlimacsil)
                .Build();
            scheduler.ScheduleJob(jobdetay, trigger);
            scheduler.Start();

            Log log = new Log()
            {
                Name = "CanliMaclariSil Görev Emri",
                Description = "CanliMaclariSil Görev Emri Verildi.",
                Start = DateTime.Now,
                Finish = DateTime.Now,
                TotalMilliSecond = 0,
                Type = 6
            };
            DbWork db = new DbWork();
            db.LogEkle(log);
        }
        catch (Exception ex)
        {
            string h_mesaj = ex.Message.ToString();
            string icerik = "";
            if (ex.InnerException != null) { icerik = ex.InnerException.ToString(); }
            string h_yer = ex.StackTrace.ToString();
            dal.HataEkle("", "Gorevler Katmanı > Gorev_Zamanlayici.cs", "CanliMaclariSil()", DateTime.Now, h_mesaj, icerik, h_yer);
        }
    }
    private void VideoEslestir()
    {
        try
        {
            ISchedulerFactory schfack = new StdSchedulerFactory();
            IScheduler scheduler = schfack.GetScheduler();

            IJobDetail jobdetay = JobBuilder.Create<VideoEslestir>()
                .WithIdentity("videoeslestir")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(s => s.WithIntervalInMinutes(videoeslestir).RepeatForever())
                .StartAt(g_videoeslestir)
                .Build();
            scheduler.ScheduleJob(jobdetay, trigger);
            scheduler.Start();

            Log log = new Log()
            {
                Name = "VideoEslestir Görev Emri",
                Description = "VideoEslestir Görev Emri Verildi.",
                Start = DateTime.Now,
                Finish = DateTime.Now,
                TotalMilliSecond = 0,
                Type = 6
            };
            DbWork db = new DbWork();
            db.LogEkle(log);
        }
        catch (Exception ex)
        {
            string h_mesaj = ex.Message.ToString();
            string icerik = "";
            if (ex.InnerException != null) { icerik = ex.InnerException.ToString(); }
            string h_yer = ex.StackTrace.ToString();
            dal.HataEkle("", "Gorevler Katmanı > Gorev_Zamanlayici.cs", "videoeslestir()", DateTime.Now, h_mesaj, icerik, h_yer);
        }
    }

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的,您目前使用的调度程序与您拥有的触发器一样多。每个调度程序将使用自己的Threadpool和10个线程,由于没有更多的线程可以执行您的作业,因此您的线程可能会被彼此阻塞。所有触发器只需要1个调度程序。

您可以更改代码以仅创建一个调度程序

public void RunJobs()
{
    ISchedulerFactory schfack = new StdSchedulerFactory();
    IScheduler scheduler = schfack.GetScheduler();

    CanliMaclariGetir(scheduler);
    // ...your other jobs

    // after scheduling all of your jobs, start the scheduler
    scheduler.Start();
}

private void CanliMaclariGetir(IScheduler scheduler)
{
    try
    {
        IJobDetail jobdetay = JobBuilder.Create<CanliMaclar>()
            .WithIdentity("canlimacgetir")
            .Build();
        ITrigger trigger = TriggerBuilder.Create()
            .WithSimpleSchedule(s => s.WithIntervalInSeconds(canlimacgetir).RepeatForever()).StartAt(g_canlimacgetir).Build();
        scheduler.ScheduleJob(jobdetay, trigger);

        Log log = new Log()
        {
            Name = "CanliMaclariGetir Görev Emri",
            Description = "CanliMaclariGetir Görev Emri Verildi.",
            Start = DateTime.Now,
            Finish = DateTime.Now,
            TotalMilliSecond = 0,
            Type = 6
        };

        DbWork db = new DbWork();
        db.LogEkle(log);
    }
    catch (Exception ex)
    {
        string h_mesaj = ex.Message.ToString();
        string icerik = "";
        if (ex.InnerException != null) { icerik = ex.InnerException.ToString(); }
        string h_yer = ex.StackTrace.ToString();
        dal.HataEkle("", "Gorevler Katmanı > Gorev_Zamanlayici.cs", "CanliMaclariGetir()", DateTime.Now, h_mesaj, icerik, h_yer);
    }
}

我还建议您重写整个代码,不要重复自己。你的方法几乎一样。您可以使用方法创建Trigger以使其更具可读性:

public ITrigger CreateTrigger(TimeSpan timeSpan, string name = "")
{
    return TriggerBuilder.Create()
        .StartNow()
        .WithDescription(name)
        .WithSimpleSchedule(x => x
            .WithInterval(timeSpan)
            .RepeatForever())
        .Build();
}

优化代码的其余部分取决于您;)