private static IScheduler CreateScheduler(string customerCode) { var ramJobStore = new RAMJobStore(); var schedulerResources = new QuartzSchedulerResources { JobRunShellFactory = new StdJobRunShellFactory(), JobStore = ramJobStore, Name = $"TasksScheduler_{customerCode}" }; var threadPool = new DefaultThreadPool(); threadPool.Initialize(); schedulerResources.ThreadPool = threadPool; var quartzScheduler = new QuartzScheduler(schedulerResources, TimeSpan.Zero); ITypeLoadHelper loadHelper; try { loadHelper = ObjectUtils.InstantiateType <ITypeLoadHelper>(typeof(SimpleTypeLoadHelper)); } catch (Exception e) { throw new SchedulerConfigException("Unable to instantiate type load helper: {0}".FormatInvariant(e.Message), e); } loadHelper.Initialize(); ramJobStore.Initialize(loadHelper, quartzScheduler.SchedulerSignaler); var standartScheduler = new StdScheduler(quartzScheduler); schedulerResources.JobRunShellFactory.Initialize(standartScheduler); quartzScheduler.Initialize(); SchedulerRepository schedRep = SchedulerRepository.Instance; schedRep.Bind(standartScheduler); return(standartScheduler); }
public async Task TestAcquireTriggers() { ISchedulerSignaler schedSignaler = new SampleSignaler(); ITypeLoadHelper loadHelper = new SimpleTypeLoadHelper(); loadHelper.Initialize(); RAMJobStore store = new RAMJobStore(); await store.Initialize(loadHelper, schedSignaler); // Setup: Store jobs and triggers. DateTime startTime0 = DateTime.UtcNow.AddMinutes(1).ToUniversalTime(); // a min from now. for (int i = 0; i < 10; i++) { DateTime startTime = startTime0.AddMinutes(i * 1); // a min apart IJobDetail job = JobBuilder.Create <NoOpJob>().WithIdentity("job" + i).Build(); SimpleScheduleBuilder schedule = SimpleScheduleBuilder.RepeatMinutelyForever(2); IOperableTrigger trigger = (IOperableTrigger)TriggerBuilder.Create().WithIdentity("job" + i).WithSchedule(schedule).ForJob(job).StartAt(startTime).Build(); // Manually trigger the first fire time computation that scheduler would do. Otherwise // the store.acquireNextTriggers() will not work properly. DateTimeOffset?fireTime = trigger.ComputeFirstFireTimeUtc(null); Assert.AreEqual(true, fireTime != null); await store.StoreJobAndTrigger(job, trigger); } // Test acquire one trigger at a time for (int i = 0; i < 10; i++) { DateTimeOffset noLaterThan = startTime0.AddMinutes(i); int maxCount = 1; TimeSpan timeWindow = TimeSpan.Zero; var triggers = await store.AcquireNextTriggers(noLaterThan, maxCount, timeWindow); Assert.AreEqual(1, triggers.Count); var trigger = triggers.First(); Assert.AreEqual("job" + i, trigger.Key.Name); // Let's remove the trigger now. await store.RemoveJob(trigger.JobKey); } }
public void TestAcquireTriggersInBatch() { ISchedulerSignaler schedSignaler = new SampleSignaler(); ITypeLoadHelper loadHelper = new SimpleTypeLoadHelper(); loadHelper.Initialize(); RAMJobStore store = new RAMJobStore(); store.Initialize(loadHelper, schedSignaler); // Setup: Store jobs and triggers. DateTimeOffset startTime0 = DateTimeOffset.UtcNow.AddMinutes(1); // a min from now. for (int i = 0; i < 10; i++) { DateTimeOffset startTime = startTime0.AddMinutes(i); // a min apart IJobDetail job = JobBuilder.Create <NoOpJob>().WithIdentity("job" + i).Build(); SimpleScheduleBuilder schedule = SimpleScheduleBuilder.RepeatMinutelyForever(2); IOperableTrigger trigger = (IOperableTrigger)TriggerBuilder.Create().WithIdentity("job" + i).WithSchedule(schedule).ForJob(job).StartAt(startTime).Build(); // Manually trigger the first fire time computation that scheduler would do. Otherwise // the store.acquireNextTriggers() will not work properly. DateTimeOffset?fireTime = trigger.ComputeFirstFireTimeUtc(null); Assert.AreEqual(true, fireTime != null); store.StoreJobAndTrigger(job, trigger); } // Test acquire batch of triggers at a time DateTimeOffset noLaterThan = startTime0.AddMinutes(10); int maxCount = 7; TimeSpan timeWindow = TimeSpan.FromMinutes(8); IList <IOperableTrigger> triggers = store.AcquireNextTriggers(noLaterThan, maxCount, timeWindow); Assert.AreEqual(7, triggers.Count); for (int i = 0; i < 7; i++) { Assert.AreEqual("job" + i, triggers[i].Key.Name); } }