static void Main(string[] args) { // Create RAMJobStore instance DirectSchedulerFactory.Instance.CreateVolatileScheduler(5); ISchedulerFactory factory = DirectSchedulerFactory.Instance; // Get scheduler and add object IScheduler scheduler = factory.GetScheduler(); StringBuilder history = new StringBuilder("Runtime History: "); scheduler.Context.Add("History", history); JobKey firstJobKey = JobKey.Create("FirstJob", "Pipeline"); JobKey secondJobKey = JobKey.Create("SecondJob", "Pipeline"); JobKey thirdJobKey = JobKey.Create("ThirdJob", "Pipeline"); // Create job and trigger IJobDetail firstJob = JobBuilder.Create <FirstJob>() .WithIdentity(firstJobKey) //.StoreDurably(true) .Build(); IJobDetail secondJob = JobBuilder.Create <SecondJob>() .WithIdentity(secondJobKey) .StoreDurably(true) .Build(); IJobDetail thirdJob = JobBuilder.Create <ThirdJob>() .WithIdentity(thirdJobKey) .StoreDurably(true) .Build(); ITrigger firstJobTrigger = TriggerBuilder.Create() .WithIdentity("Trigger", "Pipeline") .WithSimpleSchedule(x => x .WithMisfireHandlingInstructionFireNow() .WithIntervalInSeconds(5) .RepeatForever()) .Build(); JobChainingJobListener listener = new JobChainingJobListener("Pipeline Chain"); listener.AddJobChainLink(firstJobKey, secondJobKey); listener.AddJobChainLink(secondJobKey, thirdJobKey); scheduler.ListenerManager.AddJobListener(listener, GroupMatcher <JobKey> .GroupEquals("Pipeline")); // Run it all in chain scheduler.Start(); scheduler.ScheduleJob(firstJob, firstJobTrigger); scheduler.AddJob(secondJob, false, true); scheduler.AddJob(thirdJob, false, true); Console.ReadLine(); scheduler.Shutdown(); Console.WriteLine("Scheduler shutdown."); Console.WriteLine(history); Console.ReadLine(); }
public static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration().WriteTo.ColoredConsole().CreateLogger(); var host = new HostBuilder() .ConfigureAppConfiguration(x => x.AddJsonFile("appsettings.json")) .ConfigureServices((services) => { services.AddJobService <ConsolePrintJob>((job, trigger) => { job.WithIdentity("ConsolePrintJob").WithDescription("Simple job"); trigger.StartNow().WithSimpleSchedule((x) => x.WithIntervalInSeconds(5).RepeatForever()); }); services.AddJobService <LogWriteJob>((job, trigger) => { job.WithIdentity("LogWriteJob").WithDescription("Simple job"); trigger.StartNow().WithSimpleSchedule((x) => x.WithIntervalInSeconds(2).RepeatForever()); }); services.AddJobService <Job1>((job) => { job.WithIdentity("Job1"); }); }) // You can use the other `UserQuartz` 2 methods // if you only want to configure either the scheduler factory // or the scheduler instance .UseQuartz( (context, config) => { // Here you can further customize options passed down to the StdSchedulerFactory config.Set("quartz.threadPool.threadCount", Environment.ProcessorCount.ToString()); }, (context, provider, scheduler) => { // You can further configure the scheduler instance here, like // add job listeners, trigger listeners, etc. // DO NOT call the Start method here as it will be automatically // invoked by the hosted service once it is started. var listener = new JobChainingJobListener("Chain"); var firstJob = new JobKey("ConsolePrintJob"); listener.AddJobChainLink(firstJob, new JobKey("Job1")); scheduler.ListenerManager.AddJobListener(listener, KeyMatcher <JobKey> .KeyEquals(firstJob)); } ) .UseConsoleLifetime() .UseSerilog() .Build(); await host.RunAsync(); }
private static async Task Main(string[] args) { // config Serilog logger Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.File(new CompactJsonFormatter(), "./logs/msBackend/json/log.json", rollingInterval: RollingInterval.Day) .WriteTo.File("./logs/msBackend/txt/log.txt", rollingInterval: RollingInterval.Day) .CreateLogger(); // config and setup Microsoft.Logging for Quartz using var loggerFactory = LoggerFactory.Create(builder => { builder .ClearProviders() .AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Warning) .AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Warning) .SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug) .AddSerilog(dispose: true); }); Quartz.Logging.LogContext.SetCurrentLogProvider(loggerFactory); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); var configuration = builder.Build(); MainOptions = new ModelScoutAPIOptions(); configuration.GetSection(ModelScoutAPIOptions.ModelScout) .Bind(MainOptions); // Grab the Scheduler instance from the Factory StdSchedulerFactory factory = new StdSchedulerFactory(); IScheduler scheduler = await factory.GetScheduler(); // define the job and tie it to our HelloJob class JobKey mainJobKey = new JobKey("MainJob", "group1"); IJobDetail job = JobBuilder.Create <MainJob> () .WithIdentity(mainJobKey) .Build(); // Trigger the job to run now, and then repeat every 10 seconds ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartNow() .Build(); JobChainingJobListener chain = new JobChainingJobListener("testChain"); chain.AddJobChainLink(mainJobKey, mainJobKey); // Tell quartz to schedule the job using our trigger await scheduler.ScheduleJob(job, trigger); scheduler.ListenerManager.AddJobListener(chain, GroupMatcher <JobKey> .AnyGroup()); Console.WriteLine("Press any key to start the schedular"); await Console.In.ReadLineAsync(); // and start it off await scheduler.Start(); // some sleep to show what's happening Console.WriteLine("Press any key to close the application"); await Console.In.ReadLineAsync(); // and last shut down the scheduler when you are ready to close your program await scheduler.Shutdown(); Console.ReadKey(); }
private static async Task RunProgramRunExample() { Alarm alarm1 = new Alarm() { NimId = "NIM1", Category = "operational" }; Alarm alarm2 = new Alarm() { NimId = "NIM2", Category = "operational" }; Alarm alarm3 = new Alarm() { NimId = "NIM3", Category = "service" }; AlarmRoute alarmRoute1 = new AlarmRoute() { Category = "service", CustomerID = -1, OrgId = 3, Id = "No1", Schedule = new AlarmSchedule() { Cron = "0 * * ? * FRI" }, Rules = new List <AlarmRouteRule>() { new AlarmRouteRule() { Id = 1, Destination = "Mr.Keert", Duration = 1, Fleet = 0, Repeat = 10 }, new AlarmRouteRule() { Id = 2, Destination = "Bergen", Duration = 10, Fleet = 0, Repeat = 1 }, new AlarmRouteRule() { Id = 3, Destination = "RS", Duration = 1, Fleet = 15, Repeat = 1 } } }; try { // Grab the Scheduler instance from the Factory NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" }, { "quartz.jobStore.type", "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" }, { "quartz.jobStore.tablePrefix", "QRTZ_" }, { "quartz.jobStore.dataSource", "quartzDatasource" }, { "quartz.dataSource.quartzDatasource.connectionString", @"Server=(localdb)\MSSQLLocalDB;Database=IntelinetTMSTest;Uid=quartz;Pwd=quartz123" }, { "quartz.dataSource.quartzDatasource.provider", "SqlServer" } }; StdSchedulerFactory factory = new StdSchedulerFactory(props); IScheduler scheduler = await factory.GetScheduler(); int counter = 0; List <JobKey> jobKeys = new List <JobKey>(); List <AlarmJobInfo> alarmJobInfoList = new List <AlarmJobInfo>(); foreach (AlarmRouteRule rule in alarmRoute1.Rules) { string groupname = "Route_rule_" + counter; IJobDetail job = JobBuilder.Create <AlarmAction>() .WithIdentity("AlarmRouteRule_" + rule.Id, groupname) .Build(); job.JobDataMap.Put("alarm", alarm1); job.JobDataMap.Put("rule", rule); ITrigger trigger = GetTrigger(groupname, rule, job); jobKeys.Add(new JobKey("AlarmRouteRule_" + rule.Id, groupname)); alarmJobInfoList.Add(new AlarmJobInfo() { Job = job, Trigger = trigger }); counter++; } JobChainingJobListener chain = new JobChainingJobListener("testChain"); for (int i = 1; i < jobKeys.Count; i++) { chain.AddJobChainLink(jobKeys[i - 1], jobKeys[i]); } scheduler.ListenerManager.AddJobListener(chain, GroupMatcher <JobKey> .AnyGroup()); for (int i = 0; i < alarmJobInfoList.Count - 1; i++) { IJobDetail j = alarmJobInfoList[i].Job; j.JobDataMap.Put("nextJob", alarmJobInfoList[i + 1].Job); j.JobDataMap.Put("nextTrigger", alarmJobInfoList[i + 1].Trigger); } // Tell quartz to schedule the job using our trigger await scheduler.ScheduleJob(alarmJobInfoList[0].Job, alarmJobInfoList[0].Trigger); // some sleep to show what's happening //await Task.Delay(TimeSpan.FromSeconds(30)); await scheduler.Start(); // and last shut down the scheduler when you are ready to close your program //await scheduler.Shutdown(); } catch (SchedulerException se) { Console.WriteLine(se); } }