0

What happens when quartz sheduler becomes out of scope? Will Garbage Collector kills it or it will keep running? Samle code:

namespace Quartz_test
{
    public class SimpleJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("hello there");
        }
    }

    class Program
    {
        static void ScheduleTask()
        {
            // construct a scheduler factory
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            // get a scheduler
            IScheduler scheduler = schedFact.GetScheduler();
            scheduler.Start();
            IJobDetail job = JobBuilder.Create<SimpleJob>()
                .WithIdentity("myJob", "group1")
                .Build();
            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(3))
                .Build();
            scheduler.ScheduleJob(job, trigger);
        }

        static void Main(string[] args)
        {
            ScheduleTask();
            //link to scheduler is lost here
            //how to stop SimpleJob execution?
            //Console.ReadLine();
        }
    }

this code outputs:

hello there
hello there
hello there

All seems OK, but why program never finish even if I comment last Console.ReadLine();?

And last question: how to stop IJob execution if I lost all links to my sceduler

tommybee
  • 2,409
  • 1
  • 20
  • 23
Yaros
  • 75
  • 8

1 Answers1

1

There is a shutdown method in the library.

syntax is

void Shutdown( bool waitForJobsToComplete);

You can use it.

IScheduler scheduler = schedFact.GetDefaultScheduler();
scheduler.Shutdown(false);

You can also use either UnscheduleJob or DeleteJob method.

scheduler.unscheduleJob(new TriggerKey("myJob", "group1"));

or

scheduler.deleteJob(new JobKey("myJob", "group1"));

Here is your program with debugging information. I borrowed code from here

using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Quartz_test
{
    public class SimpleJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("hello there");
        }
    }

    class Program
    {

        private static void GetAllJobs(IScheduler scheduler)
        {
            IList<string> jobGroups = scheduler.GetJobGroupNames();
            // IList<string> triggerGroups = scheduler.GetTriggerGroupNames();
            Console.Write("START ===== \n is start? " + scheduler.IsStarted);
            Console.WriteLine("is stop? " + scheduler.IsShutdown);
            foreach (string group in jobGroups)
            {
                var groupMatcher = GroupMatcher<JobKey>.GroupContains(group);
                var jobKeys = scheduler.GetJobKeys(groupMatcher);
                Console.WriteLine();
                foreach (var jobKey in jobKeys)
                {
                    var detail = scheduler.GetJobDetail(jobKey);
                    var triggers = scheduler.GetTriggersOfJob(jobKey);
                    foreach (ITrigger trigger in triggers)
                    {
                        Console.WriteLine("group : " + group);
                        Console.WriteLine("name : " + jobKey.Name);
                        Console.WriteLine("desc : " + detail.Description);
                        Console.WriteLine("trg name : " + trigger.Key.Name);
                        Console.WriteLine("trg group : " + trigger.Key.Group);
                        Console.WriteLine("trg type : " + trigger.GetType().Name);
                        Console.WriteLine("trg state : " + scheduler.GetTriggerState(trigger.Key));
                        DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc();
                        if (nextFireTime.HasValue)
                        {
                            Console.WriteLine("next trigger? : " + nextFireTime.Value.LocalDateTime.ToString());
                        }

                        DateTimeOffset? previousFireTime = trigger.GetPreviousFireTimeUtc();
                        if (previousFireTime.HasValue)
                        {
                            Console.WriteLine("previous trigger? : " + previousFireTime.Value.LocalDateTime.ToString());
                        }
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine("===== END \n");
        }

        static void ScheduleTask() 
        {
            IScheduler scheduler = null;
            IJobDetail job = null;
            ITrigger trigger = null;

            try
            {
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();
                // get a scheduler
                scheduler = schedFact.GetScheduler();
                scheduler.Start();

                job = JobBuilder.Create<SimpleJob>()
                    .WithIdentity("myJob", "group1")
                    .Build();
                trigger = TriggerBuilder.Create()
                    .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(3))
                    .Build();

                scheduler.ScheduleJob(job, trigger);

                GetAllJobs(scheduler);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException.ToString());
            }
            finally
            {
                GetAllJobs(scheduler);

                TimeSpan spn = TimeSpan.FromSeconds(100L);
                Thread.Sleep(spn);


                scheduler.Shutdown(true);

            }

        }

        static void Main(string[] args)
        {
            ScheduleTask();
            //link to scheduler is lost here
            //how to stop SimpleJob execution?
            //Console.ReadLine();
        }
    }
}

Best regards, Yaros

tommybee
  • 2,409
  • 1
  • 20
  • 23
  • Sorry, but it does not make sence. I can't stop scheduler this way from main because haven't link to it. And why program does not finish after all scheduled jobs completed? – Yaros Aug 23 '17 at 15:49
  • I've added some debugging information. Could you please tell me the result after the code executing? – tommybee Aug 24 '17 at 08:15
  • Sorry for slow reply. Here it is: – Yaros Aug 24 '17 at 15:19
  • [link](https://pastebin.com/i4ycwqV7). All go as expected, debugger says execution flow does not return to Main() until Thread.Sleep() finish. Scheduller shutted down in `finally` block so program stops after Main() ends. But still no answers :( – Yaros Aug 24 '17 at 15:29
  • What version of quartz module you use and also the version of visual studio? Mine is 2.6, the latest one and 2012 as a version of visual studio. I don't have any problem with the code. – tommybee Aug 25 '17 at 00:24