Search code examples
c#quartz-scheduleraspnetboilerplate

Get list of background jobs in IQuartzScheduleJobManager


We schedule background jobs in method PostInitialize of WebModule like this:

IocManager.Resolve<IQuartzScheduleJobManager>().ScheduleAsync<WorkFlowDetailNotificationWorker>(job =>
{
    job.WithIdentity("SendNotificationForRemainGardeshKarDetail", "AutoNotification")
       .WithDescription("WorkFlowsNotification");
}, trigger =>
{
    trigger.StartNow().WithSchedule(CronScheduleBuilder.CronSchedule("0 0/60 8-19 * * ?")).Build();
});

How can we get the list of registered background jobs in Application Service, so that we can manipulate their properties?

Our project startup template is ASP.NET Boilerplate, AngularJS and EntityFramework.


Solution

  • You can get list of all the jobs registered to Quartz in Asp.Net Boilerplate framework like below;

        public class MySampleAppService
        {
                private readonly IAbpQuartzConfiguration _abpQuartzConfiguration;
    
                public MySampleAppService(IAbpQuartzConfiguration abpQuartzConfiguration)
                {
                   _abpQuartzConfiguration = abpQuartzConfiguration
                }
    
                private void ListAllJobs()
                {
                    var scheduler = _abpQuartzConfiguration.Scheduler;
                    var jobGroups = scheduler.GetJobGroupNames();
                    foreach (string group in jobGroups)
                    {
                        var groupMatcher = GroupMatcher<JobKey>.GroupContains(group);
                        var jobKeys = scheduler.GetJobKeys(groupMatcher);
                        foreach (var jobKey in jobKeys)
                        {
                            var detail = scheduler.GetJobDetail(jobKey);
                            var triggers = scheduler.GetTriggersOfJob(jobKey);
                            foreach (ITrigger trigger in triggers)
                            {
                                Console.WriteLine(group);
                                Console.WriteLine(jobKey.Name);
                                Console.WriteLine(detail.Description);
                                Console.WriteLine(trigger.Key.Name);
                                Console.WriteLine(trigger.Key.Group);
                                Console.WriteLine(trigger.GetType().Name);
                                Console.WriteLine(scheduler.GetTriggerState(trigger.Key));
    
                                var nextFireTime = trigger.GetNextFireTimeUtc();
                                if (nextFireTime.HasValue)
                                {
                                    Console.WriteLine(nextFireTime.Value.LocalDateTime.ToString(CultureInfo.InvariantCulture));
                                }
    
                                var previousFireTime = trigger.GetPreviousFireTimeUtc();
                                if (previousFireTime.HasValue)
                                {
                                    Console.WriteLine(previousFireTime.Value.LocalDateTime.ToString(CultureInfo.InvariantCulture));
                                }
                            }
                        }
                    }
                }
    
          //...
    }