Quartz.net does not always work

独自空忆成欢 提交于 2019-12-10 11:56:23

问题


I am using the simple scheduler of the Quartz.net library. I have 7 different triggers. The sequence is like this.

First: every 120 minutes.
Second: every 12 hours.
Third: every 5 minutes.
Fourth: every 3 minutes.
Fifth: every 29 seconds.
Sixth: every 10 seconds.
Seventh: every 5 seconds.

It's a bit complicated as you see it. I have a problem like this. First Trigger needs to work every two hours. But after a few runs, it stops by itself. It does not work at all. The others are working and they are OK. I'm sorry for my bad english.

    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:


As i mentioned in my comments, you use currently as many schedulers as triggers you have. Each scheduler will use his own Threadpool with 10 threads and it could be possible that your Threads gets blocked by each other due to the fact that there are no more Threads to execute your jobs. You need only 1 Scheduler for all your Triggers.

You can change your code to create just one Scheduler

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);
    }
}

I would also recommend that you rework the whole code to not repeat yourself. Your methods do almost the same. You can use a method to create your Trigger to make it more readable:

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

The rest to optimize the code is up to you ;)



来源:https://stackoverflow.com/questions/42623667/quartz-net-does-not-always-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!