Quartz.net does not always work
Asked Answered
B

1

1

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);
        }
    }
Beggs answered 6/3, 2017 at 10:50 Comment(12)
Post the CRON expressions that you are usingPosy
Quartz.net runs in the AppPool of the website. If there is no traffic the AppPool will be put to sleep and the triggers won't be fired. When the pool starts up again the timer are reset. Or something else is causing a recycle of the AppPool, like changes to the bin folderMuzzleloader
Ok, I add source code.Gentlemanly
VDWWD thank you for interest. I am warning you by submitting your own server HttpWebRequest. At this point, ApplicationPool is always running. It does not stop.Gentlemanly
@GökhanAtilgan do you really need 7 different schedulers for your jobs? Normally you create only one and schedule all your jobs there. Btw. Quartz swallows all exceptions that are thrown during execution. Are you sure that no exceptions are thrown?Armendariz
@Armendariz Actually there were 10 schedules. But I have grouped the ones that will start at the same moment. For errors, run and forget to say Task.Factory.StartNew (() => LegendsGetir ()); I use command every trigger.Gentlemanly
@GökhanAtilgan But why so much Schedulers? You only need one. Each scheduler will come with a TreadPool of 10 Threads. It could be possible that these ThreadPools block each other due to the fact that there are no more Threads to execute your jobs.Armendariz
@Armendariz Every scheduler has to work at certain intervals. For example, a list of live matches needs to be drawn from another server in 5 seconds. In the same way, the proportions of live matches need to be drawn in 10 seconds. Normal, non-live matches and odds must be drawn in 2 hours. You also need to pick up the results of the match every 5 minutes and finalize the betting coupons.Gentlemanly
@GökhanAtilgan it seems that you misunderstood the difference between a Trigger and a Scheduler. All your points you have stated are controlled with Triggers, each can have a different interval and can trigger a different Job. But for all these, you need only One Scheduler. Each Scheduler can fire 10 different Trigger by default (you can raise this limit to the bounds of your machine). But you need still just one scheduler.Armendariz
@Armendariz Sorry i did not know that. How can I do it? Could you give an example?Gentlemanly
@GökhanAtilgan There is no need to excuse Abi ;) Just create the Scheduler in your RunJobs method and give it your other methods as parameter private void BultenGetir(IScheduler scheduler)Armendariz
@Armendariz :) Eyvallah, But I guess you need to give a complete example. Can you arrange for me two of the tasks in the source code I gave above? I add the rest.Gentlemanly
A
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 ;)

Armendariz answered 6/3, 2017 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.